1
如何使用結構映射和流利的驗證程序集掃描器將驗證程序綁定到nhibernate實體? srFluent驗證和Structuremap AssemblyScanner實現。 ASP.NET MVC3
如何使用結構映射和流利的驗證程序集掃描器將驗證程序綁定到nhibernate實體? srFluent驗證和Structuremap AssemblyScanner實現。 ASP.NET MVC3
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);
});
}
}
請注意指定Singleton時要小心。如果驗證程序調用數據庫,那麼它使用的連接可能會在此期間被處理掉。 –
@ vartan-arabyan,我應該註冊我的所有viewmodels(超過100個在我的項目中)個人嗎?天哪!請說不? –