Want events to fire when a new object is created

Thread is closed for posting
3 posts, 1 answers
  1. AJ
    AJ avatar
    33 posts
    Member since:
    Jun 2011

    Posted 05 Apr 2013 Link to this post

    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
        private set;
    protected override void OnInit(EventArgs e)
        this.DbContext = ContextFactory.GetContext();
        this.DbContext.Events.Added += Events_Added;
    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. Answer
    Viktor Zhivkov
    Viktor Zhivkov avatar
    324 posts

    Posted 08 Apr 2013 Link to this post

    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
    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>();
    10.        base.SaveChanges(failureMode);
    12.        // refresh the entities in memory
    13.        this.Refresh(RefreshMode.OverwriteChangesFromStore, addedInvoices);
    14.        // use the invoice items as you like
    15.    }
    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...
    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.

    Viktor Zhivkov
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  3. AJ
    AJ avatar
    33 posts
    Member since:
    Jun 2011

    Posted 12 Apr 2013 Link to this post

    Thank you for your help. I'll have to really evaluate what the next best step would be, but I appreciate your assistance!
Back to Top