2011-05-09 66 views
3

我正在尋找一些幫助,如何實現與ninjects作爲DI框架fluentvalidation框架。流利的驗證Ninject

有一個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 

回答

4

readmeNinject 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)); 
    } 
} 
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)); 
    } 
}