Fire logic when new object is saved

4 posts, 0 answers
  1. Robert Lautenbach
    Robert Lautenbach avatar
    18 posts
    Member since:
    Dec 2009

    Posted 29 Apr 2010 Link to this post

    Is it possible to execute some custom logic whenever a particular new persisted object is saved?

    For example, I have a bunch of persisted objects. One of them is a person object. Whenever I create and save a new person object I want to fire my custom logic to create some default related items for the new person.

    It looks like I can add some event code to fire before a save occurs. But I don't see anyway to fire after a save.
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 30 Apr 2010 Link to this post

    Hi Robert Lautenbach,

    You could use ITracking.Added event which occurs after an object was marked as persistent capable by the object context. The ITracking.Added event is being raised after the persistent object has been added to the IObjectContext:

    scope.Tracking.Added += new AddEventHandler(Tracking_Added);
      
    private static void Tracking_Added(object sender, AddEventArgs e)
    {
     ClassName addedObject = e.PersistentObject as ClassName;
     if (addedObject!= null)
     {
      //retrieving of the addedObject’s ObjectId
      IObjectId objectId = Database.GetObjectId(addedObject);
        
      Console.WriteLine("{0}. {1}", objectId, addedObject.Name);
     }
    }

    I think that will help you.



    Sincerely yours,
    Damyan Bogoev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Robert Lautenbach
    Robert Lautenbach avatar
    18 posts
    Member since:
    Dec 2009

    Posted 24 Jun 2010 Link to this post

    Does this mean that's there's no way to link to this event from within the persistent class itself?

    I basically want to have a PostStore event similar to PreStore so I can execute custom logic when a specific class is created and/or updated. Is there any way to accomplish this?
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 29 Jun 2010 Link to this post

    Hi Robert Lautenbach,

    You could use the Database.GetContext method to obtain the IObjectContext instance which manages the given persistence capable object:

    IObjectScope scope = Database.GetContext(this) as IObjectScope;

    After that you could use the proposed approach from the preceding answer.
    Hope that helps.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top