我正在努力让Fluent NHibernate和NHibernate Validator一起工作,并且互联网上似乎缺乏关于最佳方法的文档。我找到了一些网站,详细介绍了如何配置验证器和NHibernate但不是Fluent NHibernate。我意识到Fluent NHibernate只是NHibernate,具有很好的映射,但我不能完全理解配置。
这是我用来设置SessionFactory
的代码:
public static void Initialise()
{
Configuration config = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c
.Server("")
.Database("")
.Username("")
.Password("")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Map>()
.Conventions.AddFromAssemblyOf<EnumConvention>())
.BuildConfiguration();
DataBindingInterceptor interceptor = new DataBindingInterceptor();
SessionFactory = config
.SetInterceptor(interceptor)
.BuildSessionFactory();
}
从this question开始,我尝试创建一个类似于设置验证器的方法:
private static void ConfigureValidator(Configuration config)
{
NHibernate.Validator.Cfg.Environment.SharedEngineProvider = new SharedValidatorProvider();
var nhvc = new NHVConfiguration();
nhvc.Properties[NHibernate.Validator.Cfg.Environment.ApplyToDDL] = "true";
nhvc.Properties[NHibernate.Validator.Cfg.Environment.AutoregisterListeners] = "true";
nhvc.Properties[NHibernate.Validator.Cfg.Environment.ValidatorMode] = "UseAttribute";
var ve = new ValidatorEngine();
ve.Configure(nhvc);
ValidatorInitializer.Initialize(config, ve);
}
但是,我在尝试查找SharedValidatorProvider
和NHVConfiguration
的命名空间时遇到错误。我有项目中引用的Castle.Core,FluentNHibernate,NHibernate,NHibernate.ByteCode.Castle,NHibernate.Validator和NHibernate.Validator.Specific DLL以及以下using
:
using NHibernate.Validator.Engine;
using NHibernate.Validator.Event;
using NHibernate.Validator.Cfg;
using NHibernate.Cfg;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
我非常感谢你帮助Fluent NHibernate和NHibernate Validator一起愉快地工作。希望这将成为人们在将来尝试这样做时的权威来源!
答案 0 :(得分:2)
我认为您在上面的代码中需要NHibernateSharedEngineProvider
和不 SharedValidatorProvider
。要使用NHibernateSharedEngineProvider
,您需要使用以下内容:
using NHibernate.Validator.Event;
NHVConfiguration
是您所指的帖子中的变量,而非是一种类型。我认为你需要更仔细地看一下那篇文章。
也只是一张纸条。如果您在查找类型时遇到问题,Visual Studio中的对象浏览器就是您的朋友。它能够搜索您项目中包含的所有程序集。
修改:
以下是我目前正在修改您想要的内容,但NHV配置得很流利:
public static void Initialise()
{
//Read the configuration from hibernate.xml.cfg or app.config
private static NHibernate.Cfg.Configuration normalConfig = new NHibernate.Cfg.Configuration().Configure();
ConfigureNhibernateValidator(normalConfig);
Configuration config = Fluently.Configure(normalConfig)
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c
.Server("")
.Database("")
.Username("")
.Password("")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Map>()
.Conventions.AddFromAssemblyOf<EnumConvention>())
.BuildConfiguration();
DataBindingInterceptor interceptor = new DataBindingInterceptor();
SessionFactory = config
.SetInterceptor(interceptor)
.BuildSessionFactory();
}
private static void ConfigureNhibernateValidator(NHibernate.Cfg.Configuration config)
{
var provider = new NHibernateSharedEngineProvider();
NHibernate.Validator.Cfg.Environment.SharedEngineProvider = provider;
var nhvConfiguration = new NHibernate.Validator.Cfg.Loquacious.FluentConfiguration();
nhvConfiguration
.SetDefaultValidatorMode(ValidatorMode.UseAttribute)
.Register(Assembly.Load("Namespace.To.Business.Objects")
.ValidationDefinitions())
.IntegrateWithNHibernate
.ApplyingDDLConstraints()
.RegisteringListeners();
ValidatorEngine validatorEngine = NHibernate.Validator.Cfg.Environment.SharedEngineProvider.GetEngine();
validatorEngine.Configure(nhvConfiguration);
ValidatorInitializer.Initialize(config, validatorEngine);
}