Configuring Fluent NHibernate and NHibernate Valid

2019-06-09 12:30发布

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!

1条回答
老娘就宠你
2楼-- · 2019-06-09 12:46

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);
    }
查看更多
登录 后发表回答