Interested?
We've got a pretty basic version of an NHibernateDatasource control for ASP.NET that we wrote a few projects back. We got really tired of trying to work with ObjectDataSource, switched to another developer's component called
MyObjectDataSource, and then decided to try to write out own NHibernateDataSource, which turned out to be surprisingly simple.
It was adapted from some
sample code that Paul Wilson wrote for his O/R Mapper.
I've looked around and it doesn't look like anyone has written an equivalent for NHibernate.
If anyone is aware of one, I'd really like to see what other people have done.
Otherwise, if anyone is interested in our code, let me know and I'll try to package it up in a reusable way.
We're pretty happy with it. It allows us to use ASP.NET 2.0's 2-way databinding, often without any code required.
The Code
The code we've written boils down to:
- SessionHolder -- a utility class that, together with our
- NHibernateModule, manages the creation and disposal of an ISession within the context of an http request.
- NHibernateTemplate - a little utility wrapper around an ISession that provides a few convenience methods and ultra-simple transaction wrapping.
- NHibernateUtils - some static utility methods to create the SessionFactory and support attributes
- NHibernateDataSource - a web control that allows you to declare the type to be loaded or specify an HQL query. You can also add parameters to do further filtering.
The net result is that you can create an ASP page and drop in something like:
Code:
<hb:NHibernateDataSource ID="NHibernateDataSource1" runat="server" TypeName="My.Model.SomeType"/>
And then bind a GridView to it (specifying the correct key field) and get full CRUD and sorting support just like you get with a SqlDataSource.
You can also add parameters to filter the query. So you could add a parameter like:
Code:
<asp:QueryStringParameter Name="id" QueryStringField="Id" />
And then bind to a FormView and you have a detail view with full CRUD support.
This has worked really well for us on two projects and we love it.
Caveats...- The code has only been tested with NHibernate-1.2.0.Alpha1.
- The code was quick and dirty -- undocumented and a little ugly
- We may not exactly be following "best practices" for session and transaction management. That said, we haven't had any problems and this control has saved us a lot of time.
- There probably won't be any runnable sample code
- The control does not yet support pagination
- The designer support isn't as rich as for the MS datasource controls
- ASP.NET 2-way databinding still kinda sucks in that it does not support nested properties.
I just thought I'd mention it in case anyone was interested. We love Hibernate and NHibernate and use them on every Java and .NET project we do.