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

Fire logic when new object is saved

3 Answers 97 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.
Robert Lautenbach
Top achievements
Rank 1
Robert Lautenbach asked on 29 Apr 2010, 03:47 PM
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.

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 30 Apr 2010, 06:38 PM
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.
0
Robert Lautenbach
Top achievements
Rank 1
answered on 24 Jun 2010, 09:58 PM
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?
0
Damyan Bogoev
Telerik team
answered on 29 Jun 2010, 06:57 PM
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
Tags
Development (API, general questions)
Asked by
Robert Lautenbach
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Robert Lautenbach
Top achievements
Rank 1
Share this question
or