【问题标题】:FluentValidation in a service layer?服务层中的 FluentValidation?
【发布时间】:2012-08-25 19:48:26
【问题描述】:

我一直在 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??
    }
}

类似的东西。我也愿意使用另一个验证库/方案?

谢谢!!

【问题讨论】:

    标签: validation fluentvalidation service-layer


    【解决方案1】:

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

    这是ValidatorFactory上的线程:FluentValidation Autofac ValidatorFactory

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2012-05-11
      • 2014-02-05
      • 2017-06-07
      • 2013-05-16
      • 2014-02-13
      • 2023-01-03
      相关资源
      最近更新 更多