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

后端 未结 4 1457
臣服心动
臣服心动 2021-01-12 23:11

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#? (

相关标签:
4条回答
  • 2021-01-12 23:24

    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.

    0 讨论(0)
  • 2021-01-12 23:25

    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.

    0 讨论(0)
  • 2021-01-12 23:31

    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)

    0 讨论(0)
  • 2021-01-12 23:46

    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

    0 讨论(0)
提交回复
热议问题