我已經做了接口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;
}
但我不想一次又一次地寫這個方法。那麼做正確的做法是什麼?
將其公開並將其放置在共享位置? – Ehsan
寫實用程序的方法:) –
看看命名空間[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