Beispiel #1
0
 public FluentValidationService(
     IValidatorFactory validatorFactory,
     AutofacValidatorFactory autofacValidatorFactory)
 {
     _validatorFactory        = validatorFactory;
     _autofacValidatorFactory = autofacValidatorFactory;
 }
        public static void Start()
        {
            //移除掉mvc自己的验证
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
            //实例化一个验证工厂
            AutofacValidatorFactory autofacValidator = new AutofacValidatorFactory(AutofacConfig.GetConfiguratedBulid());

            //将验证器换成第三方的
            ModelValidatorProviders.Providers.Add(new FluentValidation.Mvc.FluentValidationModelValidatorProvider(autofacValidator));
            //更改mvc默认的元数据提供者
            ModelMetadataProviders.Current = new CustomModelMetadataProvider();
        }
Beispiel #3
0
        public void CreateInstance_ReturnValidator()
        {
            //Arrange
            var validator = new GetCustomerQueryValidator();
            var indexMock = new Mock <IIndex <Type, IValidator> >();

            indexMock.Setup(x => x[It.IsAny <Type>()])
            .Returns(validator);

            //Act
            var factory = new AutofacValidatorFactory(indexMock.Object);
            var result  = factory.CreateInstance(It.IsAny <Type>());

            //Assert
            result.Should().Be(validator);
        }