bloodfire1004 bloodfire1004 - 1 year ago 73
C# Question

What are the different approaches to Object-Object mapping in .NET?

I'm needing to do some mapping between objects (e.g. PersonModel to PersonViewModel) and am researching on the different approaches to do this. Specifically I'm working with Entity Framework and trying to map the generated models to a viewmodel.

However, I've yet to find an article or resource that compiles a list of how you can do this. So far, I've come across the following:

  • Implicit Conversion (I think this is the most basic approach? since you manually map properties from one object to another, its simple but tedious)

  • Extension Methods (haven't worked with this yet)

  • Reflection (I've tinkered a bit, but only managed very basic mapping)

  • Automapper (VERY popular, but I'm having trouble making it work well with EF)

  • Value Injecter (haven't worked with this yet)

  • Emit Mapper (haven't worked with this yet, but probably I would have trouble making it work with EF?)

Can you please help point out and elaborate on the approaches available there, as well as pros / cons of each? For example, I saw some that mentioned Automapper is slow compared to manual mapping? Or possibly, point out an article that tackles this?

EDIT: since some may ask what problem I have with AutoMapper, please see this: Automapper: How to map IList to EntityCollection

Answer Source

Well, I can give you a way where you do your own mapping, pretty simple to do and can be executed quickly over a large amount of data. I'll show you what I'd do, and then try to elaborate on why I do what I do. Here goes:

public class PersonViewModel
    public static Expression<Func<Person, PersonViewModel>> FromPerson
            return p => new PersonViewModel
                Name = p.FirstName,
                SurName = p.LastName

    public string Name { get; set; }
    public string SurName { get; set; }
    public static PersonViewModel CreateViewModel(Person original)
        var func = FromPerson.Compile();
        var vm = func(original);

        return vm;

Now you'll notice that I have 2 ways to convert from a Person EF model to a ViewModel. This is because the first one, which uses the Expression>, is used to convert a large bulk of object in a Select() statement. Simple usage:

return people.Select(PersonViewModel.FromPerson);

In this case we've probably retrieved a collection of Person objects from the DB and need to show them, say, in a list or something, but using the ViewModel. In this way the operation is performed in bulk and is much faster than simply creating all the objects via the other method. Now, the static CreateViewModel method can be used to map a single object where needed. An example is if you've gotten a single user's data from the DB and need to show it, but using your ViewModel. In that case, it would be appropriate to use the static method, instead of the Expression, which is mainly for bulk conversions.

That's what I can offer, aside from wondering what's wrong with using AutoMapper, since it's pretty straightforward and you haven't really elaborated on what the problem is with using it alongside EF. Hope this helps you at least a little bit in your problem :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download