验证接口属性

时间:2014-03-31 10:45:01

标签: c# interface

我创建了一个界面IRequest

public interface IRequest
{
    [DataMember(IsRequired = true)]
    string EndUserIp { get; set; }

    [DataMember(IsRequired = true)]
    string TokenId { get; set; }

    [DataMember(IsRequired = true)]
    string ClientId { get; set; }

    [DataMember(IsRequired = true)]
    int TokenAgencyId { get; set; }

    [DataMember(IsRequired = true)]
    int TokenMemberId { get; set; }
}

我在多个班级实现了这一点;现在我需要验证所有属性:

 public static bool ValidateCommon(IRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.ClientId))
            throw new BusinessServiceException(ErrorType.InvalidRequest, "ClientId can not be null or Empty");
        if (string.IsNullOrWhiteSpace(request.TokenId))
            throw new BusinessServiceException(ErrorType.InValidSession, "TokenID should not be Null or Empty");
        if (!IsValidTokenId(request.TokenId))
            throw new BusinessServiceException(ErrorType.InValidSession, "TokenID is not in correct Format");
        if (string.IsNullOrWhiteSpace(request.EndUserIp))
            throw new BusinessServiceException(ErrorType.InValidIpAddress, "IP Address should not be Null or Empty");
        if (!IsValidIp(request.EndUserIp))
            throw new BusinessServiceException(ErrorType.InValidIpAddress, "IP Address is not in correct Format");
        if (request.TokenAgencyId < 0)
            throw new BusinessServiceException(ErrorType.InvalidRequest, "TokenAgencyId should be positive Integer");
        if (request.TokenMemberId <= 0)
            throw new BusinessServiceException(ErrorType.InvalidRequest, "TokenMemberId should be positive Integer");
        return true;
    }

但我不想一次又一次地写这种方法。那么做同样的方法是什么?

2 个答案:

答案 0 :(得分:2)

创建一个名为IRequestValidator的接口,其中一个名为Validate的函数占用IRequest。需要验证IRequest时,实现界面并实例化/注入组件。

通过这种方式,您可以在一个地方获得验证系统,并在以后轻松交换它(如果您注入组件;如果不是,请使用工厂模式以避免将new调用硬编码到您的代码中的组件。)

public interface IRequestValidator {
    bool Validate(IRequest req);
}

// at first you have one way of validating elements

public class AllWelcomeValidator: IRequestValidator {
    public bool Validate(IRequest req) {return true; // no validation atm }
}

// later...

public class NowWeGotRulesValidator: IRequestValidator {
    public bool Validate(IRequest req) {
        if (string.IsNullOrWhiteSpace(request.ClientId))
            throw new BusinessServiceException(ErrorType.InvalidRequest, "ClientId can not be null or Empty");
        // etc...
    }
}

答案 1 :(得分:0)

您可以使用Code Contracts为界面定义一些前置条件,后置条件和不变量。我不是专家,但应该可以这样做:

[ContractClass(typeof(IRequestContract))]
public interface IRequest
{
    [DataMember(IsRequired = true)]
    string EndUserIp { get; set; }

    [DataMember(IsRequired = true)]
    string TokenId { get; set; }

    [DataMember(IsRequired = true)]
    string ClientId { get; set; }

    [DataMember(IsRequired = true)]
    int TokenAgencyId { get; set; }

    [DataMember(IsRequired = true)]
    int TokenMemberId { get; set; }
}


[ContractClassFor(typeof(IRequest))]
internal abstract class IRequestContract : IRequest
{
    string EndUserIp
    { 
        get
        {
            return null;
        }
        set
        {
            Contract.Requires<BusinessServiceException>(!string.IsNullOrWhiteSpace(value), "IP Address should not be Null or Empty");
            Contract.Requires<BusinessServiceException>(!IsValidIp(request.EndUserIp, "IP Address is not in correct Format");
        }
    }

    string TokenId 
    { 
        get
        {
            return null;
        }
        set
        {
            Contract.Requires<BusinessServiceException>(!string.IsNullOrWhiteSpace(value), "TokenID should not be Null or Empty");
            Contract.Requires<BusinessServiceException>(!IsValidTokenId(request.TokenId), "TokenID is not in correct Format");
        }
    }

    string ClientId 
    { 
        get
        {
            return null;
        }

        set
        {
            Contract.Requires<BusinessServiceException>(!string.IsNullOrWhiteSpace(value), "ClientId can not be null or Empty");
        }
    }

    int TokenAgencyId 
    { 
        get
        {
            return default(int);
        }
        set
        {
            Contract.Requires<BusinessServiceException>(value < 0, "TokenAgencyId should be positive Integer");
        }
    }

    int TokenMemberId 
    { 
        get
        {
            return null;
        }
        set
        {
             Contract.Requires<BusinessServiceException>(value < 0, "TokenMemberId should be positive Integer");
        }
    }
}

只需要找到一种方法将错误代码以某种方式传递给BusinessServiceEXception