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

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

提交回复
热议问题