3
votes

First i want to share with you this very interesting article about ViewModel : http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications

I have one questions
Is it a good practice that all ViewModel classes derive from a base class ex : BaseViewModel since most websites/web applications have common infos to display, generally in the Master Page ? In ASP.NET MVC that works fine, the layout(master page) model is the BaseViewModel and each View have a diffrent ViewModel.

Any examples in using ViewModel Interfaces is also welcome.

Thanks. Riadh

1

1 Answers

5
votes

Is it a good practice that all ViewModel classes derive from a base class

While that might be true in some cases, it is not something that could be generalized for all ASP.NET MVC applications. There are also other ways of displaying common information in all ASP.NET MVC views such as for example using child actions.

Child actions are completely independent of the main action and even if they execute in the same HTTP request and the result aggregated, they could have a completely separate view model containing only the information needed for this specific part of your layout. They could independently query your data providers to fetch domain models and map them to the corresponding view models. All this could happen completely independent from the main action and view model. Thus your main view model doesn't need to derive from a common base view model.

So you cannot really generalize about whether using common base view models is best practice or not. The simple answer to your question is: it depends on the specifics of the application you are developing.