what would be the best practice for tying validation rules to specific process using http://fluentvalidation.codeplex.com/
Currently I'm using "Rule Sets" feature to group rules to different processes:
public class ObjAValidation: AbstractValidator<A>
{
public ObjAValidation()
{
RuleSet("ProcessA", () =>
{
RuleFor(x => ...);
RuleFor(x => ...);
});
RuleSet("ProcessB", () =>
{
RuleFor(x => ...);
RuleFor(x => ...);
});
}
}
And then validate using:
var a = new A(){...};
IValidator<A> validator = new ObjAValidation();
var result = validator.Validate(a, ruleSet: "ProcessA");
I have two problems with this approach:
- I don't like to use strings as process names. I would like to use a more strongly typed approach. For example to be able to use marker interfaces or attributes.
- In my Unit tests I can't setup the Validate method of IValidator because you can't use optional arguments with Moq.
Mock<IValidator<A>> _mockValidator = new Mock<IValidator<A>>();
_mockValidator.Setup(x => x.Validate(new A(), ruleSet: "ProcessA"));
Second line generates a run time error: An expression tree may not contain a named argument specification. And if you want to pass the ruleSet argument without a named argument to Validate method you have to provide a "IValidatorSelector selector" object. But this interface is not documented.