配置 Fluent NHibernate 和 NHibernate 验证器
我正在努力让 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();
}
来自 这个问题,我尝试创建一个如下所示的方法来设置我的验证器:
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
的命名空间时遇到错误NHV配置
。我有项目中引用的 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 愉快地协同工作。希望当人们将来尝试这样做时,这将成为最终的来源!
I am struggling to get Fluent NHibernate and the NHibernate Validator working together and there seems to be a lack of documentation on the internet about the best way to do this. I have found a few websites which detail how to configure the validator and NHibernate but not Fluent NHibernate. I realise that Fluent NHibernate is just NHibernate with nice mappings but I can't quite get my head around the configuration.
This is the code that I use to setup my 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();
}
From this question, I tried to create a method that looked like this to setup my validator:
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);
}
However I got errors trying to find the namespace for SharedValidatorProvider
and NHVConfiguration
. I have the Castle.Core, FluentNHibernate, NHibernate, NHibernate.ByteCode.Castle, NHibernate.Validator and NHibernate.Validator.Specific DLLs referenced in the project and the following using
's:
using NHibernate.Validator.Engine;
using NHibernate.Validator.Event;
using NHibernate.Validator.Cfg;
using NHibernate.Cfg;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
I would really appreciate your help to get Fluent NHibernate and the NHibernate Validator working happily together. Hopefully this will become the definitive source when people try to do this in the future!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在上面的代码中需要
NHibernateSharedEngineProvider
和 NOTSharedValidatorProvider
。要使用NHibernateSharedEngineProvider
,您需要使用以下内容:NHVConfiguration
是您引用的帖子中的变量,而不是类型。我认为您需要更仔细地查看该帖子。也只是一个注释。如果您在查找类型时遇到困难,Visual Studio 中的对象浏览器是您的朋友。它能够搜索项目中包含的所有程序集。
编辑:
下面是我目前正在做的修改,但 NHV 配置流畅:
I think you want
NHibernateSharedEngineProvider
and NOTSharedValidatorProvider
in your code above. To useNHibernateSharedEngineProvider
you need the following using:NHVConfiguration
is a variable in that post that you are referring to and NOT a type. I think you need to look at that post more closely.Also just a note. If you ever have trouble finding a type the Object Browser in visual studio is your friend. It has the ability to search through all the assemblies you have included in your project.
Edit:
Below is what I'm currently doing modified what you want above but NHV is configured fluently instead: