Accessing original values in PreStore() during updates

4 posts, 0 answers
  1. Tom
    Tom avatar
    3 posts
    Member since:
    Nov 2007

    Posted 09 Nov 2009 Link to this post

    I have successfully implemented IInstanceCallbacks, and it is working perfectly for my logging purposes, but it would be nice to build a description based upon the fields that changed.  Is there a way to access the original values for each field in during the PreStore() and PreRemove()?
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 10 Nov 2009 Link to this post

    Hi Tom,

    You cannot access the old values of the changed fields in the PreStore() method. However you can subscribe to one of the IObjectScope events (in your case Changing) and there you can populate a list of changes that will be traversed later by the PreStore() method. For example if you need a to know the property that has been changed, the old and the new value you can have one helper class defined like this:
    public class HistoryHelperClass
        {
            private string propertyThatChanged;
     
            public string PropertyThatChanged
            {
                get { return propertyThatChanged; }
                set { propertyThatChanged = value; }
            }
            private object oldValue;
     
            public object OldValue
            {
                get { return oldValue; }
                set { oldValue = value; }
            }
            private object newValue;
     
            public object NewValue
            {
                get { return newValue; }
                set { newValue = value; }
            }
        }
    Now in your program you can subscribe to the changing event and implement a simple logic like this:
    public static List<HistoryHelperClass> listOfChanges = new List<HistoryHelperClass>();
           static void Main(string[] args)
           {
               IObjectScope scope = ObjectScopeProvider1.ObjectScope();
               scope.Tracking.Changing += new ChangeEventHandler(Tracking_Changing);
               scope.Transaction.Begin();
               //some logic performed
               scope.Transaction.Commit();
            
           }
     
          static  void Tracking_Changing(object sender, ChangeEventArgs e)
           {
               listOfChanges.Add(new HistoryHelperClass { PropertyThatChanged = e.FieldName, OldValue = e.OldValue, NewValue = e.NewValue });
           }
    Now you will have a list filled with the changes you have made during your transaction. Later when you call Commit() you can traverse that list and extract those changes. When you are done you should clear the list at the end of the PreStore() method.

    Greetings,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Tom
    Tom avatar
    3 posts
    Member since:
    Nov 2007

    Posted 10 Nov 2009 Link to this post

    Petar,

    I understand hooking the Tracking_Changing and building the list of changes, but I am a little unclear as to when I would traverse the list.  It doesn't appear to be available in the PreStore methods of any persistent classes that have been changed.
  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 13 Nov 2009 Link to this post

    Hello Tom,

    You will need to apply the correct access modifiers for your list (depending on the project setup). If you have defined your list as static you should be able to access it via the [name of the class in which the list is defined].[the name of the list].
    If you are still facing difficulties please contact us and we will prepare a sample project for you.

    Greetings,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top