Table-Per-Class inheritance with Fluent NHibernate

by Mikael Henriksson 23. July 2009 08:59

First of all I am an Entity Framework dude. Don’t worry I’m not going to convert BUT my boss and friend has a history of being an expert DBA and those guys have their own take own naming columns and at a recent project we were working on I realized it was going to be way to much work customizing the EF model every time I needed to regenerate it and the only thing I could think of was fluent nhibernate(besides T4 template maybe but imagine the work to have T4 do the whole generation for you).

I of course instantly joined the mailing list which is one of the best mailing lists I’ve ever joined. I even managed to help a guy once but that’s not why I am writing. My first try on Fluent was not a success, my second try was semi successful. Then I have the third try in a bogus project I am doing just for fun. Second time around with all the funny column names with under_scores etc I had no problems mapping to an existing database but when I try to do the same at home for my bogus project it fails…. LOL! No exceptions nothing even happens. I created a unit test to setup a sqlite db and nothing. So I switch over from my fluent mappings to AutoMappings and voila there is the Db with inheritance etc. My take on that is that I actually created a pretty good class structure but it took some time to figure out the Fluent Part. There is a lot of information about how to map fluently but hardly any information about how inheritance is supposed to work and reading the NHibernate documentation about sub-classing doesn't help much either. Time to ask the mailing list. It’s mostly James that answers and he answers most times of the day. I got a reply at 7 something in the morning!! With version 1 only a few weeks away this might be old news by then but since I could not find any information about JoinedSubClass I thought I’d post it still. There is a new feature called SubClassMap<T> coming for FNH 1.0 but that is pretty far away from NHibernate-JoinedSubClass-XmlMapping so I thought I’d post it here for future reference.

person

As you can see Contact inherits from Person. The reason is that I don’t want users to have to type in the names of people who organizes or helps out with tournaments and I really don’t want them to have to fill out contact information for persons who is just organizing and helping out and if I ever have a look in the person table I will go crazy if I see a lot of empty information for contact_address_street_one and so on and so forth.

internal class PersonMap : ClassMap<Person>
{
	public PersonMap()
	{
		WithTable("persons");

		Id(x => x.Id, "person_id");
		Map(x => x.FirstName, "person_first_name");
		Map(x => x.LastName, "person_last_name");
		Map(x => x.CreatedAt, "created_at");

		JoinedSubClass<Contact>("person_id", MapContact);
	}

	private static void MapContact(JoinedSubClassPart<Contact> p)
	{
		p.WithTableName("contacts");
		p.Map(x => x.Email, "contact_email");
		p.Map(x => x.Phone, "contact_phone");
		p.Map(x => x.Mobile, "contact_mobile");

		p.Component(x => x.Address, 
		c =>
			{
				c.Map(x => x.StreetOne, "address_street_one");
				c.Map(x => x.StreetTwo, "address_street_two");
				c.Map(x => x.Zip, "address_zip");
				c.Map(x => x.City, "address_city");
				c.Map(x => x.Country, "address_country");
			});

		p.References(x => x.ContactType)
			.LazyLoad()
			.Cascade.All()
			.ColumnName("contact_type_id");
	}
}

That should on SchemaExport generate a table-per-subclass type of schema. In the future it will get even easier. Fluent is becoming an intuitive interface towards NHibernate. Gone is the need for XML mappings. If I am not mistaken NHibernate will become something totally different in the future everything is becoming Fluent even the configuration. What I miss in the Entity Framework is there with Fluent but then again I miss some things from Entity Framework when I work on NHibernate. I guess they will never really compete on the same level. And also things are changing more rapidly for NHibernate since they release more often :) It can however not hurt to learn both!

Tags:

Fluent NHibernate

blog comments powered by Disqus

About the author

Life architect specialized in programming

Month List

Widget Twitter not found.

Root element is missing.X