views:

228

answers:

1

I understand that this is highly specific to the concrete application, but I'm just wondering what's the general opinion, or at least some personal experiences on the issue.

I have an aversion towards the 'open session in view' pattern, so to avoid it, I'm thinking about simply fetching everything small eagerly, and using queries in the service layer to fetch larger stuff.

Has anyone used this and regretted it? And is there maybe some elegant solution to lazy loading in the view layer that I'm not aware of?

+2  A: 

Lazy loading is only beneficial if you aren't going to use the data in question at all (e.g. showing only a list of customers, hereby ignoring the nested set of orders), or if it's not to be decided yet whether the user would like to view the data in question (e.g. having a list of customers in memory and the request for list of orders depends on future actions).

If you'll for sure show all the data at once, then lazy loading is not needed, it would only cost an extra query.

BalusC
I agree that this is where the performance benefits come from, but I'm interested to see if they really matter, and are they worth sacrificing code elegance for.Of course, all the data will rarely be shown at once, but in your example, I wouldn't want to lazy load customer's orders anyway, there may be thousands, and they should be paginated, so a custom query is needed. And if a customer has an address object, for example, that seems small enough to eagerly load, so I don't need lazy here neither. So, is my suspicion correct? Is lazy loading not worth the trouble?
Robert
It indeed depends more on how many memory it unnecessarily consumes. You'd like to have your application to be as memory efficient as possible. A profiler can give new insights.
BalusC