企业库验证块 - 根据另一个属性值进行验证

时间:2010-09-20 09:07:24

标签: c# validation enterprise-library

仅当第三个字段具有特定值时,才需要验证两个字段。 在这段代码中,我想使用不存在的CheckIf属性。 只有当另一个属性具有特定值时,才可以验证字段吗?

public string CustomerType { get; set; } // P=Private B=Business

[NotNullValidator(MessageTemplate = "You must specify the property 'Name'", CheckIf = "CustomerType=='P'")]
public string PrivateName { get; set; }

[NotNullValidator(MessageTemplate = "You must specify the property 'Name'", CheckIf = "CustomerType=='B'")]
public string BusinessName { get; set; }

谢谢!!!

1 个答案:

答案 0 :(得分:1)

从验证角度来看,我同意Siva您可以使用SelfValidation来实现此目的。然而,从OO的角度看你的代码时,我不禁注意到好好看看你的设计。您似乎要么向我们展示两种子类型Customer,即PrivateCustomerBusinessCustomer

class Customer
{
}

class PrivateCustomer : Customer
{
    public string PrivateName { get; set; }
}

class BusinessCustomer : Customer
{
    public string BusinessName { get; set; }
}

或者......这两个属性实际上是同一个东西。在这两种情况下,您的验证消息甚至会将其称为“名称”。在这种情况下,你最终会得到这个设计:

class Customer : Customer
{
    public string CustomerType { get; set; }

    public string Name { get; set; }
}
相关问题