开发者

ASP.NET MVC, Model and ViewModel separation of concerns?

开发者 https://www.devze.com 2022-12-08 23:11 出处:网络
Lately, I\'ve been exploring what\'s the best way to organize presentation layer of ASP.NET MVC application when underlying Model is complex. What came up till now is MVVM architectu开发者_如何转开发r

Lately, I've been exploring what's the best way to organize presentation layer of ASP.NET MVC application when underlying Model is complex. What came up till now is MVVM architectu开发者_如何转开发re with their ViewModel objects. However, I'm not sure what are the best practices when this kind of architecture is in case.

  • Does ViewModel objects can contain Model objects?
  • If MVVM used, is it advisable that Model objects are used on Views?
  • Where validation should be implemented, on ViewModel or Model classes?
  • Should business layer (service layer) know about ViewModel , and who is responsible for mapping between ViewModel and Model?


  • Most of the times, ModelView objects are just containers holding Model objects when we need to send multiple types of them, or when we need to pass couple of more properties which are only needed in Views.
  • If the View's job is to display the details of a Model and there is nothing else to pass to View, why not?
  • Depends. You can use your ModelView and Model classes just to carry data between layers. And take care of validation via cutom model binders or with a service layer.
  • There's no reason why they shouldn't know about ModelViews. But usually you just get the requested Model(s) from the service layer from the controller, and then pass it/them directly or in a ModelView to the View.

BTW, I wouldn't consider ModelViews as an architecture. It's more like "use one when you need one". After all, there's no point in doing this to pass data to view :

class SomeModelView {
    public MyModel model { get; set; }
}

Just pass the MyModel if that's enough for the view to do its job.

0

精彩评论

暂无评论...
验证码 换一张
取 消