-->
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.  [ 4 posts ] 
Author Message
 Post subject: Custom events on domain objects loaded in dif sessions...
PostPosted: Mon Feb 13, 2006 9:26 am 
Beginner
Beginner

Joined: Thu Dec 08, 2005 6:49 pm
Posts: 49
I am writing a complex windows forms application with lots of ways to view and edit each domain object.
All of my domain objects implement INotifyPropertyChanged (ie, they all have a PropertyChanged event) so that when properties are changed, it is reflected in all the appropriate places.
My object model is selected from the database in lots of separate on-demand sessions (I don't want to load the entire thing on start-up as it is quite large).

To use the common Blog/BlogItem example, if I list all Blogs in one session, then all BlogItems in another session, BlogItem.Blog is never one of the objects loaded in the first list.

Code:
foreach (Blog blog in Logic.ListBlogs())
    foreach (BlogItem item in Logic.ListBlogItems())
        Console.WriteLine(blog == item.Blog);

Both Logic methods above create and dispose their own sessions. Every line written to the console reads False.

I understand that this is perfectly normal behaviour (which is why I have overridden the Equals method on my objects). However my problem is if I register a PropertyChanged event in a Blog object loaded in the second session, then edit the equivalent object loaded in the first, the event obviously isn’t fired (since they are actually separate objects in memory).

What I really need is only one object instance created for any given primary key, no matter what session it is referenced in. I was hoping that the HashtableCacheProvider would achieve this, but nothing seemed to change when I switched it on (how would I tell if it is operating correctly?).
Is there a solution to my problem other than having a static property change registry (and hence adding another level of coupling between my domain and UI)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 3:42 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I too am interested in knowing how to deal with this, as we have the exact same scenario -- lots of separate on-demand sessions, with entities implementing an interface with a PropertyChanged event. I thought the 2nd level cache was supposed to provide the desired behavior, but I've heard people have had trouble getting it to work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 5:10 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, second-level cache does not serve this purpose. Actually, NHibernate is just designed to work in a way where each session has its own copies of objects and objects are not shared between different sessions. Trying to coerce NH into sharing is going to be hard because you will essentially have to "fight" its underlying assumptions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 7:23 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I understand that it's a bad idea to have multiple NHibernate sessions open at the same time, since any given entity can be associated with at most one session. However, since we hear so much about keeping NHibernate sessions short, and because many of us need to develop applications that can be heavily and arbitrarily customized by adding additional, isolated customization layers, it seems we have no choice but to always be creating and discarding NHibernate sessions for each little additional task that could possibly be executed. The entity objects then need to be constantly reassociated with new NHibernate sessions, via ISession.Lock() or ISession.SaveOrUpdateCopy().

Is this part of what you mean by "fighting" NHibernate's underlying assumptions? If so, how are we supposed to deal with the need to allow additional logic to be plugged into an application, at just about any point, where that logic may need to load additional entities (either explictly or through lazy-loading)?


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