-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to map ObservableCollection(Of T)
PostPosted: Tue Nov 20, 2007 11:23 pm 
Newbie

Joined: Tue Nov 20, 2007 11:18 pm
Posts: 2
Hi,
Anybody has experience to map ObservableCollection?

my code like this:
Code:
   class Group
   {
      public    string name;
      public ObservableCollection<User> users;   
    }

 


I tried all set, list, map, bag and arrary.
all of it cannot work.

exception is:

"Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[User]' to type 'System.Collections.ObjectModel.ObservableCollection`1[User]'."


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 6:33 am 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
Observable collections do not come packaged with NHibernate, so you will need to use a custom collection type. Gary DeReese has a workable example that accomplishes this, which he’s posted to his blog. I’ve recently been improving upon his work and submitting comments there. Go to Gary’s blog to get started: http://analog-man.blogspot.com/2007/01/ ... rnate.html

I’ve also started working on an "ObservableSet" class that derives from Iesi.Collections.Generic.HashedSet, which you may find useful.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 1:12 am 
Newbie

Joined: Tue Nov 20, 2007 11:18 pm
Posts: 2
Thank you HappyNomad.

It is working now.
I find that your post sample is a bit complicated.

I change my code to:

Code:


class Group
   {
      public    string name;
      public ObservableCollection<User> users;   

      private IList<User> usersNhibernate
      {
             get{
                 return users;
             }
              set{
                if(null != value)
                {
                      ObservableCollection<User> tmpUsers=new ObservableCollection<User>();
                      foreach(User user in value)
                      {
                           tmpUsers.add(user);
                       }
                      users=tmpUsers;
                 }
              }

       }
   

    }




And in the mapping file, instead of mapping users collection, I map usersNhibernate property.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 3:11 am 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
Are you sure that code behaves correctly? Every time NHibernate tries to set the usersNhibernate property to an instance of it's own collection implementation, it looks like your code then throws away that instance. You may find a problem after running some tests. Using the download from Gary's blog, I suggest you follow the example in ListContainer.cs. The collection property there gets set to an instance of either ObservableList<T> or PersistentGenericObservableBag<T>.

The code on Gary's blog is the best currently available for easily including persistent observable lists in your program. However, I've made a modified version which I think is an improvement. In my version, client collection properties are declared of type IList<T> and transient collection instances are of type ObservableCollection<T> instead of a custom collection class. This code is not yet online but I can let you know when it is.


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

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
I've completed my implementation of 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.  [ 5 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.