1

I've become aware of an itch due to some non-DRY code pertaining to view model classes in an (ASP.NET) MVC web application and I'm thinking of scratching my itch by organizing code in various 'partial view model' classes.

By partial-view-model, I'm referring to a class like a view model class in an analogous way to how partial views are like views, i.e. a way to encapsulate common info and behavior.

To strengthen the 'analogy', and to aid in visually organizing the code in my IDE, I was thinking of naming the partial-view-model classes with a _ prefix, e.g. _ParentItemViewModel.

As a slightly more concrete example of why I'm thinking along these lines, imagine that I have a domain-model-entity class ParentItem and the user-friendly descriptive text that identifies these items to users is complex enough that I'd like to encapsulate that code in a method in a _ParentItemViewModel class, for which I can then include an object or a collection of objects of that class in all the view model classes for all the views that need to include a reference to a parent item, e.g. ChildItemViewModel can have a ParentItem property of the _ParentItemViewModel class type, so that in my ChildItemView view, I can use @Model.ParentItem.UserFriendlyDescription as desired, like breadcrumbs, links, etc.


As a second example, imagine that I have entity classes SomeKindOfBatch, SomeKindOfBatchDetail, and SomeKindOfBatchDetailEvent, and a view model class and at least one view for each of those entities. Also, the example application covers a lot more than just some-kind-of-batches, so that it wouldn't really be useful or sensible to include info about a specific some-kind-of-batch in all of the project view model classes. But, like the above example, I have some code, say for generating a string for identifying a some-kind-of-batch in a user-friendly way, and I'd like to be able to use that in several views, say as breadcrumb text or text for a link.

As a third example, I'll describe another pattern I'm currently using. I have a Contact entity class, but it's a fat class, with dozens of properties, and at least a dozen references to other fat classes. However, a lot of view model classes need properties for referencing a specific contact and most of those need other properties for collections of contacts, e.g. possible contacts to be referenced for some kind of relationship. Most of these view model classes only need a small fraction of all of the available contact info, basically just an ID and some kind of user-friendly description (i.e. a friendly name). It seems to be pretty useful to have a 'partial view model' class for contacts that all of these other view model classes can use.

Maybe I'm just misunderstanding 'view model class' – I understand a view model class as always corresponding to a view. But maybe I'm assuming too much.

  • You realize that all you're talking about is ViewModel inheritance, right? Having a BaseViewModel (hopefully named better than that) does not break the MVVM structure so the only trade-offs you're going to have are those between inheritance and non-inheritance. Since inheritance is standard to OOP languages like C# due to the maintainability benefits, I would go with that. – Aaron Hawkins Feb 5 '14 at 23:02
  • @AaronHawkins – I don't understand how my question is all about inheritance. The 'parent' class objects have children; they're not a kind of child. But this is not really a strong question, I'll now readily admit. I don't think what I described, and what I'm doing, is going to be an issue. – Kenny Evitt Feb 6 '14 at 0:54
  • 1
    Maybe I'm misunderstanding, but it sounds like you want to encapsulate common info and behavior across most or all of your view models so that each of your view models have some of the same properties and methods. If that's the case, then you probably want to use inheritance to create some sort of base view model that is inherited by most or all of your view models. – Aaron Hawkins Feb 6 '14 at 14:31
  • @AaronHawkins – I added more examples; hopefully my confusion is clearer. I think I may be over-analyzing this. – Kenny Evitt Feb 6 '14 at 15:13
  • 1
    Can you provide some code examples of a slim version of your Model and ViewModel? Maybe that will help me get the idea of what you're doing because now I'm not sure if you're talking about inheritance (eg. CompanyContact vs. EmployeeContact) or just looking for a way to get at the name of the entity (eg. John Smith vs. Jane Doe). – Aaron Hawkins Feb 6 '14 at 15:57
1

I use partial view models all of the time when the 'partial' part is supposed to look the same between different pages. For example, if I have a list of items that may be displayed in the same way on several pages, I will have a partial view model (e.g. '_ListOfWhatevers.cshtml') that may use IList<Whatever> as the model type upon which it is based. Then I will render that partial view with the appropriate list of items from my higher level views. So far I have not encountered a point where I wish I had not done it. It helps a lot with DRY when you have many pages showing the same type of data in slightly different contexts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.