我有以下课程
public class ValidProjectHeader : AbstractValidator<Projects.ProjectHeader>
{
public ValidProjectHeader()
{
RuleFor(x => x.LobId).Must(ValidateLOBIDExists);
RuleFor(x => x.CreatedByUserId).NotEmpty();
RuleFor(x => x.ProjectManagerId).NotEmpty();
RuleFor(x => x.ProjectName).NotEmpty();
RuleFor(x => x.SalesRepId).NotEmpty();
RuleFor(x => x.DeliveryDate).NotEmpty();
RuleFor(x => x.ProjectStatusId).NotEmpty();
RuleFor(x => x.DeptartmentId).NotEmpty();
RuleFor(x => x.CustomerId).NotEmpty();
RuleSet("Insert", () =>
{
RuleFor(x => x.ProjectLines).Must(ValidateProjectLines).SetCollectionValidator(new ValidProjectLine());
});
RuleSet("Update", () =>
{
RuleFor(x => x.ProjectLines).SetCollectionValidator(new ValidProjectLine());
});
}
我想做的是用 rulset 调用验证,但是当我用 RuleSet 调用验证时,我也想返回“通用”规则。
我调用验证的代码如下
public abstract class BaseValidator
{
private List<ValidationFailure> _errors;
public bool IsValid { get; protected set; }
public List<ValidationFailure> Errors
{
get { return _errors; }
protected set { _errors = value; }
}
public virtual bool CallValidation()
{
Errors = new List<ValidationFailure>();
ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute;
IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator;
FluentValidation.Results.ValidationResult result = validator.Validate(this);
IsValid = result.IsValid;
Errors = result.Errors.ToList();
return result.IsValid;
}
public virtual bool CallValidation(string ruleSet)
{
Errors = new List<ValidationFailure>();
ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute;
IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator;
FluentValidation.Results.ValidationResult result = validator.Validate(new FluentValidation.ValidationContext(this, new PropertyChain(), new RulesetValidatorSelector(ruleSet)));
IsValid = result.IsValid;
Errors = result.Errors.ToList();
return result.IsValid;
}
public BaseValidator()
{
Errors = new List<ValidationFailure>();
}
}
我可以CallValidation
用成员调用方法,ruleSet
但它也没有调用“通用”规则。
我知道我可以创建一个“通用”规则集来运行这些规则,但在这种情况下,我必须始终使用通用规则集调用验证。
有什么方法可以调用 RuleSet 并调用通用规则。