I’m unclear on the best way to have multiple view models that derive from a single model.
The model has the data annotations currently but we like the flexibility of view models as an abstraction layer and also to provide the flexibility of using the model attributes with different annotations depending on the views needs.
I’ve seen declaring the base model with MetadataType attribute to point to the view model that has the data annotations but this forces a 1:1 relationship when we need a 1:M (1 model : M viewmodels).
We aren’t using mappers at the moment and leaving mappers aside, what would be the best way to manually accomplish having view models that include the data annotations that represent a single model. Perhaps having the viewmodel derive from the model class, removing the model data annotations and then creating properties in the view model that mirror the model (but can’t be the same name as that would shadow the model properties?) and have the view dictated data annotations on the view model property such as …
[DisplayName("Version Number")]
[StringLength(30)]
public double VMVersionNumber
{
get {
return VersionNumber; // VersionNumber is model property
}
set {
value = VersionNumber;
}
}
Thanks
Wouldn’t composition work better than inheritance?