Question

I have been using FluentValidation on my View Models in ASP.NET MVC 3 and it works awesome!

I want to now use it as my validation engine for my domain objects inside my service layer.

Can you do complex validation schemes with it?

I am looking for something like this:

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??
    }
}

Something like this. Also I am open to using another validation lib/scheme?

Thanks!!

Was it helpful?

Solution

I solved this by creating a ValidationService combined with a ValidatorFactory, it is a pretty slick solution I think.

Here is the thread on the ValidatorFactory: FluentValidation Autofac ValidatorFactory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top