-->
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.  [ 9 posts ] 
Author Message
 Post subject: INotifyPropertyChanged, INotifyCollectionChanged support
PostPosted: Mon Nov 06, 2006 3:37 pm 
Newbie

Joined: Mon Nov 06, 2006 3:14 pm
Posts: 4
Greetings,

I am writing a WPF application and want to use NHibernate. To avoid writing a UI layer over my domain model (at first, at least), and to make data binding work well with my domain objects, I would like to have all of my collections implement INotifyCollectionChanged and my domain objects implement INotifyPropertyChanged. Doing this will make the data binding "just work".

I was impressed with the level of control NHibernate gives you for domain objects, basically giving you unlimited freedom in inheriting and implementing whatever you desire.

Unfortunately, I don't see the same flexibility with what collections NHibernate uses. There is no way (that I can see) to make the collections implement any interface because the collections are using proxies to update themselves.

So, my question is, are there any plans to include these interfaces optionally in the collections? If not, do I have any options for getting my collections to implement these interfaces on my own other than changing the NHibernate codebase?

Thanks,
Michael Hedgpeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 11:15 pm 
Newbie

Joined: Mon Nov 06, 2006 3:14 pm
Posts: 4
I've done more investigation in the codebase and see that the collections are hard-coded into the system.

What would be really nice is if I could provide my own collections that implemented my own interfaces that subclassed the Persistent collections NHibernate uses. It would use a form of configurable dependency injection.

I take it that kind of thing hasn't been considered yet. I supposed I'll update the source code of NHibernate on my machine to suit my needs and move on.

Thanks,
Michael Hedgpeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 3:24 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Have you seen IUserCollectionType in 1.2.0?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 7:46 pm 
Newbie

Joined: Wed Jan 24, 2007 7:37 pm
Posts: 3
Location: Houston Metro, TX, USA
I've worked out a solution for this issue. I developed some collection classes which implement NHibernate's IUserCollectionType and WPF's INotifyCollectionChanged interface that will trigger UI updates when their contents change. Check out my blog article for more info and to download my demo project:

http://analog-man.blogspot.com/2007/01/bridge-gap-between-your-nhibernate.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 10:08 pm 
Newbie

Joined: Mon Jun 18, 2007 9:54 pm
Posts: 2
I also have a question regarding this topic.

I have used gdereese's blog article to implement a custom collection class implementing "INotifyCollectionChanged". Was wondering how this works when making a query against a particular persistent class.

For example, IList<Book> = _session.CreateCriteria(typeof(Book)).List<Book>(); How could I get this to return an IObservableList<Book> subclass?

Thanks in advance for any comments.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 7:11 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
you cannot change the type of collection that a criteria query will return. Your only alternative is to poulate an IObservableList<Book> with an IList<Book>. Custom collection types have nothing to do with results returned from the query API's.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 4:30 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We handled this by using field mapping rather than property mapping (field.camelcase-underscore), with the field that NHibernate sees feeding a private property with a different name. Then the public property whose name does match the field is of our collection type, which uses the composition design pattern to forward to the underlying collection.

So, an example looks like this:

Code:
public class Contact
{
    private IList _addresses;
    private OurListType<Address> _addressesTyped;

    public virtual OurListType<Address> Addresses
    {
        get
        {
            if (this._addressesTyped == null)
            {
                this._addressesTyped = new OurListType<Address>(this.AddressesUntyped, ...);
            }
            else if (!object.Equals(this._addressesTyped.InnerList, this.AddressesUntyped)
            {
                this._addressesTyped.InnerList = this.AddressesUntyped;
            }
            return this._addressesTyped;
    }

    private IList AddressesUntyped
    {
        get
        {
            if (this._addresses == null)
            {
                this._addresses = new ArrayList();
            }
            return this._addresses;
        }
    }

    ...
}


Then OurListType can provide whatever it wants -- we implement IList, IList<T>, IBindingListView, IComparer, etc. Our list type tracks the property name on the item class that references the owner of the list (the "parent"), and automatically sets/clears it when items are added/removed from the list.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 12:27 am 
Newbie

Joined: Mon Jun 18, 2007 9:54 pm
Posts: 2
Thanks Nels_P_Olsen and jnapier for your replies. Pretty much affirmed what I was thinking. It just seems that if a collection could be mapped to a custom IList<> collection, the same functionality could be added to ICriteria. This would cleanly address binding issues of object lists returned from a query, especially in a WPF application.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 12:20 pm 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
After studying gdereese's example, I created an improved solution which includes three types (bag, list, and set) of observable collections. See my blog entry (http://happynomad121.blogspot.com/2007/12/collections-for-wpf-and-nhibernate.html) for further details and to download the solution.


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