HappyNomad:

I got ObservableBag working with the new trunk by extending
PersistantGenericBag and replacing the InnerBag with an
ObservableCollection. See below:

    [Serializable, DebuggerTypeProxy(typeof
(NHibernate.DebugHelpers.CollectionProxy<>))]
    public class PersistentObservableBag<T> : PersistentGenericBag<T>,
INotifyCollectionChanged
    {

        public PersistentObservableBag(ISessionImplementor session)
            : base(session)
        {
        }

        public PersistentObservableBag(ISessionImplementor session,
ICollection<T> coll)
            : base(session, coll)
        {
            INotifyCollectionChanged collection = coll as
INotifyCollectionChanged;
            if (collection != null)
            {
                collection.CollectionChanged += new
NotifyCollectionChangedEventHandler(OnCollectionChanged);
            }
        }

        public override void BeforeInitialize(ICollectionPersister
persister, int anticipatedSize)
        {
            this.InternalBag = (IList<T>)
persister.CollectionType.Instantiate(anticipatedSize);
            INotifyCollectionChanged collection = base.InternalBag as
INotifyCollectionChanged;
            if (collection != null)
            {
                collection.CollectionChanged += new
NotifyCollectionChangedEventHandler(OnCollectionChanged);
            }
        }

        protected void OnCollectionChanged(object sender,
NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
                CollectionChanged(this, e);
        }

        public event NotifyCollectionChangedEventHandler
CollectionChanged;

    }

    public class ObservableBagType<T> : IUserCollectionType
    {
        #region IUserCollectionType Members

        public bool Contains(object collection, object entity)
        {
            return ((IList<T>)collection).Contains((T)entity);
        }

        public IEnumerable GetElements(object collection)
        {
            return (IEnumerable)collection;
        }

        public object IndexOf(object collection, object entity)
        {
            return ((IList<T>)collection).IndexOf((T)entity);
        }

        public object ReplaceElements(object original, object target,
ICollectionPersister persister, object owner, IDictionary copyCache,
ISessionImplementor session)
        {
            IList<T> result = (IList<T>)target;
            result.Clear();
            foreach (object item in ((IEnumerable)original))
                result.Add((T)item);
            return result;
        }

        // return an instance of the inner collection type
        public object Instantiate(int anticipatedSize)
        {
            return new ObservableCollection<T>();
        }

        public IPersistentCollection Instantiate(ISessionImplementor
session, ICollectionPersister persister)
        {
            return new PersistentObservableBag<T>(session);
        }

        public IPersistentCollection Wrap(ISessionImplementor session,
object collection)
        {
            return new PersistentObservableBag<T>(session,
(ICollection<T>)collection);
        }

        #endregion
    }

Reply via email to