Fluent验证和Structuremap AssemblyScanner实现。 ASP.NET MVC3

时间:2011-08-11 23:07:22

标签: c# asp.net-mvc fluent-nhibernate structuremap fluentvalidation

如何使用structuremap和fluent验证程序集扫描程序将验证程序绑定到nhibernate实体? SR

1 个答案:

答案 0 :(得分:2)

ObjectFactory.Configure(cfg => cfg.AddRegistry(new MyRegistry()));
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

//Configure FV to use StructureMap
var factory = new StructureMapValidatorFactory();

//Tell MVC to use FV for validation
ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(factory));
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

public class StructureMapValidatorFactory : ValidatorFactoryBase
{
public override IValidator CreateInstance(Type validatorType)
{
    return ObjectFactory.TryGetInstance(validatorType) as IValidator;
}
}


public class MyRegistry : Registry
{
public MyRegistry()
{
    AssemblyScanner.FindValidatorsInAssemblyContaining<LiveReport.Domain.Validation.PersonValidator>()
      .ForEach(result =>
      {
          For(result.InterfaceType)
             .Singleton()
             .Use(result.ValidatorType);
      });

}
}