Ninjects with fluentvalidation
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));
}
}
精彩评论