Upgrade to NHibernate 3.0 and FluentNHibernate 1.2.0

Lees voor met webReader

We had some problems loading multiple assemblies when we upgraded from NHibernate2.1.2 to NHibernate3.0.

Here is the setup of loading all mapping files from all the assemblies that have mappingfiles, that we succesfully used for NH2.1.2.

 C# |  copy code |? 
01
02
private static FluentConfiguration CreateNHibernateConfiguration()
03
{
04
    // reads settings from config
05
    Configuration nHibernateConfiguration = new NHibernate.Cfg.Configuration();
06
    // configure default NHibernate
07
    nHibernateConfiguration.Configure(); 
08
    // pass the default configuration into FluentNHibernate
09
    FluentConfiguration configuration = Fluently.Configure(nHibernateConfiguration);  
10
 
11
    string[] assemblies = {
12
        "My.AssemblyA", 
13
        "My.AssemblyB", 
14
        "My.AssemblyC", 
15
         // etc, cut for brevity; all assemblies containing 
16
         // either *.hbm.xml mappings and/or Fluent mappings
17
    };
18
 
19
    foreach (string name in assemblies)
20
    {
21
        Assembly assembly = Assembly.Load(name);
22
        // combination of normal and fluent mappings:
23
        configuration.Mappings(m =>
24
        {
25
            m.FluentMappings.AddFromAssembly(assembly);// .ExportTo(@"D:\EXPORT\");
26
            m.HbmMappings.AddFromAssembly(assembly);
27
        });
28
        return configuration;
29
    }
30
}
31

Unfortunately, the above code does NOT work when we upgraded to NHibernate 3.0 with its accompanying FluentNHibernate release. After some investigation it turned out that within the loop of the assemblies, only the classmaps of the last assembly remained in the configuration.

We had to change the above piece of code to the following to have it work for FluentNHibernate:

 C# |  copy code |? 
01
02
private static FluentConfiguration CreateNHibernateConfiguration()
03
{
04
    // reads settings from config
05
    Configuration nHibernateConfiguration = new NHibernate.Cfg.Configuration();
06
    // configure default NHibernate
07
    nHibernateConfiguration.Configure(); 
08
    // pass the default configuration into FluentNHibernate
09
    FluentConfiguration configuration = Fluently.Configure(nHibernateConfiguration);  
10
 
11
    string[] assemblies = {
12
        "My.AssemblyA", 
13
        "My.AssemblyB", 
14
        "My.AssemblyC", 
15
         // etc, cut for brevity; all assemblies containing 
16
         // either *.hbm.xml mappings and/or Fluent mappings
17
    };
18
    // here is the difference from previous example:
19
    configuration.Mappings(m => {
20
        foreach (string name in assemblies)
21
        {
22
	 Assembly assembly = Assembly.Load(name);
23
	 // combination of normal and fluent mappings:
24
            m.FluentMappings.AddFromAssembly(assembly);// .ExportTo(@"D:\EXPORT\");
25
            m.HbmMappings.AddFromAssembly(assembly);
26
        }
27
       }).
28
    return configuration;
29
}

Note; this is example code, some try..catch..log/debug.writeline blocks have been removed for readability.

As you can see, we had to get the lambda expression at the top and thus -around- the assemblyloop instead of inside the loop. This was probably needed because of the internal changes within the FluentNhibernate.AddFromAssembly code, but we’re not entirely sure.