11

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

Community
  • 1
  • 1
bloodfire1004
  • 493
  • 2
  • 8
  • 24
  • You are asking about an entire article or even a book chapter. SO is a Q&A site. And Automapper has no problems working with EF, mainly because objects returned by EF are plain-old objects. If you use EF specific classes like `EntityCollection` you are doing it wrong. Check all tutorials and you'll see that they use the most basic `ICollection` – Panagiotis Kanavos Aug 19 '16 at 14:31
  • You can also you an [Explicit Casting](https://medium.com/antarrscripts/refactoring-dotnet-explicit-operators-d34030e667e6) – Antarr Byrd Mar 13 '19 at 08:14

2 Answers2

5

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
    {
        get
        {
            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 :)

  • I would suggest that `CreateViewModel` would use `FromPerson` to reduce code duplication. Almost any real-life mapping would have much more properties so applying DRY (Don't Repeat Yourself) principle would make sense. – Phil1970 Aug 19 '16 at 14:26
  • Ah, you're right, I actually forgot about that since I wrote it on the fly, thanks for the correction, will fix it. – Konstantin Severy Sep 13 '16 at 06:53
2

Well, if you do know the objects’ types upfront then the accepted answer works great.

If not I’d go with AutoMapper or PropMapper.

If you want to roll something of your own, the most “up to date” approach is to use compiled Expression trees. You enumerate the type’s properties and then build a block of assign expressions for each property, and “compile” this block:

var e = Expression.Assign(Expression.Property(srcObj, prop1), Expression.Property(destObj, prop2)));

Here’s a step-by-step blog post on this: https://dev.to/alexjitbit/yet-another---lightning-fast---object-mapper-for-net-2bj2

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149