SVN Repository available

I’ve made availble a svn repository with the source code of the channel library for Spring.Net and my work on a Cuyahoga module for events. The last one is still pretty much a work in progress. I think I’ll be able to work on it some more later this year. There’s a lot on my todo list.

Readers have reported that the latest version of Fluent NHibernate breaks the approach which I illustrated on this blog, I’ll look into that later today. I’ll also be patching the channel library with a bug fix which someone on the Spring.Net forum was kind enough to provide. Had hoped to do that yesterday but the machine on which I was working did not have NUnit installed and Sourceforge was down. So stay tuned, this blog will get some more action in August.

Repository is located at:

Edit: Moved code to github @ https://github.com/BennyM/SpringWCFChannelManager

Mapping a list of components with Fluent NHibernate

This took me some time to figure out since documentation for Fluent NHibernate is still a little scarce. Let’s say we have an entity “ActivityType” which has an icon and a name, but the name is stored for each language so we have a one to many relationship. This relationship is not only modelled in the entity “ActivityType” but also in several others. The translation is modelled in the “LocalizedEntity” class. As illustrated in the class diagram below:

The DB is illustrated in the next diagram, to make it easy the tables have a different name compared to the entity diagram :p.

The mapping for the “Language” entity is pretty straightforward. The mapping for the “ActivityType” and its localizations was a bigger challenge since I got lost in all the delegates after the Component method.

public class LanuageMapping
: ClassMap<Language>
{
	public LanuageMapping()
	{
		Id(x => x.Id);
		Map(x => x.Name);
		WithTable("[7de_Languages]");
	}
}
 
public class ActivityTypeMapping
: ClassMap<ActivityType>
{
	public ActivityTypeMapping()
	{
		WithTable("[7de_EventTypes]");
		Id(activityType => activityType.Id);
		Map(activityType => activityType.IconName);
		HasMany<LocalizedEntity>(activityType => activityType.Descriptions)
		.WithTableName("[7de_LocalizedEventTypes]")
		.Component(description => 
				   {
				   description.References<Language>(desc => desc.Language)
				   .CanNotBeNull();
				   description.Map(desc => desc.Localization)
				   .CanNotBeNull();
				   });
	}
}

Using Fluent NHibernate in Spring.Net

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.