when you load an entity with a collection from nhibernate; nhibernate will change your collection (field or property) with a Persistent representation (is like a proxy, and one of the purposes is to make lazy loading work). You can't change the instance of the collection with a new one.
If you need some especial behavior or especial collection, you can write one, from the one insides nhibernate PersistentXX (PersistentBag, PersistentGenericBag, PersistentSet). Then write a custom TypeCollectionFactory, or specify the type of the collection in the mapping. Having said that, i think you are doing it wrong. The undo/redo thing is an UI behavior, and you should model this in ViewModels. Even if you need undo capabilities in your entities ISession.Refresh is more than enough for most of the cases. 2011/3/4 H.Alex <[email protected]> > I've build a generic undo/redo manager that should work on any object. > I plan to publish it as an article, I don't know, on CodeProject > maybe. Anyhow, it works like a charm, except on NHibernate > collections. It causes NHibernate (3.0) to fail when updating an > object with the 'Found two representations of same collection' > NHibernate exception. Naturally, I'd like to fix this but I'm out of > ideas so I'm posting here hoping for help. > > In my StateManager I'm using Castle.DynamicProxy to create proxy > objects and I intercept setter calls to log changes. The problem is, > besides loging, I replace a set object with a proxy that then also > intercepts setters and also add/remove etc. methods for collections. > That way I get a nice deep 'state managebility' :) which works fine > (when updating) for objects but not for collections. I think the > problem is that for objects I create a class proxy with target but for > collections this is impossible b/c they are sealed classes or the > relevant methods are not virtual. Thus I create an interface proxy > with target. I think this is what throws NHibernate off. > > Also to menition, I'm using an interceptor to intercept the > Instantiate method so to get 'state trackable' entities back right > from NHibernate. And to note that I lazy load all my relations with > the 'no-proxy' option. Before I did that I couldn't get anything to > work:) > > So, in just a few lines of code I'd like to illustrate the problem. > > I am using a ManagerPool (my class) as a mean for session management > and also a Manager object that wraps session operations. No fault > there, as everything works and is well tested. The employee the > manager returns is state trackable because like I explained the > Instantiate method is intercepted. > > I hope someone can make NHibernate understand or suggest a way around > it. > > [Test] > public void Updating_state_trackable_entities_should_work() > { > var mp = new > ManagerPool(Shared.StateManager.NHibernateHelper.Helper); > > var empMan = mp.GetManager<EmployeeManager>(); > var emp = empMan.GetAll().First(); > emp.Person.Name.First = "Donald"; > emp.Person.Name.Last = "Duck"; > > emp.AddVacation(new Vacation() { LeaveStart = > DateTime.Today.AddDays(-7), LeaveEnd = DateTime.Today, PayPercentage = > 5 }); > > var stateManager = (emp as IStateTrackable).Originator; > Assert.IsNotNull(stateManager); > Assert.IsTrue(stateManager.CanUndo); > Assert.AreEqual(3, stateManager.ActionsCount); > > //this fails with 'Found two representations of same > collection' NHibernate exception. > empMan.Update(emp); > > var mp2 = new ManagerPool(); > var emp2 = > mp2.GetManager<EmployeeManager>().GetAll().First(); > > Assert.AreEqual("Donald", emp2.Person.Name.First); > Assert.AreEqual("Duck", emp2.Person.Name.Last); > Assert.IsTrue(emp2.Vacations.Any(p => p.LeaveStart == > DateTime.Today.AddDays(-7) && p.LeaveEnd == DateTime.Today && > p.PayPercentage == 5)); > } > > Here is the stack trace from the exception: > Test > > 'Indigo.Domain.Tests.StateManager_Fixture.Updating_state_trackable_entities_should_work' > failed: NHibernate.HibernateException : Found two representations of > same collection: Indigo.Domain.Employee.PayAmmounts > at > > NHibernate.Engine.Collections.ProcessReachableCollection(IPersistentCollection > collection, CollectionType type, Object entity, ISessionImplementor > session) > at NHibernate.Event.Default.FlushVisitor.ProcessCollection(Object > collection, CollectionType type) > at NHibernate.Event.Default.AbstractVisitor.ProcessValue(Object > value, IType type) > at NHibernate.Event.Default.AbstractVisitor.ProcessValue(Int32 i, > Object[] values, IType[] types) > at > > NHibernate.Event.Default.AbstractVisitor.ProcessEntityPropertyValues(Object[] > values, IType[] types) > at > > NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent > event) > at > > NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent > event) > at > > NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent > event) > at > NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent > event) > at NHibernate.Impl.SessionImpl.Flush() > at NHibernate.Transaction.AdoTransaction.Commit() > Domain\ManagerBase.cs(62,0): at Shared.Domain.ManagerBase`1.Update(T > item) > Tests\StateManager_Fixture.cs(142,0): at > > Indigo.Domain.Tests.StateManager_Fixture.Updating_state_trackable_entities_should_work() > > Just to make things clear, this is the code that actually generates > the proxies. > > protected override void MakeObject(object target, out object > obj, out Type t) > { > t = target.GetType(); > if (!IsCollection(t)) > { > IInterceptor[] interceptors = > _setterInterceptors.Union(new IInterceptor[] { new > StateTrackableImplementationInterceptor(this, t) }).ToArray(); > obj = _generator.CreateClassProxyWithTarget(t, new > Type[] { typeof(IStateTrackable) }, target, _proxyGenOptionsSetters, > interceptors); > } > else > { > //collections are sealed or their methods aren't > virtual. thus, class proxying is impossible:( > var otherInterfaces = t.GetInterfaces(); > var targetInterface = otherInterfaces[0]; > otherInterfaces[0] = typeof(IStateTrackable); > IInterceptor[] interceptors = > _collectionInterceptors.Union(new IInterceptor[] { new > StateTrackableImplementationInterceptor(this, t) }).ToArray(); > obj = > _generator.CreateInterfaceProxyWithTarget(targetInterface, > otherInterfaces, target, _proxyGenOptionsCollections, interceptors); > } > } > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
