我正在寻找一些关于如何使用ninjects作为DI框架实现流畅验证框架的帮助。
有一个ninject扩展,但我找不到有关如何使用它的文档。 在哪里可以找到设置这些非常好的框架的文档/教程?
Vb.net解决方案
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
答案 0 :(得分:4)
readme的Ninject Fluent Validation module非常明确:
要使用,请按照以下步骤操作:
连接ASP.NET MVC以使用 NinjectValidatorFactory:
NinjectValidatorFactory ninjectValidatorFactory =
new NinjectValidatorFactory(ninjectKernel);
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(ninjectValidatorFactory));
DataAnnotationsModelValidatorProvider.
AddImplicitRequiredAttributeForValueTypes = false;
将一个模块添加到您的项目中 绑定所有验证器:
public class FluentValidatorModule : NinjectModule {
public override void Load() {
AssemblyScanner.FindValidatorsInAssemblyContaining().ForEach(
match => Bind(match.InterfaceType).To(match.ValidatorType));
}
}
答案 1 :(得分:3)
public class FluentValidatorModule : NinjectModule
{
public override void Load()
{
// NOTE: it should have: <IValidator>()
AssemblyScanner.FindValidatorsInAssemblyContaining<IValidator>()
.ForEach(match => Bind(match.InterfaceType).To(match.ValidatorType));
}
}