Verify Interface Properties

I created an 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; }
}

and I implemented this in several classes; Now I need to check all the properties:

 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;
    }

But I do not want to write this method again and again. So what is the right approach to do the same?

+4
source share
2 answers

Create an interface called IRequestValidatorwith function Validateby taking IRequest. Deploy the interface and instantiate / paste the component if you need to confirm yours IRequest.

, ( , factory, 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...
    }
}
+2

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 -.

0

Source: https://habr.com/ru/post/1534233/


All Articles