Wednesday, February 1, 2012

unif of work framework in AX

One of the most common design patterns in enterprise software development is the Unit of Work, According to Martin Fowler, the Unit of Work pattern

·       Keeps track of the objects affected by a transaction
·       Coordinates the writing out of changes
·       Resolve the concurrency problem 
A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

In x++, there is a system class exists that can be used to implement the unit of work framework some of the details are as follows
UnitofWork class,
some important methods
Clears all the changes
Deletes the records on commit/saveChanges
inserts the records on commit/saveChanges
updates the records on commit/saveChange
This method will hit the server one time to commit the changes
 
I would like to take an example to demonstrate the use of the UoW framework,
Example 1:
If there is a need to inserts data in a bulk to Table1.
there is also a call tJournalHeader.insert() needed in the for loop



using Unit of work


Example2:
What if we are doing bulk insert of a journal with many lines and the journal header contains some totals calculated based on its lines. It is impossible to insert lines before header since the value of the journal's surrogate key is unknown. But that would be so convenient because otherwise the header should be inserted first, then the lines and then the header should be updated with the calculated totals



AX client has the great support of the unit of work as well. This means that form datasources can be grouped into a unit of work, so all the benefits of simultaneous commit and surrogate key propagation will be available on forms. There are a lot of possibilities and tricks there, so this topic worth one or more dedicated posts as well.

No comments:

Post a Comment