配置 Fluent NHibernate 和 NHibernate 验证器

发布于 2024-11-30 19:59:56 字数 2444 浏览 1 评论 0原文

我正在努力让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2024-12-07 19:59:56

我认为您在上面的代码中需要 NHibernateSharedEngineProviderNOT 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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文