配置流畅的NHibernate和NHibernate验证器 [英] Configuring Fluent NHibernate and NHibernate Validator

查看:235
本文介绍了配置流畅的NHibernate和NHibernate验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难得到流利的NHibernate和NHibernate验证程序一起工作,似乎在互联网上有关最好的方法做这个文件的缺乏。我发现了几个网站,详细说明如何配置验证程序和NHibernate,但不是流利的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 NHVConfiguration 。我有Castle.Core,FluentNHibernate,NHibernate,NHibernate.ByteCode.Castle,NHibernate.Validator和NHibernate.Validator.Specific DLLs在项目中引用和下面的使用' :

 使用NHibernate.Validator.Engine; 
using NHibernate.Validator.Event;
using NHibernate.Validator.Cfg;
使用NHibernate.Cfg;
使用NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

我非常感谢您帮助Fluent NHibernate和NHibernate Validator一起工作。希望这将是人们在未来尝试这样做的最终来源!

解决方案

我想你想要 NHibernateSharedEngineProvider NOT SharedValidatorProvider 。要使用 NHibernateSharedEngineProvider ,您需要使用以下命令:

 使用NHibernate.Validator 。事件; 

NHVConfiguration 您指的是类型。我认为你需要更仔细地观察那个帖子。



也只是一个注释。如果您在查找类型时遇到问题,Visual Studio中的对象浏览器是您的朋友。它可以搜索您的项目中包含的所有程序集。



编辑



下面是我目前正在做的修改你想要的,但NHV被流畅地配置:

  public static void Initialise()
{
//从hibernate.xml.cfg或app.config中读取配置
private static NHibernate.Cfg.Configuration normalConfig = new NHibernate.Cfg.Configuration )。配置();

ConfigureNhibernateValidator(normalConfig);

配置config = Fluently.Configure(normalConfig)
。数据库(MsSqlConfiguration.MsSql2008.ConnectionString(c => c
.Server()
。 Database()
.Username()
.Password()))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf< Map> $ b .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);
}


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!

解决方案

I think you want NHibernateSharedEngineProvider and NOT SharedValidatorProvider in your code above. To use NHibernateSharedEngineProvider you need the following using:

using NHibernate.Validator.Event;

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:

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);
    }

这篇关于配置流畅的NHibernate和NHibernate验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