Which is the best approach? AutoMapper against implicit (C# Reference)

橙三吉。 提交于 2019-12-10 21:51:50

问题


Automapper is a way to match types, ideally when you want to map a model and its viewmodel. But is this not the same approach that we can make with implicit in C#? (Suppose that both model have the same properties but with different names, on this case, you need to specify in AutoMapper which is linked between models)

With autommaper we have

public class Employee
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class EmployeeViewItem
{
    public string Name { get; set; }
    public string Email { get; set; }
}

Usually we do:

Employee employee = new Employee
{
    Name = "John SMith",
    Email = "john@codearsenal.net"
}

EmployeeViewItem viewItem = new EmployeeViewItem();
viewItem.Name = employee.Name;
viewItem.Email = employee.Email;

with AutoMapper

 EmployeeViewItem employeeVIewItem = Mapper.Map<Employee, EmployeeViewItem>(employee); 

Now, with the implicit C# Reference

public class Employee
{
    public static implicit operator EmployeeViewItem(Employee employee)
    {
         EmployeeViewItem viewItem = new EmployeeViewItem();
         viewItem.Name = employee.Name;
         viewItem.Email = employee.Email;
         return viewItem;
    }

    public static implicit operator Employee(EmployeeViewItem ev)
    {
        var e = new Employee();
        e.Name = ev.Name;
        e.Email = ev.Email;
        return e;
    }
}

回答1:


I say no to this use of implicit.

The viewmodel in this example has no extra properties. However if this really was the case you wouldn't require a viewmodel at all. in reality it would have a number of other properties, possibly containing data not from the original model. eg. IsSelected or something.

The implicit conversion is suppose to work without loss of data and this would not be possible with the reverse conversion back to the model

Secondly!

The purpose of the viewmodel is to match data required by the view. you are supposed to have mutiple viewmodels per model. eg. perhaps you have an edit view and a non editable view. or a mobile app and a webpage!

the model shouldn't know about these views or their models and the use of implicit would require it to be coupled




回答2:


AutoMapper uses reflection to map properties (slight performance overhead), allows advanced custom rules for mapping and requires 1 line of code in basic (common?) scenarios.

Implicit operators require you to specify each property, are prone to errors (adding a new property but not adding it to the operator), are more difficult to setup for multiple types, create lots of useless code and even in the most basic setup you still have N lines of code where N is the amount of properties.

I think it speaks for itself.




回答3:


This depends on your particular scenario.

If AutoMapper is capable of mapping your classes onto each other, and you don't need any special logic to happen during the mapping, then why bother writing all that code yourself when there's something out there that can do it for you with a NuGet reference and a couple of lines? Plus, this method will adapt as you change the classes, saving you the trouble of maintaining it.

If you need to do clever things during the mapping that AutoMapper can't do, or for some reason AutoMapper isn't available in your project, then you'll have to write your own operators. If it can be avoided, I'd advise against it, but sometimes you're stuck with it.




回答4:


Automapper basically allows you:

  1. To forget about routine, trivial code;
  2. To handle complex type internal properties automatically;
  3. To skip (ignore) some properties during copy-construction;
  4. Advanced: to create IOC-friendly mapping procedures;
  5. To configure 1-3 processes fluently in one place.

But it is obviously slower than handwritten code. So in case you are not concern about performance very much - you should give it a try, it may be worthful. Otherwise you can try something faster (for ex. emitmapper) or write you own mappers by hand and combine to convert complex types.

My experience shows that viewmodels more often are very different from models (DTO's) coz they are created for different tasks, to solve different problems. So automatic mapping would be difficult in such scenarios, оr not advantageous (become unreadable clutter, conglomeration)



来源:https://stackoverflow.com/questions/29257215/which-is-the-best-approach-automapper-against-implicit-c-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!