开发者

DataAnnotations and FluentValidation not working in MVC 2 project

开发者 https://www.devze.com 2023-01-18 20:26 出处:网络
I have edited the original question since the same error is occurring the difference being the implementation, I have now added Ninject to the mix.

I have edited the original question since the same error is occurring the difference being the implementation, I have now added Ninject to the mix.

I have created a class for the validation rules

public class AlbumValidator : AbstractValidator<Album> {
    public AlbumValidator() {
        RuleFor(a => a.Title).NotEmpty();
    }
}

I have created a V开发者_C百科alidatorModule for Ninject

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

Here is my ValidatorFactory

public class NinjectValidatorFactory : ValidatorFactoryBase {
    public override IValidator CreateInstance(Type validatorType) {
        if (validatorType.GetGenericArguments()[0].Namespace.Contains("DynamicProxies")) {
            validatorType = Type.GetType(string.Format("{0}.{1}[[{2}]], {3}",
                validatorType.Namespace,
                validatorType.Name,
                validatorType.GetGenericArguments()[0].BaseType.AssemblyQualifiedName,
                validatorType.Assembly.FullName));
        }

        return Container.Get(validatorType) as IValidator;
    }

    IKernel Container { get; set; }
    public NinjectValidatorFactory(IKernel container) {
        Container = container;
    }
}

and the relevant parts from my Global

protected override void OnApplicationStarted() {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);

        var factory = new NinjectValidatorFactory(Container);

        ModelValidatorProviders.Providers.Add(
            new FluentValidationModelValidatorProvider(factory));

        DataAnnotationsModelValidatorProvider
            .AddImplicitRequiredAttributeForValueTypes = false;
    }

    protected override IKernel CreateKernel() {
        return Container;
    }

    IKernel Container {
        get { return new StandardKernel(new FluentValidatorModule()); }
    }

I load the sample site click on the create new album link and then click the create button leaving the title empty I am then greeted with the error protected override void OnApplicationStarted() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes);

        var factory = new NinjectValidatorFactory(Container);

        ModelValidatorProviders.Providers.Add(
            new FluentValidationModelValidatorProvider(factory));

        DataAnnotationsModelValidatorProvider
            .AddImplicitRequiredAttributeForValueTypes = false;
    }

    protected override IKernel CreateKernel() {
        return Container;
    }

    IKernel Container {
        get { return new StandardKernel(
            new Bootstrapper(),
            new FluentValidatorModule()); }
    }

I load up the create form and click create leaving the title empty low and behold an error

This property cannot be set to a null value.

The line it references is within the Entity Framework auto generated class, I traced the

Namespace.Contains("DynamicProxies")

and it was returning false, is this because I told EF to use a custom namespace SampleMusicStore.Web?

Or am I missing something else?

Cheers.


The problem is that Entity Framework is generating dynamic proxies on your classes, and then your system is trying to validate against the proxy classes instead of the classes you defined.

The way to resolve this is the same as this answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号