2014-03-31 50 views
4

我已經做了接口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; 
    } 

但我不想一次又一次地寫這個方法。那麼做正確的做法是什麼?

+0

將其公開並將其放置在共享位置? – Ehsan

+1

寫實用程序的方法:) –

+1

看看命名空間[System.ComponentModel.DataAnnotations](http://msdn.microsoft.com/de-de/library/system.componentmodel.dataannotations.aspx)和[Validator] (http://msdn.microsoft.com/de-de/library/system.componentmodel.dataannotations.validator(v=vs.110).aspx)class – Jehof

回答

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... 
    } 
} 
+0

但在那種情況下,我需要在所有不能解決我的目的的類中實現IRequestValidator。 – Rohit

+0

除非不同的IRequest實現具有不同的驗證規則,否則您只需要一個'IRequestValidator'實現 – samy

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莫名其妙。