New feature: post-commit hooks

March 9, 2018 8:03 am Published by Leave your thoughts

In Starcounter 2.3.2 and 2.4 we’ve added post-commit hooks that let’s you specify a certain block of code to run after every transaction. This functionality is, for example, useful if you want to send out confirmation emails after you’ve created orders in the database:

using Starcounter;
 
[Database]
public class Order
{
    // Properties
}
 
public class Program
{
    static void Main()
    {
        Hook<Order>.AfterCommitInsert += (sender, id) =>
        {
            // Executes after you commit a new Order
            var order = Db.FromId<Order>(id);
            SendConfirmationEmail(order);
        };
 
        // Create a new order and trigger the hook
        Db.Transact(() => new Order());
    }
 
    private static void SendConfirmationEmail(Order order) 
    { 
        // Implementation for sending a confirmation email   
    }
}

With this, you’re guaranteed to send out a confirmation email after someone creates a new order. The hook is triggered no matter what app creates the order, as long as the app is in the same database, the app with the hooks runs, and the app has registered the hooks.

Read more about this in the documentation.

Categorised in:

This post was written by Erlend Landrö

Leave a Reply

Your email address will not be published. Required fields are marked *