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)?