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.  [ 8 posts ] 
Author Message
 Post subject: Lazy Loading patterns with WinForms
PostPosted: Thu Jun 29, 2006 10:13 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I'm currently suffering my non-lazy loading app. Everything was beautiful locally, until I loaded in production-wide data. Now it's WAY too slow.

This is understandable, of course, because I have about 8 objects and only 2 are aggregate roots, so as soon as I load one of my objects, it traverses the object graph and loads all assicated collections.

I'm using Windows Forms (2.0) and my layers are separated as follows:

DomainModel: Persistent classes and mapping files. NO reference to DAL
DataAccessLayer: Repositories with NHibernate reference
UI: uses DAL to retrieve/save domain objects

Since my DomainModel doesn't have a reference to the DAL, I cannot implement lazy loading functionality, say through a base class and a level of indirection (ie: a property getter making a call to reconnecto to a session and hydrate the collection).

What patterns would you all suggest for working around this?

Does the UI have to explicitly tell the DAL what to load? Take the blog example, as it is so pervasive on this topic:

should I add these 2 methods? :

Code:
public Blog Load(int blogId)
{  Load(blogId, false); }

public Blog Load(int blogId, loadPosts)
{
   //code to load blog/posts as necessary
}


? this seems like a heavy burden for the UI, but I'm considering it.

Another option I'm thinking is to remove the blog-> posts association and do everything by querying. Ie:

Code:
public IList<Post> GetPosts(Blog blog)
{
   //... implement a query
}


....but I'm weary of design implications that this makes, as my entire DomainModel and DAL are already unit tested and ready... functionality-wise.

Thanks in advance for any advice.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 12:02 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
After some reading of HIA, I am leaning toward leaving the session open, like the session-in-view pattern. The catch here is that I'm not using a web app, so I don't have well-defined lines where I can open and close a session.

Right now I'm relying on 3 operations:
-user clicks "Save Changes"
----all repositories flush their data
----transaction is committed

-user clicks "Cancel Changes"
----transaction is rolled back
----all repositories reload their data

-user closes form
----transaction is rolled back (which will do nothing if they previously clicked "Save changes")


The downside to this is that I am leaving a connection open for quite a while, which doesn't smell right to me.

I'd greatly appreciate any thoughts/advice on this route.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 1:10 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'm not an expert on how to use NHibernate with Windows Forms apps, but, I'd probably be inclined to leave the session open for the duration of the running application. Of course, you would have to test to see what happens if the database connection gets dropped. I'm not sure offhand if NHibernate is smart enough to automatically reconnect if the connection is lost.

This would be a major architectural change, but, you could use XML web services instead. Then, encapsulate all the NHibernate/database access code in the web service and have the Windows Forms client just communicate with the web service. If you do it this way, then you can basically use the same design patterns that you would for a normal web app. i.e. use a session per request.

Of course you run into a number of gotchas when using web services too though. Some of the issues that I ran into are that generic ILists don't work and circular references don't work if you're using the default encoding. In my case, I nulled out properties that I didn't want being sent to the client. Otherwise, lots of extra data would get serialized. One other problem that I ran into is that you can't set lazy="true" for classes (works OK for collections) because the generated proxy classes don't have a no arg constructor and as a result can't be XML serialized.

The web services approach is the one that I'm using with a Windows Forms app and it seems to be working OK after working around the issues that I mentioned above.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 2:26 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I'm not sure how using a web service would be any different than what I'm doing...

I have a persistence layer that can retrieve objects for me and save/update/delete objects.

The part that I'm not so sure about is where should my winforms app should decide to hold a reference to the Repository objects.

consider this example:

Code:
//winforms code
public void BindCatsToGrid()
{
     CatRepository catRepo = new CatRepository(); //should I do this?
     grid.DataSource = CatRepository.GetAll();

     //or
     grid.DAtaSource = CatRepository.Instance.GetAll(); // this?

     //or
     grid.DAtaSource = Repositories.CatRepository.GetAll(); //where repositories is a local winforms singleton
}


none of these are jumping out as elegant. Currently I'm using the 3rd option.[/code]

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject: Re: Lazy Loading patterns with WinForms
PostPosted: Mon Jul 12, 2010 3:27 am 
Newbie

Joined: Mon Jul 12, 2010 3:02 am
Posts: 4
After looking into the answers provided and further research I found an article that uses delegates for the lazy loading. This provided a simpler solution than using proxies or implementing NHibernate.

_________________
Website Marketing New York


Top
 Profile  
 
 Post subject: Re: Lazy Loading patterns with WinForms
PostPosted: Mon Jul 26, 2010 3:27 am 
Newbie

Joined: Mon Jul 26, 2010 3:15 am
Posts: 4
There are so many solution are related to the loading pattern with win forms. For these there are so many facilities can be available such as further research I found an article that uses delegates for the lazy loading. This provided a simpler solution than using proxies or implementing NHibernate.

_________________
voucher codes argos


Top
 Profile  
 
 Post subject: Re: Lazy Loading patterns with WinForms
PostPosted: Mon Sep 06, 2010 3:44 am 
Newbie

Joined: Mon Sep 06, 2010 3:21 am
Posts: 5
Yes I agree with you and it is great that many solution are related to the loading pattern with win forms because this one is reliable then any other.

_________________
package holidays


Top
 Profile  
 
 Post subject: Re: Lazy Loading patterns with WinForms
PostPosted: Wed Sep 22, 2010 10:25 am 
Newbie

Joined: Wed Sep 22, 2010 10:00 am
Posts: 5
Too many solutions in the form of a win is associated with loading patterns. These additional research facilities there, too, such as' lazy loading, I use a delegate can not find a lot of documents. NHibernate implements proxy, or by using a simple solution that provides more.

_________________
live dealer casino


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.