4

我一直在 ASP.NET MVC 3 中的视图模型上使用 FluentValidation,效果非常好!

我现在想将它用作我的服务层内我的域对象的验证引擎。

你能用它做复杂的验证方案吗?

我正在寻找这样的东西:

public class MyService : IMyService
{
    private readonly ISomeOtherService someOtherService;
    public MyService(ISomeOtherService someOtherService)
    {
        this.someOtherService = someOtherService;
    }

    public bool SaveObject()
    {
        var validator = new MyValidator(someOtherService);
        if (!validator.IsValid())
        {
            //spin through the validation results, add them to IValidationDictionary which is a ModelState wrapper

            return false;
        }
    }
}

public class MyValidator : AbstractValidator<MyObject>
{
    private readonly IOtherService service;
    public MyValidator(ISomeOtherService service)
    {
        // Here I want to be able to perform complex validation rules that may involve other services??
    }
}

像这样的东西。我也愿意使用另一个验证库/方案吗?

谢谢!!

4

1 回答 1

2

我通过创建 aValidationService和 a来解决这个问题ValidatorFactory,我认为这是一个非常巧妙的解决方案。

这是上的线程ValidatorFactoryFluentValidation Autofac ValidatorFactory

于 2012-09-04T15:56:38.033 回答