Recursive change tracking

4 posts, 0 answers
  1. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 22 Sep 2009 Link to this post

    I want to set a LastUpdated property on my objects whenever they change but when I do this using the change tracking api, I end up with recursive calls that eventually cause a stack overflow.  Is there a way to temporarily turn off change tracking or some other way to accomplish what I want to do?  Here's a look at the code that doesn't work.

            static void Tracking_Changed(object sender, ChangeEventArgs e) 
            { 
                IDomainObject obj = e.PersistentObject as IDomainObject; 
                if (obj != null
                { 
                    obj.LastUpdated = DateTime.Now; 
                } 
            } 

  2. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 23 Sep 2009 Link to this post

    Hi Ben Friedman,

    There are few possible solutions to your problem. In order to find the right one, we would require a bit more information on what are you trying to achieve. Are you going to use this value for concurrency control or is it just for information? Are you intending to have this value set whenever a property is changed inside a transaction, or do you want to have the value of the time when the object was last updated in the database?

    We are looking forward for your next ticket. Thanks for the collaboration.

    Greetings,
    Zoran
    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. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 23 Sep 2009 Link to this post

    Hi Zoran

    This value will just be for informational purposes and it really doesn't matter whether it reflects the time at which the property was changed or when the database was updated.  Which ever solution is easiest, though it would be nice to see how to do both.

    Thanks!
  5. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 25 Sep 2009 Link to this post

    Hello Ben Friedman,

    In this case i would suggest you some of the best practices for the purpose. You should implement the IInstanceCallbacks interface on the persistent class that you want to keep track of. One of the IInstanceCallbacks members is the PreStore() method.

    This method is called just before an object gets stored into the database. There you can check if an object has been updated and apply your logic.
        public void PreStore() 
            { 
                IObjectContext scope = Database.GetContext(this); 
                if (scope.IsDirty(this)) 
                { 
                    this.LastUpdated = DateTime.Now; 
                } 
            } 


    Regards,
    Zoran
    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