For the next rev of dashCommerce, we are going to be using NHibernate and so I have been exploring ways to both: 1.) get up to speed with NHibernate and 2.) the fastest way to get started working with NHibernate. I had seen all of the articles on Fluent NHibernate, but to me it just looked like a C# version of Xml. That’s not a ding against, it, but when you have system with about 50 tables in it, well . . . typing is typing.
But, one thing that caught my eye when I started looking around the Fluent NHibernate project was the AutoMapping feature. This feature will allow you to set up some conventions for use against your database and it will attempt to generate the mapping files for you. That’s pretty slick.
But would it work well enough to actually use since Fluent NHibernate is still pretty young?
Well, it worked well enough for me.
So, here is what I had to do to pull it off against the dashCommerce database.
When I started trying to get the mappings to work against a bunch of dashCommerce tables, I was getting some errors about not being able to map certain entities. So, as soon as I saw that, and recognized that the exception wasn’t going to help me identify what, exactly, the problem was I dug into the code and ended up changing this very simple line in the ClassMap file (public void ApplyMappings(IMappingVisitor visitor)) around line 195: Debug.WriteLine(writer.ToString()); to Console.WriteLine(writer.ToString()); I was using a small console client with some sample queries in it, so this was best for me at the time. But, once I did that, I was able to quickly look at that output and determine the cause of my pains.
To be exact, they were all related to a few key things:
- The AutoMap functionality relies on conventions to determine the structure of your database tables.
- I had to update the AutoMap conventions with the conventions used by the dashCommerce database.
- The dashCommerce database was not entirely consistent in its naming conventions. (gufaw!)
This last one is the one that really appealed to me as I know I put a lot of work into being consistent in the way the dashCommerce database is constructed, but it just showed me where I had missed things. This is great, and showed me the beauty of the AutoMap functionality. And it will be helpful in generating an upgrade script for dashCommerce going forward to ensure the database conventions are consistently applied.
So, how much code did it take to get the AutoMapping functionality working with dashCommerce? Not much. It’s not at 100% coverage yet, but we are in pretty good shape.
One other interesting thing about the AutoMapping in combination with dashCommerce is that in the dC database, we kind of namespace things out so there is a correlation between the assemblies and the database. For instance, we have the following naming conventions: dashCommerce _Core_tableName, dashCommerce_Store_tableName, and dashCommerce_Content_tableName. I’m still hmm’ing and haw’ing over whether or not I really like that anymore, but it does make things very clear for the developer that is customizing dashCommerce. That means it made the conventions less flexible, so I had to accommodate that in the code by creating separate files for each mapping. So, for instance, in getting the Core up and running, the code for Fluent NHibernate AutoMap was as simple* as this:
var configuration = MsSqlConfiguration.MsSql2005 .ConnectionString(connectionString => connectionString .Server("MYSERVER") .Database("DASHCOMMERCE") .Username("USER") .Password("PASSWORD")) .UseReflectionOptimizer() .ConfigureProperties(new Configuration()); var autoPersistenceModel = AutoPersistenceModel .MapEntitiesFromAssemblyOf<Log>() .Where(type => type.IsClass && !type.IsAbstract && type.Namespace == "Core.Entities") .WithConvention(convention => { convention.FindIdentity = p => p.Name == p.DeclaringType.Name + "Id"; convention.GetTableName = type => "dashCommerce_Core_" + type.Name; convention.GetPrimaryKeyNameFromType = type => type.Name + "Id"; convention.GetForeignKeyNameOfParent = prop => prop.Name + "Id"; }) ; autoPersistenceModel.Configure(configuration);
*simple took me a couple of hours, but that was digging into Fluent NHibernate for the first time and trying to grok and learn all at once.
There is still some work to do on Many to Many relationships, but this was darned impressive and a huge boost in productivity for dashCommerce and NHibernate. Thank you to the folks that have created Fluent NHibernate, this makes working with NHibernate easier than any other ORM / code generation tool out there.
One other side note worth mentioning is that once you have the AutoMap set up correctly, you can have Fluent NHibernate spit out the mapping files so that you can use those directly (which is what we are doing for dashCommerce).
March 17, 2009 at 7:35 pm
Chris,
Can you provide some information on why you plan to move to nhibernate? What is wrong with the current architecture and what is driving the change?
Thanks,
March 27, 2009 at 2:58 pm
Hello,
What is the reasoning behind changing from SubSonic to Fluent NHibernate?
Is it faster? Does it generate DAL classes etc like SubSonic?
Thank you
March 27, 2009 at 4:44 pm
NHibernate is persistent ignorant. The community is growing hugely. It has support for more databases. It does not take a database up approach, which loosens what we can do with the objects. There are other technical reasons, but these are the highlights.
April 1, 2009 at 2:51 am
Hi Chris,
Please, before begining with NHibernate, please, take a look at this persistent framework: http://www.codeplex.com/euss.
It’s a great framework, very complet, free, open source, very easy to use, robust, …
I’m using it in my comercial projects and works very fine.
Please, just take a look before apply NHibernate.
Thank you for your work.
Albert Arranz
April 2, 2009 at 5:40 pm
Will do.
April 17, 2009 at 8:44 pm
I am really looking forward to the nhibernate version of dashCommerce. I was looking into nhibernate before looking into more specifically purposed application/frameworks (and bumping into dashCommerce), and one of the first things I thought when I looked at the dashCommerce source was “Imagine if this was done using nHibernate!”
May 27, 2009 at 11:20 am
Chris,
Good luck with NHibernate. I have been using it for just under a year now and believe me it took quite a while to get to grips with the best way to achieve things but I have to say, I’m loving NHibernate! I do use Fluent NHibernate but the only draw back is that it doesn’t support stored procs.
K
May 31, 2009 at 2:49 pm
I have a question becuase I have implemented a virtual webservice http handler for the webpartmanager to provide more ajax functionality. this works fine in the devenv however when hosted on IIS 7 with a classic application pool. this part tanks. I change the application pool from classic to integrated then subsonic tanks on a Utility.GetSiteRoot() with an error on the context not being available for get_Request(). is this one on the reasons you are looking for another DAL for dash commerce?
June 17, 2009 at 2:38 pm
Hi Chris, I read this article with interest as I’ve got a couple of sites using subsonic and the main reason I started with subsonic was the auto-generation of the classes from the db tables. Have you manually created all the poco tables required for you to jump ship to NHibernate or have you found some tool to do the heavy lifting for you?(after all 50+ tables is a lot in anyone’s money)
Ant
(P.S. I’d previously been eyeing up Castle ActiveRecord as a replacement for SubSonic)
June 17, 2009 at 8:14 pm
Hi Ant,
I created a code generation tool that uses NVelocity for templating that allows me to spit out the POCO’s quickly. It’s not exactly how I think it should be done, but with an existing system, I wanted to get on to the meat of the work quickly and this helped with that.
July 26, 2009 at 5:09 am
There is a great CMS that I use called Cuyahoga (http://www.cuyahoga-project.org/) and this uses NHibernate also. The system is great. In fact, the reason I have just found and am currently using Dashcommerce is due to the requirement to source an ECommerce solution for some jobs that may be coming up.
I would REALLY like to see the two work together… I am looking at this right now! So the news that you are to move to NHibernate is great. Take a look at http://www.cuyahoga-project.org/. I think Dashcommerce would make an awesome module…
July 26, 2009 at 8:26 am
Yes, I am familiar with Cuyahoga. It is a very nice system. I used it as inspiration for the CMS functionality in dC.
dC isn’t built as a module for any CMS, and I don’t have any plans to do that.