1
如何通過實體類型在流暢驗證中注入或找到驗證器? 我有以下類和希望通過液態驗證驗證實體如何在流利驗證中通過實體類型注入或查找驗證器?
public class BaseEntity {}
public class Article :BaseEntity
{
public string Name {get;set;}
}
public class ArticleValidator : AbstractValidator<Article>
{
public ArticleValidator()
{
RuleFor(x => x.Name).NotEmpty().Length(0,512);
}
}
,並有一些推廣的BaseEntity:
public static ValidationResult Validate(this BaseEntity entity)
{
//????and here how i can find entity validator by type of entity and validate it and return result.
}
public class ArticleService
{
public void AddArticle(Article aricle)
{
var result = article.Validate();
if(result.IsValid)
;.......
}
}
@VaibhavMule問題中描述的代碼是C#,而不是Java。 – Radiodef