Using Fluent NHibernate in Spring.Net

by Benny 4. January 2009 16:09

In order to load the mappings you've written using Fluent NHibernate you need to call the extension method "AddMappingsFromAssembly" on the configuration. The "LocalSessionFactoryObject" defined in Spring.Net supports out of the box the loading of .hbm files from an assembly or a location in the file system. Luckily this class can be extended with ease. The code below is all you need to use Fluent NHibernate with Spring.Net, any suggestions for a better name are welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using FluentNHibernate;
using System.Reflection;
using NHibernate.Cfg;

namespace SessionFactories
{
    public class FluentNhibernateLocalSessionFactoryObject
        : LocalSessionFactoryObject
    {
        /// <summary>
        /// Sets the assemblies to load that contain fluent nhibernate mappings.
        /// </summary>
        /// <value>The mapping assemblies.</value>
        public string[] FluentNhibernateMappingAssemblies
        {
            get;
            set;
        }

        protected override void PostProcessConfiguration(Configuration config)
        {
            base.PostProcessConfiguration(config);
            if(FluentNhibernateMappingAssemblies != null)
            {
                foreach(string assemblyName in FluentNhibernateMappingAssemblies)
                {
                    config.AddMappingsFromAssembly(Assembly.Load(assemblyName));
                }
            }
        }
    }
}

Update 29 June: please check the comments since a new version brought some changes to this blog post.

Tags: ,

Software Development | Spring.Net

Comments

2/11/2009 10:32:18 PM #

SLMP30123

Could you add a configuration within the spring xml file?

SLMP30123

2/17/2009 8:45:14 PM #

Benny Michielsen

What exactly do you mean by that?

Benny Michielsen Belgium

5/1/2009 6:00:53 PM #

Bruno Kmita

I guess what he meant was, what should you add to spring's configuration file, in order to use this class.

Bruno Kmita Brazil

6/27/2009 9:58:12 PM #

David Lieberman

Updated to the most current version of Fluent and this no longer works. Arg.

David Lieberman United States

6/28/2009 3:20:47 PM #

David Lieberman

It seems we now need to do Fluent configuration to get this to work:

protected override void PostProcessConfiguration(Configuration config)
   {
       base.PostProcessConfiguration(config);

       Fluently.Configure(config)
           .Database(MySQLConfiguration.Standard.ConnectionString(
               c => c.FromConnectionStringWithKey("connectionString")))
           .Mappings( m => m.FluentMappings
                .AddFromAssemblyOf<FluentNHibernateLocalSessionFactoryObject>())
                .BuildSessionFactory();
        }

Note that the connection string now needs to exposed in the standard .NET way via app.config -- it doesn't seem to be picked up from the Spring.NET NHibernate config XML block.

David Lieberman United States

6/29/2009 7:02:53 PM #

Benny Michielsen

Thanks for the update.

You can use the regular .Net way (connectionstrings section) and Spring.Net together. So it's not a big problem.

Benny Michielsen Belgium

7/1/2009 5:43:58 PM #

David Lieberman

Actually, it turns out this didn't quite solve the problem for me. I use Spring.NET transaction attributes in my services, and using Fluently.Configure in the LocalSessionFactoryObject.PostProcessConfiguration appears to break this -- I'm not getting back Spring's ISession implementation (I think), so the transaction attributes aren't being observed.

Benny, do you have a solution that works with the most current Fluent update? I found that with the newest version of Fluent, no mappings were loading when I invoked

config.AddMappingsFromAssembly(Assembly.Load(assemblyName));

which is why I went to Fluently.Configure in the first place

David Lieberman United States

7/2/2009 6:26:57 AM #

Benny Michielsen

I'll look into it, but it'll be something for next week. Won't have a computer the following days.

Benny Michielsen Belgium

7/13/2009 4:46:08 PM #

David Lieberman

Thanks, Benny! I'll be interested to see what you come up with.

David Lieberman United States

8/10/2009 3:27:29 PM #

liang

protected override ISessionFactory NewSessionFactory(Configuration config)
        {
            config = Fluently.Configure(config)
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<FluentSessionFactoryObject>())
                .BuildConfiguration();
            return base.NewSessionFactory(config);
        }

liang People's Republic of China

8/14/2009 9:03:42 PM #

Rodrigo Longo

I'm working with HibernateDaoSupport and I can't figure out how to configure Fluent in order to make it work. Anybody could help me?

Rodrigo Longo Argentina

1/20/2010 10:45:22 AM #

robsosno

