This is a migrated thread and some comments may be shown as answers.

Accessing original values in PreStore() during updates

3 Answers 50 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tom
Top achievements
Rank 1
Tom asked on 10 Nov 2009, 05:47 AM
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()?

3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 10 Nov 2009, 11:18 AM
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.
0
Tom
Top achievements
Rank 1
answered on 11 Nov 2009, 05:17 AM
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.
0
PetarP
Telerik team
answered on 13 Nov 2009, 10:51 AM
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.
Tags
Development (API, general questions)
Asked by
Tom
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Tom
Top achievements
Rank 1
Share this question
or