views:

179

answers:

5

Hi all, i think this issue is common in web applications with a middle-sized model. Let's say I have a SportCenter class holding a list of BasketballField, when showing reservations or properties of a BasketballField I still want to show few information about the SportCenter it belongs to.

I'm using ASP.Net MVC and nHibernate for the data layer, so my question is: is it worth to make nHibernate load a whole SportCenter instance (actually contained collections are lazy-loaded but still the class is "heavy") together with my BasketballField and its infos just to show just few fields of the SportCenter?

On the other hand, building very fine-tuned queries in HQL take me back to old Classic ASP days with hand-made SQL queries...

Any best-practice to suggest?

Thank you all, Peter.

+2  A: 

Try it, run it, profile it. Try both ways, and use a profile like Red Gate's ANTS Profiler to see whether there is a noticeable performance difference.

If there isn't that much of a difference, then use the one that is more readable - using the SportCenter class - otherwise, use HQL.

IMHO, OR/Ms, in their current state, aren't a complete replacement for SQL/variants.

Lucas Jones
+1 rules of optimization: http://www.c2.com/cgi/wiki?RulesOfOptimization
Mauricio Scheffer
BTW I'd recommend nhprof (http://nhprof.com/) instead of ANTS for this.
Mauricio Scheffer
A: 

If your querying it from the BasketballField end then each BasketballField can either "fetch join" the SportCentre, or turn lazy loading off from the BasketballField end (as each BasketballField will only have one SportCentre).

The major overheads I've experienced with this come from nHibernate thrashing the database with lots of small lazy loads, the solution is normally to make it return all the data in a single roundtrip.

Jon Spokes
A: 

I'm becoming a strong believer in Command/Query Separation (the Greg Young version) so I would say if you want to display data to the user (Query) you should definitely not be using your domain model. Getters are quite often an anti pattern when it comes to a good domain.

My currently preferred process is to use screen specific DTOs populated by NHibernate. These work best when driven off a separate table but you can use NHibernates AliasToBeanTransformer to populate from your domain table.

ShaneC
But screen specific DTOs make the Business Layer aware and dependent - at least semantically - on the UI layer, which is not good. Even writing specific queries out of nHibernate just to generate "views"will ruin the nHibernate-only Data Acess layer.Anyway your point sounds well-motivated, what do you mean exactly with "Getters are quite often an anti pattern when it comes to a good domain"?Thank you for your answer, Peter.
Peter
Actually your DTOs should live in a separate query layer whose only job is to fulfill the needs of client querying in which case the dependency is (imho) fine.The Business Layer (or in my case Domain Layer) exists to service Commands. The problem with getters is that they quickly lead to anemic domains. If everyone can get at all your data (which should be private) then why would they ever need you to do anything for them? Ever since I read about the "Tell, Don't Ask" principle in Pragmatic Programmers I've been making an effort to stay more OO in my designs.
ShaneC
A: 

Consider Linq. NHibernate supports Linq. With Linq it's easy to write type safe queries that return "partial objects" or whatever fields you want really.

The other two options you presented are probably "good enough" and should work.

As person-b said, use a profiler if you are concerned with performance.

View specific DTOs are a best practice and probably make sense here.

Michael Maddox
A: 

Your problem is that you are exposing your Domain to UI layer. You should separate your Model from your ViewModel.

epitka