I am having a blast with the series where I am updating my simple Mix 09 Business Application demo. In this part, I wanted to explore one of the most popular data access solution for .NET – NHibernate. Many customers finds that the flexibility of NHibernate makes it easier for them to build maintainable and testable applications. As an aside, I think NHibernate is an excellent example of the vibrant open source community on .NET that I’d like to support.
You can see the full series here.
The demo requires (all 100% free and always free):
- VS2008 SP1
- Silverlight 3 RTM
- .NET RIA Services July '09 Preview
- NHibernate (w/ NHibernate Linq ) and Fluent NHibernate
Basically what I wanted to do was switch over my DomainService from getting its data from Entity Framework to getting its data from NHibernate. The rest of the application stays effectively the same.
To start with I grabbed the code from the previous walk through, deleted the edmx file.
Below is the code I use to create the NHibernate SessionFactory. The fluent interface makes it very easy to configure.
Next, let’s look at the mapping class.. this shows how the data from the database are mapped into a .NET Class.
And here is the type I map the data into. Notice this has the validation attributes for RIA Services to do the validation on the client and server.
That’s the bulk of the NHibernate specific code. Now let’s look at the DomainService..
First, in line 2, notice I have factored some of the plumbing code into a base class. This is very simple (not production ready) example.. we will look it in more detail later. Then we return the static SessionFactory the the base class. The SessionFactory is static such that it is created only once per AppDomain rather than on each request.
Here we use NHibernate Linq to return an IQueryable that RIA Services can use to do paging, filtering, etc.
Then we have Insert and Update which are very basic..
Now, let’s take a look at the NHibernateDomainService base class… Again this is very basic, just to show the concepts..
Notice in the constructor we create the Session… then we close the session in the Dispose() method.
The interesting bit is handling the ChangeSet… Notice we use a transaction to wrap the calls to our update\create methods. This ensures that if their is any failure we roll back the changes (this is the contract for DomainService)…
Now, because we plug into the DomainService pattern, we get our silverlight client with paging, filtering, etc
…and validated editing.
But we also get ASP.NET support via the DomainDataSource as in this example from the sitemap.aspx
And we get a REST based interface via the Astoria support for DomainService.
And we get ASP.NET Dynamic Data Support, again with paging, filtering,
and validating editing.
And all the other presentation tiers..
Wow, pretty cool..
For more on NHibernate, check out Ayende’s blog…
For more NHibernate and DomainService, check out Chris van de Steeg’s blog post ASP.NET MVC, DynamicData, Domain-/RiaServices, Unity and NHibernate: Part 1