加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net – 具有流畅验证的Ninjects

发布时间:2020-12-16 09:22:15 所属栏目:asp.Net 来源:网络整理
导读:我正在寻找一些关于如何使用ninjects作为DI框架实现流畅验证框架的帮助. 有一个ninject扩展,但我找不到如何使用它的文档. 在哪里可以找到设置这些非常好的框架的文档/教程? Vb.net解决方案 Public Class DinnerPublic Property DinnerID As GuidPublic Prop
我正在寻找一些关于如何使用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

解决方法

Ninject Fluent Validation module的 readme非常明确:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读