views:

327

answers:

1

Hello,

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 architecture with their ModelView objects. However, I'm not sure what are the best practices when this kind of architecture is in case.

  • Does ModelView objects can contain Model objects?
  • If MVVM used, is it advisable that Model objects are used on Views?
  • Where validation should be implemented, on ModelView or Model classes?
  • Should business layer (service layer) know about ModelViews, and who is responsible for mapping between ModelViews and Model?
+2  A: 
  • 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.

çağdaş
Thanks for you reply. Your answers were very helpful. The 4. question came up because the business logic will be used from various points: UI, API and maybe other application with totally different UI technology. That is why I asked myself is it good that business logic returns ViewModels that are suitable for the first UI, ASP.NET MVC app. Also in WPF, ViewModels are clearly part of the presentation layer.
Misha N.
Glad it was helpful :)
çağdaş
@çağdaş: Would you agree then, that if your model classes were sufficient for use in your views then you shouldn't be needing ViewModel objects? Ie. it's an unnecessary complication?
cottsak
@cottsak. Yes, exactly. After all, views are responsible for displaying **models**. It really doesn't matter if it's a viewmodel or model. As long as the model is enough for presentation, you don't need another object.
çağdaş