2

Do I need to add "AssemblyScanner.FindValidatorsInAssemblyContaining" for every object validator I create? Is there a simpler way?

public class FluentValidatorModule : NinjectModule
{
    public override void Load()
    {
        AssemblyScanner.FindValidatorsInAssemblyContaining<OfficeModelValidator>()
            .ForEach(match => Bind(match.InterfaceType).To(match.ValidatorType));
    }
}

Also, when I perform submit, the CreateInstance method in my NinjectValidatorFactory keeps getting called multiple times during one postback. Why is that happening?

public class NinjectValidatorFactory : ValidatorFactoryBase
{
    private IKernel _kernel;

    public NinjectValidatorFactory(IKernel kernel)
    {
        _kernel = kernel;
        AddBindings();
    }

    public override IValidator CreateInstance(Type validatorType)
    {
        var bindings = (IEnumerable<IBinding>)_kernel.GetBindings(validatorType);
        return bindings.Count() > 0 ? _kernel.Get(validatorType) as IValidator : null;
    }

    private void AddBindings()
    {
        _kernel.Bind<ILookupService>().To<LookupService>();
    }
}

1 Answer 1

1

The purpose of the AssemblyScanner is to scan through the assembly of the passed in type, allowing you to autobind all of them. In your example, OfficeModelValidator is just used to find the assembly where all your validators are defined.

refer to the source: https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation/AssemblyScanner.cs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.