I have some complex objects. Let's suppose my objects are as follows:
public class addressModel
{
public string street {get;set;}
public string number {get;set;}
public string state {get;set;}
}
public class contactModel
{
public string firstName {get;set;}
public string lastName {get;set;}
public addressModel address {get;set;}
}
Now I need additional classes that inherit from contactModel, but one of them requires addressModel and the other does not. This is easily handled by adding the [Required] attribute to a MetadataType class:
[MetadataType(typeof(driverMetadata))]
public class driverModel : contactModel
{
}
public class driverMetadata
{
[Required]
public addressModel address {get;set;}
}
But what if I need a third class that only needs the street and number properties of the addressModel to be required? Would I need to create a new class that inherits from addressModel and assign the MetadataType to this new class?
I totally understand that I could create a new address model class with only those 2 properties, but now imagine a class with several tens of properties. We do not want to create ad-hoc classes that mix and match the different properties we need.
I finally gave up on this and proceeded with the ad-hoc classes. This greatly reduces reusability (and therefore productivity), but as we say in Chile: "the optimum is an enemy of the good".