Thursday, March 12, 2009

NHibernate.Linq

Hiatus


Right now I need to park my attempts at configuration of a new Windows Server 2008 r2 Core web server. I've effectively managed to hose my configuration, I hope temporarily, in the process of applying a security template using secedit.exe. Not one of my best moments, particularly when I've had something similar occur previously. Unfortunately there just isn't a whole lot of info I can find about using secedit but I'll keep all those details for another post.


NHibernate


Having used the Java Hibernate ORM tool previously, plus having an interest in LINQ and wanting to cut some actual code, I figured I would kill three birds using NHibernate.Linq. Of course once I started googling it became apparent that there are many interesting things occuring in the Alt.Net space, enough to get happily side-tracked for some time.


I eventually found my way back and followed these instructions to build NHibernate from the latest subversion source. The mystery of the missing assemblyinfo.cs files was quickly solved although I did have to install NAnt and modify the default.build NAnt file to correctly generate the missing file for the NHibernate.ByteCode.Spring project. I think the entry had been removed to decouple NHibernate from Spring but the work may not have been complete at the point that I retrieved the source. The source has a comprehensive test suite which, apart from highlighting issues using NHibernate with my PostgreSQL database, is also a great starting point for information about using NHibernate.


PostgreSQL


As mentioned above, I'm using Postgres which happens to be installed on my MacBook Pro. I wanted to run the test suite against it from my Vista VM. Connecting to Postgres was actually pretty straight forward. I created a database user nhibernate with a long random password (using uuidgen) and modified the provided Postgres template, renaming it to hibernate.cfg.xml (see below). Apart from setting the appropriate values for the user credentials and server, the connection string in the template incorrectly specified "Initial Catalog" — this needed to be changed to "Database". The Postgres driver is provided by the mono project which also contains good information about the connection string parameters. Two assemblies are required, mono.security.dll, and the driver itself Npgsql.dll. Once these things are in place, and assuming you already have the nunit framework installed, the test suite runs quite happily against the Postgres database. A small percentage of tests fail, typically due to some feature difference between the target SQLServer database and Postgres, a good way to highlight those things that might bite later.



<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">
Server=mbp;Database=nhibernate;User ID=nhibernate;Password=D4F52CC8-5603-4C84-9147-3CC602EF359A;
</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>

NHibernate.Linq


NHibernate.Linq is part of the NH Contrib project and the source is available from subversion. I replaced the NHibernate.dll with the one generated above. The test suite uses the NorthWind database so no joy testing against Postgres. I have SQLExpress installed in my Vista VM and with some jiggery pokery the tests will run successfully. First allow the user Full control on folder C:\SQL Server 2000 Sample Databases (or second in the event that attempting to attach the NorthWind database results in an error 5 exception). After starting SQLExpress if it isn't already running, it's necessary to attach the NorthWind database using the sp_attach_db stored procedure. And might as well create the Test database that is also required by the test suite.


By default accessing SQLExpress is limited to the Shared Memory protocol (some info and much joy can be found at SQL Server 2005 SQLExpress error: ...provider:Named Pipes Provider, error 40 - Could not open connection to SQL Server — fortunately I was already familiar with the issue, others obviously weren't so lucky). The NHibernate.Linq test suite uses Named Pipes for connecting to the database and will choke if the protocol isn't enabled. Enabling Named Pipes is straight forward using SQLServer Configuration Manager and requires the service to be restarted.


The other trap to be aware of is the surface area configuration which needed to be modified to add my user as an administrator and ensure remote connections via named pipes are enabled. This is managed by the SQL Server 2005 Surface Area Configuration tool.


app.config file for the test suite also needs to be modified to connect to SQLExpress:



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test"
connectionString="Data Source=.\SQLExpress;initial catalog=Test;Integrated Security=SSPI"/>
<add name="Northwind"
connectionString="Data Source=.\SQLExpress;Initial Catalog=NorthWind;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>

Running


To actually run the tests, the NHibernate.Linq.Test project needs to be the Startup Project and the project properties modified to use NUnit for testing.


Finally the tests run successfully. 46 of the tests are ignored which indicates there is some work yet to be completed but at least it is obvious where the work is required.

1 comment:

Anonymous said...

Thanks for this post - this was easy to find and saved me a nice amount of time both in setup details and in knowing that all tests are not 'green'!