Crash with object in BeginEdit followed by object's parent in BeginEdit

Crash with object in BeginEdit followed by object's parent in BeginEdit

Old forum URL: forums.lhotka.net/forums/t/5980.aspx


wmkew posted on Thursday, December 11, 2008

Hello,

We're using CSLA .NET 3.5.2, and we have the following code that produces a Csla.Core.UndoException as noted below in the code comment.  The exception message is, "Edit level mismatch in CopyState".  I'm pretty sure that we're simply misusing the framework in this case; my question is, would the exception be expected behavior?  Note that if we flip the order of the two BeginEdit() calls, the crash doesn't occur.  Any thoughts?  Thanks in advance!

- Whitney

using System;
using Csla;

namespace CslaTest
{
   [Serializable]
   class A : BusinessBase<A>
   {
      public readonly BCollection Coll = new BCollection();
   }

   [Serializable]
   class B : BusinessBase<B>
   {
      [NotUndoable]
      public readonly A TheParent = new A();

      public B()
      {
         TheParent.Coll.Add(this);
      }
   }

   class BCollection : BusinessListBase<BCollection, B>
   {
   }

   class Program
   {
      static void Main(string[] args)
      {
         B b = new B();
         b.BeginEdit();
         b.TheParent.BeginEdit(); // causes UndoException
      }
   }
}

ajj3085 replied on Thursday, December 11, 2008

Well, databinding has to be done in a very particular way, or it won't work.  The exception you're getting is to bring these errors to light, so that you can more easily find the problem.  You should never get the exception if you're databinding is setup properly... the order does matter.  See the ptracker, which should illistrate what the rules are.

Copyright (c) Marimer LLC