What are the different approaches to Object-Object mapping in .NET?
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 :)
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