3

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

riadh gomri
  • 869
  • 1
  • 15
  • 21

1 Answers1

5

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you for the quick answer, as always in our job, i agree with you that there is no good practice that works for all projects. Child actions is a good alternative but suppose i want to acess in the controller (or the service) to informations retrieved from child actions ? For example user infos. i have to duplicate the code or call it twice once in the service to retrieve the information that i need and update the view depending in this infos and once in the child action that will show users informations. Any examples in IViewModel pattern? how to use it to show different views and DI ? – riadh gomri Dec 24 '12 at 11:08
  • The example you provided with UserInfos is something that should be managed by your membership provider. If you want to access the user info in your controller you simply query your membership provider to give you this information for the currently authenticated user. As far as your second question about the IViewModel pattern is concerned, I am not really sure nor understand what you are asking. As far as the different views are concerned, you could use custom Editor/Display templates which based on the exact type of your view model will automatically render the corresponding template. – Darin Dimitrov Dec 24 '12 at 11:15
  • A base controller class is the best place to replace the User property (using the new keyword) to return a custom IPrincipal. The controller can then populate the view model with any required user data. You can also do this for views in a base page. – Jamie Ide Dec 24 '12 at 18:43