After several trials and experiments I've got slightly improved version: assemblies and properties comes (like show_sql) from Spring context.
The only hardcoded part remains Database section.

    public class TestNhibernateLocalSessionFactoryObject
        : LocalSessionFactoryObject
    {
        private string[] fluentNhibernateMappingAssemblies;
        /// <summary>
        /// Sets the assemblies to load that contain fluent nhibernate mappings.
        /// </summary>
        /// <value>The mapping assemblies.</value>
        public string[] FluentNhibernateMappingAssemblies
        {
            get { return fluentNhibernateMappingAssemblies; }
            set { fluentNhibernateMappingAssemblies = value; }
        }

        /// <summary>
        /// Fluent configuration.
        /// </summary>
        /// <param name="config"></param>
        protected override void PostProcessConfiguration(Configuration config)
        {
            IDictionary<string, string> properties;

            base.PostProcessConfiguration(config);

            properties = config.Properties;

            Fluently.Configure(config)
               .Database(
                   SQLiteConfiguration
                   .Standard
                   .ProxyFactoryFactory(properties["proxyfactory.factory_class"])
                   .InMemory
               )
               .Mappings(m =>
               {
                   foreach (string assemblyName in fluentNhibernateMappingAssemblies)
                   {
                       m.HbmMappings
                            .AddFromAssembly(Assembly.Load(assemblyName));

                       m.FluentMappings
                            .AddFromAssembly(Assembly.Load(assemblyName));
                   }

                   //.ExportTo(@"c:\ala");
               })
               .ExposeConfiguration(x =>
               {
                   foreach (var item in x.Properties)
                   {
                       if (properties.ContainsKey(item.Key))
                       {
                           properties[item.Key] = item.Value;
                       }
                       else
                       {
                           properties.Add(item);
                       }
                   }
                   x.SetProperties(properties);
               })
               .BuildConfiguration();
        }
    }

I don't know why but foreach assemblyName works with field but not with a property.
Also in my case I have both fluent and xml mappings in the same assembly. If this doesn't work in other cases then you need to define two separate properties.

robsosno Poland

1/21/2010 9:14:12 AM #

James

great post.
Thanks frenz.
always posting!

James Estonia

2/7/2010 3:25:39 AM #

lemonhan

Could you add a configuration within the spring xml file
in your blog?

lemonhan People's Republic of China

3/1/2010 6:51:24 AM #

toner panasonic

very nice post Smile

toner panasonic Indonesia

3/1/2010 9:39:16 PM #

birth records

Good stuff on Using Fluent NHibernate in Spring.Net. I even agree with most of it!

birth records United Kingdom

3/2/2010 11:49:49 AM #

order viagra tablets

Instead of being shackled by the prohibitive prospects of patronising the local pharmacies, there are cheaper options to buying Viagra pills.

order viagra tablets United Kingdom

3/4/2010 9:01:52 AM #

alat kantor

very nice post Smile

alat kantor Indonesia

3/4/2010 9:03:01 PM #

robsosno

After further testing I've found that original Benny code almost works. The main change I've made is related to loading assemblies (changed API):

    public class TestNhibernateLocalSessionFactoryObject
        : LocalSessionFactoryObject
    {
        private string[] fluentNhibernateMappingAssemblies;
        /// <summary>
        /// Sets the assemblies to load that contain fluent nhibernate mappings.
        /// </summary>
        /// <value>The mapping assemblies.</value>
        public string[] FluentNhibernateMappingAssemblies
        {
            get { return fluentNhibernateMappingAssemblies; }
            set { fluentNhibernateMappingAssemblies = value; }
        }

        /// <summary>
        /// Fluent configuration.
        /// </summary>
        /// <param name="config"></param>
        protected override void PostProcessConfiguration(Configuration config)
        {
            base.PostProcessConfiguration(config);

            Fluently.Configure(config)
               .Mappings(m =>
               {
                   foreach (string assemblyName in fluentNhibernateMappingAssemblies)
                   {
                       m.HbmMappings
                            .AddFromAssembly(Assembly.Load(assemblyName));

                       m.FluentMappings
                            .AddFromAssembly(Assembly.Load(assemblyName));
                   }
               })
               .BuildConfiguration();
        }
    }

It just works for me. I've used FNH build 1.0.0.623.

robsosno Poland

3/7/2010 9:53:30 AM #

pabx panasonic

very nice info, thank's Smile

pabx panasonic Indonesia

3/9/2010 1:12:23 AM #

Vedezevanje

Useful and nice information. I am going to subscribe your blog. Thnx.

Vedezevanje Slovenia

3/9/2010 6:49:22 PM #

seo tools

Nice to be stumbling up to your site again, it has been weeks for me. Anyway, this is the article that i've been waiting for so long. I need this article to complete my paper in the school, and it has same topic as your article. Thanks, excellent share.

seo tools Slovenia

3/10/2010 8:32:46 PM #

how to find foreclosures

I hapen to agree with Rick above.  I will look the information and post it here.  We'll have the FACTS momentarily.

how to find foreclosures United States

3/15/2010 1:27:46 AM #

Charlie

Love your nhibernate blog I'm going to subscribe
Regards

Charlie Italy

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

Recent Posts

Page List

Recent Comments

Comment RSS