开发者

Ninjects with fluentvalidation

开发者 https://www.devze.com 2023-03-04 19:37 出处:网络
I am looking for some help on how to implement the fluentvalidation framework with ninjects as DI framework.

I am looking for some help on how to implement the fluentvalidation framework with ninjects as DI framework.

There is a ninject extension but i can't find documentation on how to use it. Where can you find documentation / tutorial to setup these very nice frameworks?

Vb.net solution

Public Class Dinner

Public Property DinnerID As Guid

Public Property Title As String

Public Property EventDate As DateTime

Public Property Address As String

Public Property HostedBy As String

Public Overridable Property RSVPs As ICollection(Of RSVP)

End Class



Imports FluentValidation

    Public Class dinnervalidator
        Inherits AbstractValidator(Of Dinner)

        Public Sub New()
            RuleFor(Function(x) x.EventDate).NotEmpty().WithMessage("Gelieve een geldige eventdatum op te geven")
            RuleFor(Function(x) x.Address).NotEmpty().WithMessage("Gelieve een adres in te vullen").Length(5, 50).WithMessage("Gelieve een Geldig adres in te vullen aub")
        End Sub
    End Class
开发者_开发知识库
Public Class fluentvalidationmodule
    Inherits NinjectModule

    Public Overrides Sub Load()
        AssemblyScanner.FindValidatorsInAssemblyContaining(Of dinnervalidator) _
            .ForEach(Function(x) Bind(x.InterfaceType).To(x.ValidatorType))

    End Sub

End Class


The readme for the Ninject Fluent Validation module is pretty explicit:

To use follow these steps:

Wire up ASP.NET MVC to use the NinjectValidatorFactory:

NinjectValidatorFactory ninjectValidatorFactory = 
    new NinjectValidatorFactory(ninjectKernel); 
ModelValidatorProviders.Providers.Add(
    new FluentValidationModelValidatorProvider(ninjectValidatorFactory));
DataAnnotationsModelValidatorProvider.
    AddImplicitRequiredAttributeForValueTypes = false;

Add a module to your project that will bind all of your validators:

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


public class FluentValidatorModule : NinjectModule
{
    public override void Load()
    {
        //  NOTE: it should have: <IValidator>()

        AssemblyScanner.FindValidatorsInAssemblyContaining<IValidator>()
            .ForEach(match => Bind(match.InterfaceType).To(match.ValidatorType));
    }
}
0

精彩评论

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

关注公众号