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

后端 未结 4 1585
臣服心动
臣服心动 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: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)

提交回复
热议问题