NHibernate Spatial for NHibernate 3.x

NHibernate Spatial for NHibernate 3.x

Some of you may have heard of NHibernate, some of you may not. I have been using it for about a year now at my work and it’s a really nice ORM to use. It’s especially easy to use with FluentHibernate as one can configure it from code. My friend and colleague Jaco Adriaansen wrote a good article about Fluent Hibernate if you are interested in this subject. For arguments sake, I will assume you have at least some basic knowledge of NHibernate.

The part I will be focussing on in this entry is NHibernate.Spatial. NH Spatial can be used to integrate spatial data into your NHibernate based application. It is a great library written by Ricardo Stuven. Unfortunately, development was halted some time ago and it never saw support for NHibernate 3.x… Until now!

Requirements

For work, we looked at getting NHibernate Spatial to work with NH3. Getting it too work didn’t turn out to be a lot of work, but I wanted to lift it into 2012. It was built with VS2008. It used old versions of the NetTopologySuite and GeoAPI. Also, I want to push NHS to NuGet. As NHibernate is C# .NET based, let’s take a more Microsoft approach. My personal goal was being able to build and test NHibernate Spatial with just Visual Studio 2012 (express) and no external tools.

So we have a few new requirements for NHibernate Spatial:

  1. All external packages should be NuGet-based
  2. All tests should be MS Tests instead of NUnit.
  3. All NHibernate Spatial libraries should be pushed to NuGet
  4. All NHibernate Spatial libraries should work with the current NHibernate version.

External packages should be NuGet-based

Microsoft has been plugging NuGet as the best package manager to date. Although this could be up for discussion, I do think it’s a good package system with a proper integration with Visual Studio. I want NHibernate spatial to be widely used so we need to make sure people can find and install it with little effort as possible. Also, sending updates via NuGet should be a lot easier and people who use NH Spatial should have more incentive to keep their application up to date.

NHibernate Spatial has some external dependencies of its own. The original codebase on NHForge.org delivers those DLL’s with the package. In the meantime, those dependencies have been updated quite a lot as well.

GeoAPI, NetTopologySuite, NHibernate are the three core dependencies. All three have been published to the NuGet official repository. So why not use them in NHibernate Spatial?

Here we found out something special. If you want to create your own NuGet package, that contains references to other NuGet packages, do not push along the binaries of the packages. It doesn’t really work with teams as explained in this stack overflow question.

So now we have used the NuGet tool to add NHibernate, NetTopologySuite and GeoAPI to NHibernate.Spatial.

Unable to comply, building in progress

Sorry for using a cheap Command & Conquer reference there, but it took quite some time to get it all to build. Apparently there has been ‘some’ changes in GeoAPI / NTS in the last few years. So a few things I noticed. First of all, GeoAPI stopped using interfaces. Well, they still exist, but a lot of the code uses types instead of interfaces in function calls now. This makes mocking difficult, but I guess it will be something we should live with for now. A future idea could be to put a layer between NH Spatial and GeoAPI/NetTopologySuite so we can use mocking again for testing purposes. Again, I think this is a thing for the future, and may probably never happen.

Getting everything to work on NH3 took some bold editing. As I’m not the original writer of the code I don’t know every assumption made. I had to edit function signatures without really knowing what the result would be.

I started out with changing all references to the correct ones. NetTopologySuite, for instance, changed from “GisSharpBlog.NetTopologySuite.Geometries” to “NetTopologySuite.Geometries”. Also an .IO namespace was added for all the different database types. That last namespace is a bit annoying: NHibernate.Spatial has a different project for each database type. When we load the NetTopologySuite.IO NuGet package it will put, for example, a reference to NetTopologySuite.IO.PostGis into NHibernate.Spatial.MSSQLSpatial.

Tests should be MS Tests instead of NUnit

First of all, there is nothing wrong with NUnit. In fact I have used it many times and it works great. Then again, when I used NUnit it was on .NET Framework version 2. It seems that it has some more trouble switching to higher versions of the framework. The latest version does seem to cope with the .NET Framework version 4, but when I started working with Nhibernate Spatial I had a lot of problems. So then I thought, why not use Microsofts built in test framework? It has a nice integration with VS2012 so why not.

NUnit and MS Test are both pretty much the same. You can write test-, setup- and teardown methods, just like NUnit. They just named it a bit different:

Item NUnit MS Test
Test class attribute TestFixture TestClass
Setting up the teststate Setup TestInitialize
Tearing down the teststate TearDown TestCleanup
A testmethod Test TestMethod

Then ofcourse each namespace has its own Assertion procedures. At the moment of writing this article, I’m still working on these unit tests, as it turned out to be more work then I thought it would be.

NH Spatial should be pushed to NuGet

I like NH Spatial. I love working with it, I like the way it does all sorts of things for me. I feel that more people should be able to enjoy it as well. NuGet is a great distribution tool to deliver wonderfull components like Nhibernate Spatial to a greater audience. My main reason for wanting to push the package to NuGet official repositories is so more people will be able to use it. They should be able to get it into their own application with relative ease, without having to struggle with compiling it against versions of dependencies. Without the need to think about what version they would need, as NuGet will just give you the latest of everything. That’s the general idea anyway…

NH Spatial should be compiled against latest NHibernate version.

If we want NH Spatial to be used in a broader context, it should be up to date. NH Spatial was built against NH2.X and I think that’s why there is no active user base. If beginners want to get enthousiastic about NH Spatial, it should work with latest versions. I want it to become really easy for our future users to obtain that they will spread the word about it. I want everybody to be able to use some form of spatial data in their applications, it doesn’t matter if it’s web of desktop based. It should be hassle free, up to date and easy too use.

In conclusion

NH Spatial is not done yet, but its workable for now when using MSSQL 2008. The code is currently in my private BitBucket repo. I’m note sure yet where it should be. I would say NHForge, but I’m not sure yet how to get access to it. I have tried to contact Ricardo Stuven, the original programmer for NH Spatial, but I have yet to receive a reply.

The current state of code:

Item Current Status
Compile against NH 3.3.1 (latest NuGet version) DONE
Compile against GeoAPI 1.6 (latest NuGet version) DONE
Compile against NetTopologySuite 1.12 (latest NuGet version) DONE
Compile all IO plugins against the new NHS code DONE
Build Oracle IO Plugin TODO
Rebuild Testcases TODO
Push to NuGet TODO
Publish Sourcecode TODO

 If somebody needs a snapshot of the current sourcebuild, just leave a comment or use the contact form. I can give you a zip with the code or maybe even access to the repo if you want to participate. I can give you nuget packages as well of the current state. (although they still need a bit of work)

I will create a separate page on this website with the current status of the project in the next few days. I plan to write more detailed articles on specifics of the project. This could be technical working of it, or how to use it in your own applications. I’m not sure when I will have time to write it as I first want to finish the TODO’s from the list above.

Please leave a comment if you have any questions / remarks.