My domain objects *are* clean, there is nothing in them but db columns
and some basic Calculate this and that methods.. The idea of this undo/
redo project was for it to be able to manage *any* object. What I have
is something like
var stateManagableObj = stateManager.MakeStateManagable(obj);
Or b/c I intercept entity initialization this is done automatically.
And thats all, and it works bar NHibernate collections :)
I do think it's nice you reminded me of the good old separation of
concerns, ie. that I should keep my domain objects irresponsible for
anything but keeping the domain. With that in my, if I fail to make
this work, I'll probably have to keep some other POCOs in my VM and
keep *them* state manageble and then when editing is done copy the
results and commit. Thus I think it'd be better to actually make this
work as then the undo/redo would be just another layer on top of my
already POCO domain.
POCO POCO POCO :D
I don't want to derive any collections as that would defeat the
purpose of the state manager project, ie. its universality.
I mean, all I do is proxy the NHibernate collection thats part of an
object. My code never knows the difference. NHibernate gets confused
somehow. I'd really like to get it to work.
I actually made some progress on this. I added a method to the state
manager: Detach(object). I added functionality to keep references to
proxy targets and this method returns the object in it's original
state, eg. the 'obj' in the example at the beggining. I hoped that I
could fool NHibernate that way to save my objects, but I got another
dead end and this one is more cool. Here's the test for it:
[Test]
public void Updating_state_trackable_entities_should_work2()
{
var mp = new
ManagerPool(Shared.StateManager.NHibernateHelper.Helper);
var compMan = mp.GetManager<CompanyManager>();
var comp = compMan.GetAll().First();
var statemanager = (comp as IStateTrackable).Originator;
comp.Address = "1313 Webfoot Walk, Duckburg, Calisota";
Assert.IsTrue(statemanager.CanUndo);
Assert.AreEqual(1, statemanager.ActionsCount);
var myComp = statemanager.Detach(comp);
Assert.AreSame(myComp, (comp as
IStateTrackable).ProxyTarget);
Assert.IsFalse(statemanager.IsStateTrackable(myComp));
//fails here - NHibernate.MappingException : No persister
for: Object
//compMan.SaveOrUpdate(myComp);
var restcomp = statemanager.MakeStateTrackable(myComp);
//fails here - NHibernate.MappingException : No persister
for: Company
compMan.SaveOrUpdate(restcomp);
var mp2 = new ManagerPool();
var comp2 =
mp2.GetManager<CompanyManager>().GetAll().First();
Assert.AreEqual("1313 Webfoot Walk, Duckburg, Calisota",
comp2.Address);
}
I think it's hilarious. From everything my eyes tell me - myComp is a
perfect POCO Company :) and even for that matter, restcomp is a
perfect state manageble company..
Maybe there is a some difference I overlooked as it is quite late but
I know the code and the chances are slim. If I find anything wrong
I'll post :)
On Mar 5, 5:22 pm, José F. Romaniello <[email protected]> wrote:
> 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.- Hide quoted text -
>
> - Show quoted text -
--
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.