-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: NHibernate DataSource -- interested?
PostPosted: Fri Jan 12, 2007 4:07 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 4:52 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
<-- is Definitely interested.

Can you post or attach the code here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 2:43 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Ok - I've got the code factored into a little demo project using Northwind and it works pretty well. I still need to refactor some proprietary stuff out of the comments and namespaces, but that shouldn't take long.

My big question is - where should I put it? I didn't really see anywhere on the forum, wiki, or website to upload contributions.

I'm leaning towards just writing an article for Code Project and putting it there, but that will take a little time, and I'll definitely want to clean up the code.

In the meantime, if you are eager, send an email to [winston_fassett at hotmail] and I'll send you the code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 5:31 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
Just sent you a request (eric.newton@...XXX... ) for the code.

I'm excited to see this. And Thanks again for taking this on.

I would guess it should go into the NHibernateContrib project. I'd like to see NHibernateContrib include this.

I'll be an advocate for it.

Kudos for doing this!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 8:27 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Eric - I just sent you the code but I got a MailMarshall message back, so you might want to be on the lookout for it.

I'll be very interested to hear what you think. So far I haven't gotten much feedback.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 7:40 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We also created a specialized datasource to for HQL queries, as well as a "relation" object to tie them in parent/child relationships. The "relation" component manages the required sequence of selects for the datasources, dynamically adds necessary filters to the child datasources' HQL queries, set parent entity values in the child datasources for new rows on save, and triggers saves in the child datasources when the visual control attached to the parent datasource is saved. The datasource and relation controls have rich design time support, even including an HQL editor with Intellisense that lets you verify the query semantics right then and there. We also created a framework to dynamically flatten out NHibernate query results at runtime to deal with the need to bind to nested properties.

It all works great -- it lets us do completely declarative setup of web pages driven by NHibernate queries. The next major version of our product is supposed to ship in April, so I hope that I can convince my bosses to release the framework as open source sometime after that.

Assuming the bosses give the go-ahead to make it open source, the only technical barrier to making it work outside our company is that this framework currently requires all entities to implement a specific interface. We're planning to remove that requirement in time for our product's April release ...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 5:09 pm 
Newbie

Joined: Fri Mar 24, 2006 4:46 pm
Posts: 13
I would also be interested in taking a look at this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 12:39 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Nels -

That sounds very interesting! Particularly the HQL parsing stuff. I wasn't able to figure out the HQL parser, so I just did some primitive string parsing when I needed to manipulate a query.

If your boss is ok with it, I would love to see what you have done.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:23 am 
Newbie

Joined: Wed Dec 07, 2005 1:33 am
Posts: 7
You may look at how Ayende has done in his excellent Nhibernate Query Analyzer. I guess that might helpful.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 12:07 pm 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
wfassett wrote:
Eric - I just sent you the code but I got a MailMarshall message back, so you might want to be on the lookout for it.

I'll be very interested to hear what you think. So far I haven't gotten much feedback.


Hi wfassett,

Why don't you make it available online if you want some opinions. For example you can put it on the Community Wiki on http://www.hibernate.org/37.html with the necessary starting guide, and make a reference from here. I would happily try it if it was available, and I think many more would give it a shot also.

_________________
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 6:39 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
I had originally planned on posting the code to the wiki, but I couldn't find anywhere to upload an attachment.

I just posted the code in an article on CodeProject:
[url=http://www.codeproject.com/useritems/NHibernateDataSource.asp]
NHibernateDataSource: A DataSourceControl for ASP.NET 2.0[/url]


Please check it out and let me know what you think.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 1:19 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
I'm just now starting to use it. I will report back soon.

I really like this a lot. Can't wait for a LinqDatasource control and LINQ-Nhibernate adapter.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.