Unit of Work

Hi all.

Unit of Work is a new framework , which is being introduced in AX 2012.
Now let us explore what is need of having this framework  :

Suppose we do have Two tables , TableHeader and TableLine.
Now we want to insert bulk data in these two tables , TableLine is linked with TableHeader by RecID.

So first we need to insert the data in TableHeader and then pass the recid of this table to Tableline and insert the record. So there are 2 major issue is : We need to hold recId of Parent table and pass to Tableline and we have to hit database with each insert.

Now Unit of Work framework come into picture and helps us , we dont have to hold the recid of parent table and also we can insert all data in a single trip . How … Let us Implement this :

1.Create 2 table SKU_Table and SKU_Line.
2.SKU_Line is related to SKU_Table by RecId.
3.Go to relation of table and make sure property CreateNavigationPropertyMethods = true
by making this property true , system will add appropriate methods to table SKU_Line
4 . Now we are ready to go

public void Insert()
{
    SKU_Table                       SKU_Table;
    SKU_Line                        SKU_Line;      
    UnitofWork                      UnitofWork;
 
    UnitofWork                      =  new UnitofWork();
    while select aaaaa
    {
     //insert data in parent table
        SKU_Table.clear();   
        SKU_Table.somefield                 = cccc;      
        UnitofWork.insertonSaveChanges(SKU_Table);
   
        while select bbbb
        {
               //insert data in child table
            SKU_Line.clear();
            SKU_Line.somefield                 = ddddd;
            SKU_Line.SKU_Table(SKU_Table);
            UnitofWork.insertonSaveChanges(SKU_Line);
        }      
    }
//Now insert data in a single trip
    UnitofWork.saveChanges();
}

Leave a Comment

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