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

Want events to fire when a new object is created

2 Answers 37 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.
AJ
Top achievements
Rank 2
AJ asked on 05 Apr 2013, 09:00 PM
I'm using the method described here to manage my OpenAccessContext (also implementing a BasePage).

Basically, I want to perform logic whenever certain objects are created. For testing purposes, I tried something extremely simple:

protected DbDataContext DbContext
{
    get;
    private set;
}
 
 
protected override void OnInit(EventArgs e)
{
    this.DbContext = ContextFactory.GetContext();
    this.DbContext.Events.Added += Events_Added;
    base.OnInit(e);
}
 
public void Events_Added(object sender, Telerik.OpenAccess.AddEventArgs e)
{
    const string SourceName = "Jarvis";
 
    if (!EventLog.SourceExists(SourceName))
        EventLog.CreateEventSource(SourceName, "Application");
 
    EventLog.WriteEntry(SourceName, "object added!!!", EventLogEntryType.SuccessAudit);
}

Needless to say, nothing was written to the Event Log.

To give you an example scenario of what I want to do:

I have an object named Invoice. I want an email to be fired to a rep whenever an Invoice is created. The problem is that an Invoice can be created in multiple ways, so I'd really prefer not to add logic to every page that creates an Invoice. Rather, I was hoping, I could utilize the Context Events and just check if the object being created is an Invoice and, if so, do any logic.

If there's a better way to do this or if there simply isn't a way to do this, let me know. Either way, thank you for your time!

 --- Andrew

2 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Zhivkov
Telerik team
answered on 08 Apr 2013, 01:05 PM
Hello Andrew,

There are a few events in OpenAccessContext.Events that can be used to implement the feature that you want, but I am not sure that any of them will be really helpful. They operate on data that will be potentially persisted in the database. You can always create a new context and ignore the existing one or just call ClearChanges. Having that in mind I will not use Added for this purpose at least without introducing some smart filtering of events and safeguards.

From my point of view the only good place to hook is OpenAccessContext.SaveChanges(). If you want to have the unique identifier of the invoice and it is generated by the database that you should raise the event immediately after SaveChanges() is called so the IDs will have their real values.
You can implement that extending your OpenAccessContext in the following way:
01.public partial class EntitiesModel
02.{
03.    public override void SaveChanges(ConcurrencyConflictsProcessingMode failureMode)
04.    {
05.        // get a snapshot of the current changes to be persisted
06.        var changeSet = this.GetChanges();
07.        // get all newly added invoices
08.        var addedInvoices = changeSet.GetInserts<Invoice>();
09. 
10.        base.SaveChanges(failureMode);
11.         
12.        // refresh the entities in memory
13.        this.Refresh(RefreshMode.OverwriteChangesFromStore, addedInvoices);
14.        // use the invoice items as you like
15.    }
16.}
Please note that there will be some overhead cause by the creation of the change set snapshot each time SaveChanges() is called. You should weight this against the option to have the business logic in all pages where Invoices are created.

Otherwise OpenAccessContext.Events.Added should be called after a new entity instance is added to the context for the first time:
1.var context = new EntitiesModel();
2.Invoice invoice = new Invoice(); // Added is not called yet!
3.// set some properties to the new instance...
4.context.Add(invoice);
5.// context.Events.Added is raised

If you are adding the Invoice to a related entity instance (acting as child in One-to-Many relationship), make sure that IsManaged setting of the association is set to True. Otherwise the event will not fire in this case.

If you suspect that Adding/Added events are not raised as they should be, please send us a code snippet that demonstrates the behaviour.

Regards,
Viktor Zhivkov
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
0
AJ
Top achievements
Rank 2
answered on 12 Apr 2013, 01:32 PM
Thank you for your help. I'll have to really evaluate what the next best step would be, but I appreciate your assistance!
Tags
Development (API, general questions)
Asked by
AJ
Top achievements
Rank 2
Answers by
Viktor Zhivkov
Telerik team
AJ
Top achievements
Rank 2
Share this question
or