Whats the best way to map without automapper?

Hi

At present I am doing all my mapping with static extension methods, all my models are very complex, have many tiers, and some higher tiers need filling with information from a database. So i have I was using the despondency resolver to inject the services,
however this causes issues, and after reading up it seems its also bad practice and I understand that.

However I can’t find a different way to do the mapping which will allow me to map efficently, I could pass the services through the extensions but that would make them very messy, so anyone have any other information I could look at to get some ideas.

Any information would be appreciated.

EnenDaveyBoy

owever this causes issues

What kind?

EnenDaveyBoy

after reading up it seems its also bad practice and I understand that.

Where DI is bad practice?

friends don’t let friends user auto mapper.  I suggest putting your complex logic into an IQueryable<PocoType> method and when you are done with your nested tiered querys then parse your poco with your simplified dto logic. dive into some expression logic
to simplify what query tree pieces you wish to modify and perhaps you can find your pattern.

http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions

I also suggest checking out Expression<Func

http://tomasp.net/blog/linq-expand.aspx/

Hth.

ignatandrei

EnenDaveyBoy

owever this causes issues

What kind?

EnenDaveyBoy

after reading up it seems its also bad practice and I understand that.

Where DI is bad practice?

The issues are the DB connection with DI and autofac., I am using this:

 public static class WebsiteGroupMap
    {
        private static IWebsiteService _websiteService = DependencyResolver.Current.GetService<IWebsiteService>();
        private static IWebsiteCategoryService _websiteCategoryService = DependencyResolver.Current.GetService<IWebsiteCategoryService>();

        public static CreateAndUpdateWebsiteGroupViewModel BlankViewModel(this  CreateAndUpdateWebsiteGroupViewModel model)
        {
            model.AddEssentials();

            return model;
        }

        public static CreateAndUpdateWebsiteGroupViewModel ViewModelFromEntity(this CreateAndUpdateWebsiteGroupViewModel model, WebsiteGroup entity)
        {
            model.ModelId = entity.Id;
            model.Name = entity.Name.Where(t => t.LocalizationCodeId == model.CultureId).FirstOrDefault().Text;

            model.websiteSelected = entity.Websites == null ? null : entity.Websites.Select(s => s.Id).ToArray();
            model.categorySelected = entity.Categories == null ? null : entity.Categories.Select(s => s.Id).ToArray();
            model.AddEssentials();

            return model;
        }

        public static CreateAndUpdateWebsiteGroupViewModel ViewModelToViewModel(this  CreateAndUpdateWebsiteGroupViewModel model)
        {
            model.AddEssentials();

            return model;
        }

        public static CreateAndUpdateWebsiteGroupViewModel AddEssentials(this  CreateAndUpdateWebsiteGroupViewModel model)
        {

            var websites = _websiteService.GetAll(model.CompanyId);

            model.WebsitesList = websites.Select(w => new SelectListItem { Value = w.Id.ToString(), Text = w.Name.Where(t => t.LocalizationCodeId == model.CultureId).FirstOrDefault().Text }).ToList();

            var categories = _websiteCategoryService.GetItems(t => t.CompanyId == model.CompanyId, new string[] {"Name"}).ToList();
                                                
            model.WebsitesList = categories.Select(w => new SelectListItem { Value = w.Id.ToString(), Text = w.Name.Where(t => t.LocalizationCodeId == model.CultureId).FirstOrDefault().Text }).ToList();
            

            return model;
        }

        public static WebsiteGroup EntityFromViewModel(this WebsiteGroup entity, CreateAndUpdateWebsiteGroupViewModel model)
        {
            if (entity.Id == 0)
            {
                entity.BuildCompanyEntity();
                entity.CompanyId = model.CompanyId;
            }

            //Add Created or Modified Information
            if (entity.Modifications == null)
            {
                entity.Modifications = new List<Data.EFModels.Modifications.Modification>();
                entity.Modifications.Add(new Modification().Build(1, model.CompanyId, model.EmployeeId));
            }
            else
            {
                entity.Modifications.Add(new Modification().Build(2, model.CompanyId, model.EmployeeId));
            }

            //Add Additional Modified Information
            entity.Modifications.Add(new Modification().Build(model.ModificationTypeId, model.CompanyId, model.EmployeeId));

            entity.Id = model.ModelId;

            entity.Name = entity.Name == null ? new List<TextResource256>().EntityFromViewModel(model.ModelId, model.Name, model.CultureId) : entity.Name.EntityFromViewModel(entity.Name.Where(t => t.LocalizationCodeId == model.CultureId).FirstOrDefault().Id, model.Name, model.CultureId);

            entity.ProcessWebsites(model.websiteSelected);
            entity.ProcessWebsiteCategories(model.categorySelected);

            return entity;
        }

The problem I think is happening, is the when the script hits the controller it runs multiple its to the service layer, and at some point I am getting conflicts, the the connection is closed, or its returning null, etc, etc.

Xequence

friends don’t let friends user auto mapper.  I suggest putting your complex logic into an IQueryable<PocoType> method and when you are done with your nested tiered querys then parse your poco with your simplified dto logic. dive into some expression logic
to simplify what query tree pieces you wish to modify and perhaps you can find your pattern.

http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions

I also suggest checking out Expression<Func

http://tomasp.net/blog/linq-expand.aspx/

Hth.

Thanks but there is nothing in regards to mapping in your post, I like the mapping I am using but

http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container

I think this is the issue I am having.

it appears you are mapping static methods in your dependency.

I think you are getting concurrent instances of your context in your data layer. Do not map your dependencies in static files, only in a single instance at APPSTART.

I would suggest verfiying your architecture.

Here is a simple example.

https://genericunitofworkandrepositories.codeplex.com/

Now I am confused, I am not mapping anything in my dependency (i don’t think)

I am mapping entities to viewmodels and back again in static methods, which in general terms is widely done, my issues it I am trying to use instances of my service layer in these methods, which I am assuming is out of scope.

If your link had complex mapping on it, I would be able to know if it helps, but alas it doesn’t , so I am no closer to finding a solution.

Sorry the link didn’t help. The code will explain how you properly use DI/IOC.

but it won’t, theres no mapping, I have it setup like that for everything else, my issues is with how I am injecting the services into the mapping, without it causing errors.

If I do all my mapping in the controller I wouldn’t have a problem, I am pretty certain If i didn’t use static extention methods and inject my services using DependencyResolver.Current.GetService I wouldn’t be getting errors, but I can’t find any examples
of how to map complex viewmodels, especially with database info in later tiers.

EnenDaveyBoy

and at some point I am getting conflicts, the the connection is closed.

Please show where connection is closed( maybe you did not load all?)


EnenDaveyBoy

and at some point I am getting conflicts, [..] its returning null, etc, etc.

null it is part of developer life.The connection is null or the database entity?

ignatandrei

EnenDaveyBoy

and at some point I am getting conflicts, the the connection is closed.

Please show where connection is closed( maybe you did not load all?)

EnenDaveyBoy

and at some point I am getting conflicts, [..] its returning null, etc, etc.

null it is part of developer life.The connection is null or the database entity?

Tomorrow I am going to keep my mapping in my static files, but I am going to remove the:

private static IWebsiteService _websiteService = DependencyResolver.Current.GetService<IWebsiteService>();

and I am going to so all the

var websites = _websiteService.GetItems(null, new string[] { "Name" }).ToList();

in the controller and see if it sorts the issue.

If not I will be back.

Hi,

In my opinion, for that scenario, it’s bad to use the static field.

Best Regards

Starain

Leave a Reply