[英]MetadataType for Complex Objects
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. 现在,我需要从contactModel继承的其他类,但是其中一个需要addressModel,而另一个则不需要。 This is easily handled by adding the [Required] attribute to a MetadataType class: 通过将[Required]属性添加到MetadataType类中,可以轻松解决此问题:
[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? 但是,如果我需要只需要addressModel的街道和数字属性的第三类怎么办? Would I need to create a new class that inherits from addressModel and assign the MetadataType to this new class? 我是否需要创建一个继承自addressModel的新类并将MetadataType分配给该新类?
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. 我们不想创建临时类来混合和匹配我们所需的不同属性。
Any help/guidance/suggestion will ne GREATLY appreciated. 任何帮助/指导/建议将不胜感激。
Thanks in advaced, Ariel 谢谢您,Ariel
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". 这极大地降低了可重用性(从而降低了生产率),但是正如我们在智利所说的那样:“最优是商品的敌人”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.