views:

389

answers:

2

Hi There

I have been using the ncommon framework (http://code.google.com/p/ncommon/) with nhibernate and asp.net mvc. I really like the implementation of the unit of work pattern, but am having problems with lazy loading in my views. I am looking how to implement the session per request pattern with ncommon or look at another framework that will support:

  1. linq to nhibernate
  2. unit of work pattern
  3. session per request pattern to support lazy loading in views

Any tips greatly appreciated.

Samuel

+1  A: 

Take a look at SharpArchitecture.net for a great NHibernate based framework for MVC that I believe will fit your specifications.

Michael Gattuso
Thanks Michael. I know the sharparchitecture framework, but find it it like using a atom bomb to kill a mouse ;-) If i remember the sharp architecture does not use the linq provider
Chev
Ha. You're probably right about the atom bomb thing! The sharp arch does have the nhibernate linq provider though.
Michael Gattuso
At the heart of the matter is sounds like you are closing your session to soon. With the session-per-request technique you open the session in the context of HttpApplication in Application_BeginRequest and close it in Application_EndRequest. You can do this in Global.asax. Sharp Architectures source code could give you some ideas on how to do this :-)
Michael Gattuso
Thanks Michael - going to take a another look at the sharp architecture and will post findings
Chev
+2  A: 

First of all if you are using your entities in your view model you are doing it wrong. I am sorry to break it to you but you can't really use your database model in your views. If I have for instance a Competition entity in the database I might end up with 3-4 CompetitionView based on what I need. Basically I load everything I might need eagerly and send just what is required for that specific view.

At first this seemed like more work to me than trying to solve it with NHibernate but in the end I found that I gain a tremendous amount of time on doing this straight away. There is also an open source project called AutoMapper that can transfer data between the various entities in your different models.

Hope this helps and that you don't despair on the best-practice!

mhenrixon
Thanks for the reply.I am using domain entities in my views. Not sure I understand that statement about the database model in my views.As the question states, I need to lazy load in my views and don't want to eager load. There is a reason the ORM's have this feature. Eager loading some of my trees would result in a performance hit and would send to much unnecessary data to the client.
Chev
"You can't really use your database model in your views" - This is a bit broad. If you are using a single object or a subset of an object there is no need to create a view model. I would agree that a complex view that has several distinct classes of objects or forms with drop downs etc. that need to be populated then absolutely create view models.
Michael Gattuso