I am wondering about how to approach inheritance with View Models in the MVVM pattern. In my application I have a Data Model that resembles the following:
class CustomObject
{
public string Title { get; set; }
}
class CustomItem : CustomObject
{
public string Description { get; set; }
}
class CustomProduct : CustomItem
{
public double Price { get; set; }
}
In my application I have a ViewModelBase class and then was going to have the following View Models:
- CustomObjectViewModel
- CustomItemViewModel
- CustomProductViewModel
A rough implementation of the CustomObjectViewModel would resemble the following:
class CustomObjectViewModel : ViewModelBase
{
private readonly CustomObject _customObject;
public CustomObjectViewModel( CustomObject customObject )
{
_customObject = customObject;
}
public string Title
{
// implementation excluded for brevity
}
}
It seems logical to me that my View Models would extend themselves in the same manner as my Model did (CustomItemViewModel extends CustomObjectViewModel and so on). However, I have noticed that as I go down the inheritance tree I'll be adding additional references to the same object. This seems rather excessive to me and was wondering how to approach this problem and if it were possible to make it much cleaner.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…