Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper inheritance and Linq

I've been looking over how to use Inheritance in AutoMapper but I'm struggling to get it working fully with Linq. Here is my code:

I have defined my mappings here:

CreateMap<Article, ArticleDetailsViewModel>()
    .Include<Article, ArticleNewsItemDetailsViewModel();

CreateMap<Article, ArticleNewsItemDetailsViewModel>();

ArticleDetailsViewModel is a base class of ArticleNewsItemDetailsViewModel.

Now here lies the problem, if I had:

CreateMap<ArticleNewsItem, ArticleNewsItemDetailsViewModel>();

All of the properties in the view model would automatically map because they are the same name as their Linq object counterpart. However, because I am using the Article => ArticleNewsItemDetailsViewModel mapping this is not possible, instead I would have to define each one as:

.ForMember(x => x.Property1, opt => opt.MapFrom(src => src.ArticleNewsItem.Property1)

I thought about moving all properties from ArticleNewsItemDetailsViewModel into a new view model and having that class a property within the ArticleNewsItemDetailsViewModel and as long as there is a mapping between those two objects then it will work, but it doesn't feel very clean.

Is there any way to avoid having to do this?

like image 729
ediblecode Avatar asked Dec 12 '12 12:12

ediblecode


People also ask

What is ProjectTo in Linq?

The . ProjectTo<OrderLineDTO>() will tell AutoMapper's mapping engine to emit a select clause to the IQueryable that will inform entity framework that it only needs to query the Name column of the Item table, same as if you manually projected your IQueryable to an OrderLineDTO with a Select clause.

What is AutoMapper in Entity Framework?

AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class. Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure.

When should I use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.


1 Answers

Supposing you have the following classes:

    public class Article
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
        public ArticleNewsItem ArticleNewsItem { get; set; }
    }

    public class ArticleDetailsViewModel
    {
        public string Prop1 { get; set; }
    }

    public class ArticleNewsItemDetailsViewModel : ArticleDetailsViewModel
    {
        public string Prop2 { get; set; }
        public string Prop3 { get; set; }
    }

    public class ArticleNewsItem
    {
        public string Prop3 { get; set; }
    }

The mapping should look like below:

var res = Mapper.Map<Article, ArticleNewsItemDetailsViewModel>(_article);
Mapper.Map(_article.ArticleNewsItem, res);

Moreover you can create custom type converter to avoid writing these two lines every time you need to map Article to ArticleNewsItemDetailsViewModel.

like image 73
k0stya Avatar answered Oct 11 '22 14:10

k0stya