NHibernate Fluently Configure Default Flush Mode f

2019-08-12 22:20发布

问题:

I am using FluentNHibernate and trying to configure Environment.DefaultFlushMode to FlushMode.Commit

According to this SO question it should be possible: How to change default FlushMode to Commit in C#?

Indeed, this feature was added https://nhibernate.jira.com/browse/NH-3619

Here is how I use it:

    public ISessionFactory BootstrapSessionFactory()
    {
        return Fluently.Configure()
            .ExposeConfiguration(cfg =>
                cfg.SetProperty(Environment.DefaultFlushMode, FlushMode.Commit.ToString()))
            .BuildSessionFactory();
    }

According to source code of FluentConfiguration

    public FluentConfiguration ExposeConfiguration(Action<Configuration> config)
    {
        if (config != null)
        {
            this.configAlterations.Add(config);
        }
        return this;
    }

    public Configuration BuildConfiguration()
    {
        try
        {
            MappingConfiguration mappingConfiguration = new MappingConfiguration(this.logger);
            foreach (Action<MappingConfiguration> mappingsBuilder in this.mappingsBuilders)
            {
                mappingsBuilder(mappingConfiguration);
            }
            mappingConfiguration.Apply(this.Configuration);
            if (this.cache.IsDirty)
            {
                this.Configuration.AddProperties(this.cache.Create());
            }
            foreach (Action<Configuration> configAlteration in this.configAlterations)
            {
                configAlteration(this.Configuration);
            }

I can see that my config alterations are being applied when I debug

However, when I actually check session.FlushMode for a session built using this sessionFactory I'm getting some other FlushMode that I did not set.

What am I missing? What am I doing wrong?

Thanks a lot!

PS: versions of libraries used are:

<PackageReference Include="FluentNHibernate" Version="2.1.2" />
<PackageReference Include="NHibernate" Version="5.1.3" />

Update: Digging a bit deeper, I can see that the code above indeed sets the SessionFactory.DefaultFlushMode correctly, however, this flush mode is not being applied to session

This is how i get ISession and ISessionFactory

    public static void Install(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<PersistenceSetup>();
        // ISessionFactory
        serviceCollection.AddSingleton(provider => provider.GetService<PersistenceSetup>().BootstrapSessionFactory());
        // ISession
        serviceCollection.AddTransient(provider => provider.GetService<ISessionFactory>().OpenSession());

And, as a result Flush modes are different when I debug:

回答1:

I could not understand why your code does not work. As long as your session factory is built successfully and you are creating session from same, it should work.

I am using NHibernate 4.1.0.4000 though; you are using upper version. I do not think this is due to version of NHibernate. I doubt this is due to compatibility issue between Fluent and NHibernate.

This is not directly relevant to your problem but I am using NHibernate Configuration by Code instead of Fulent.

Configuration configuration = new Configuration();
configuration.SessionFactory().DefaultFlushMode(FlushMode.Commit);
sessionFactory = configuration.BuildSessionFactory();

This sets default flush mode to the one I choose. When I create session using this session factory like below -

ISession session = sessionFactory.OpenSession();

this session hold correct ("Commit" in this case) flush mode.

Better solution is to switch to NHibernate's own configuration by code; this will bypass all compatibility issues. Following are the namespaces if you decide to switch:

using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;