Using separate user connection with UnitofWork


Unit of Work is a great capability in Microsoft Dynamics AX 2012 to reduce chattiness between AOS and SQL server. A high level overview of UoW is available in this informative blog post: http://dynamics-ax.blogspot.com/2011/07/microsoft-dynamics-ax-2012-unitofwork.html.

One of the cool things about unit of work (UoW) is the capability to use a custom user connection. If you would like a set of DML operations to be executed on a separate user connection then that is possible with UoW. If you have legacy code that uses RecordInsertList and want to use a custom user connection then you should rewrite the code using UoW.

Here is an example of how to use UoW with a custom user connection:

static void UoWTest(Args _args)
{          
    Table1 table1;
   
    // Define a new user connection
    UserConnection uc= new UserConnection();
   
    // Define a new UoW
    unitOfWork uofw = new UnitofWork();        
           
    // Add a table buffer to insert on save changes in the UoW
    table1.Field1 = 'foo';
    uofw.insertonSaveChanges(table1);           
   
    // Perform additional interactions that you 
    // want to club together with the UoW
    // by calling insertonSaveChanges, updateonSaveChanges
    // and deleteonSaveChanges 
   
    ...

    // Finally pass the user connection to
    //the UoW to use on SaveChanges
    uofw.saveChanges(uc);
}

Skip to main content