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>();
    }
}
RiceRiceBaby
  • 1,546
  • 4
  • 16
  • 30

1 Answers1

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

Christian Hagelid
  • 8,275
  • 4
  • 40
  • 63