Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

IModelValidatorFactory is registered twice into DI container when using FluentValidation #1243

Closed
ghost opened this issue Sep 27, 2013 · 4 comments
Milestone

Comments

@ghost
Copy link

ghost commented Sep 27, 2013

NancyBootstrapperBase registers two IModelValidatorFactory instances when Nancy.Validation.FluentValidation NuGet package is being used. Having two factories causes the validation errors to appear twice.

Original description of this issue with code examples available here:
https://gist.github.com/mattilaj/24732c8353065f9bcfc5

A minimal reproducible is available for download here:
https://skydrive.live.com/redir?resid=D6C0FB440C48EDBB!530&authkey=!AKy9iTzYKff25DE

@xt0rted
Copy link
Contributor

xt0rted commented Sep 29, 2013

I'm seeing this issue as well but I'm using Data Annotations Validation and StructureMap. When I switch back to TinyIoC I don't have the problem.

Either of these overrides in my bootstrapper seems to fix the problem.

protected override IEnumerable<IApplicationRegistrations> GetApplicationRegistrationTasks()
{
    return ApplicationContainer.GetAllInstances<IApplicationRegistrations>()
                                .Where(t => t.GetType() != typeof (Nancy.Validation.DataAnnotations.ApplicationRegistrations));
}

protected override void RegisterTypes(IContainer container, IEnumerable<TypeRegistration> typeRegistrations)
{
    container.Configure(registry =>
    {
        foreach (var typeRegistration in typeRegistrations)
        {
            if (typeRegistration.ImplementationType == typeof (DataAnnotationsValidator) ||
                typeRegistration.ImplementationType == typeof (DataAnnotationsValidatorFactory))
            {
                continue;
            }

            registry.For(typeRegistration.RegistrationType)
                    .LifecycleIs(InstanceScope.Singleton)
                    .Use(typeRegistration.ImplementationType);
        }
    });
}

@grumpydev
Copy link
Member

@thecodejunkie is this fixed by #1267 ?

@maniserowicz
Copy link

This behavior is still here in v0.21.1.0, I'm using Autofac and Fluent. Needed to write a similar workaround:

protected override void RegisterTypes(ILifetimeScope container, IEnumerable<TypeRegistration> typeRegistrations)
{
    base.RegisterTypes(container, typeRegistrations.Where(
        // this factory is registered as TypeRegistration and CollectionTypeRegistration for some reason
        // which results in validators being used twice each time, generating two error messages
        // ignoring this component here solves the problem
        x => x.ImplementationType != typeof(FluentValidationValidatorFactory))
    );
}

@thecodejunkie
Copy link
Member

It's being fixed for 0.22, as part of the Model Validation Overhaul #1265 and more specifically by #1267 which has been pulled into master

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants