Wednesday, May 4, 2011

Unit of Work

After the post about surrogate keys I was asked the following question:
There is the following problem with using surrogate keys.
A record gets RecId value at the moment it is inserted into the database. 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.

The answer is – use Unit of Work. It allows to perform create, update and delete operations without worrying about the order of those and without a need to specify surrogate key values. It will all be done automatically in the kernel!

Let we have the following tables:

The following job will insert the journal header with calculated totals and 10 lines. Note, that there is no place where surrogate keys are mentioned in the code. Business data only.

And here is the result.

Some more details about the unit of work feature:
  1. All database operations happen only when saveChanges() method is invoked.
  2. UnitOfWork class has insertOnSaveChanges(), updateOnSaveChanges() and deleteOnSaveChanges() methods for CUD operations.
  3. Surrogate keys can be propagated automatically to related records if buffers are linked via navigation methods (AJournalLine.header() method in the example). Navigation methods can be created automatically from relations. I’ll write a separate post about them.

And the last but not least. 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.


  1. This is great, got to try it, thanks for sharing :)

  2. Hi,
    This is really helpful. Thanks a lot.
    have you written that other blog about " Propagating Surrogate keys can be propagated automatically to related records "
    Can you please point me to some article to understand the same?

  3. Xbox Live comes in two flavors, Silver and Gold, but regardless of which
    you choose, you can integrate it with Windows Live Messenger.
    play and compete with different gamers around the world.
    The points do come pretty fast but you do need to work to get
    your points.

    Visit my site; free microsoft points
    Also see my web page > free microsoft points

  4. These are in fact fantastic ideas in about blogging.

    You have touched some nice points here. Any way keep up wrinting.

    Also visit my blog post - greencoffeecoupons

  5. You really make it seem so easy with your presentation but I find this topic to be actually something that I
    think I would never understand. It seems too complicated
    and very broad for me. I am looking forward for your
    next post, I'll try to get the hang of it!

    Also visit my website - website

  6. An outstanding share! I have just forwarded this onto a coworker who has been conducting a little homework on this.

    And he in fact ordered me lunch due to the fact that I stumbled upon
    it for him... lol. So allow me to reword this.... Thank YOU for the
    meal!! But yeah, thanx for spending time to talk about this matter here on your site.

    Here is my weblog buy a house

  7. I get pleasure from, cause I found just what I was taking a look for.

    You've ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

    Look at my web blog; my Latest blog post

  8. If you are going for best contents like me, simply visit this web
    site daily for the reason that it offers quality contents, thanks

    Also visit my web site: her latest blog

  9. Constipation, too much bodyweight, sitting a long time in one
    position, aging etc. First to provide relief and begin shrinking the hemorrhoids you will
    want to start with sitz bath and witch hazel.
    Natural ingredients such as witch hazel, stone root, aloe
    vera and butchers broom may also be used.

    Also visit my weblog; yoga asanas for piles treatment

  10. Howdy! I could have sworn I've been to this web site before but after looking at many of the posts I realized it's new to me.
    Anyhow, I'm certainly delighted I discovered it and I'll be book-marking it and checking back often!

    Also visit my webpage - learn more

  11. Appreciation to my father who told me on the topic of this
    weblog, this weblog is truly amazing.

    my blog post - homepage

  12. Wonderful work! This is the kind of information that should be shared across the internet.
    Disgrace on the search engines for not positioning this post upper!
    Come on over and talk over with my web site . Thank you =)

    my webpage - learn more

  13. In fact no matter if someone doesn't be aware of afterward its up to other visitors that they will help, so here it takes place.

    My site :: Buy Flats

  14. When I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on every time a
    comment is added I get four emails with the same comment.
    Perhaps there is a means you can remove me from that service?
    Thanks a lot!

    My web blog: properties

  15. Hello, its fastidious paragraph concerning media
    print, we all understand media is a enormous source of information.

    Also visit my homepage; neighborhood criminal records

  16. Thanks for ones marvelous posting! I definitely enjoyed reading it, you will be a great author.
    I will be sure to bookmark your blog and will often come back in the foreseeable future.
    I want to encourage you continue your great writing, have a nice day!

    Have a look at my blog; click

  17. The child support is intentionally for the children's education, clothing, food, school supplies, and other necessities the youngster should live by. A legal situation regarding custody, divorce, divorce, and other family related legal issues may maybe not be as easy because it seems. Additionally, carrying out research on claims, he has in fact dealt with and that are similar to your lawsuit would be very beneficial in finding one who is good enough and dependable.

    Feel free to visit my website; arizona family lawyers assistance program Flap

  18. Good answers in return of this question with real arguments and describing the
    whole thing on the topic of that.

    Here is my web-site - found it for You

  19. If you wish for to improve your know-how simply keep visiting this web site and be updated with the most up-to-date gossip posted here.

    my webpage - flat

  20. Hi thеrе to every sіngle one, it's actually a nice for me to pay a quick visit this website, it consists of important Information.

    my weblog; Payday Loans

  21. Why visitors still use to read news papers when in this technological world everything
    is presented on net?

    my blog post - use this link

  22. fantastic post, very informative. I ponder why the opposite experts of this sector don't realize this. You must continue your writing. I am sure, you've a great readers' base already!

    Also visit my site :: straight from the source

  23. Нello! Woulԁ you mind іf I shаrе yοur blοg
    with my myspаcе group? There's a lot of folks that I think would really enjoy your content. Please let me know. Cheers

    My blog post:

  24. Link exchange is nothing else except it is only placing
    the other person's webpage link on your page at suitable place and other person will also do similar in support of you.

    Stop by my website HTTP://

  25. hi!,I like уour writing ѕo so much! percentаge we kеep
    up a corгeѕpondеnce more appгoxіmаtely yοur ρoѕt on AOL?
    I need an expert on this ѕpace to solve my problem.
    Maу bе that is you! Taking а lοok aheaԁ tо peer you.

    Feel fгee to ѕurf to my homеpage - Same Day Payday Loans

  26. Simply want to say your article is as astounding.
    The clearness for your publish is simply cool and
    i can suppose you're an expert in this subject. Well along with your permission let me to seize your feed to stay updated with impending post. Thanks 1,000,000 and please continue the gratifying work.

    Here is my homepage: home equity loans Ottawa

  27. Hello There. I discovered your weblog the usage of msn.
    That is a very smartly written article. I will be sure to bookmark it and come back to learn more of your useful info.
    Thank you for the post. I will definitely return.

    my blog

  28. I like the helpful information you supply for your articles.
    I will bookmark your blog and test again right here frequently.

    I'm slightly certain I will be informed a lot of new stuff proper here! Best of luck for the next!

    Here is my site ... Look At This

  29. Thanks a lot for sharing this with all of us you really realize what you're speaking about! Bookmarked. Kindly also visit my web site =). We will have a link exchange arrangement between us

    Also visit my web site :: read more books

  30. If you are going for best contents like myself, simply visit
    this web site everyday because it offers quality contents, thanks

    My homepage ... marriage help for women

  31. Nice post. I was checking constantly this
    blog and I'm impressed! Very helpful information particularly the last part :) I care for such information a lot. I was looking for this particular info for a long time. Thank you and good luck.

    Stop by my webpage; Find Love Online

  32. One of the simplest ways to prevent a hazardous scenario is to not
    get while in the risky circumstance. Which is acceptable,
    League of Legends is actually a hundred% cost-free.
    The tribunal would be the "justice system" that League of Legends takes
    advantage of.

    Also visit my weblog ... league of legends hack

  33. This explains the dimpled and uneven, causing the skin to
    bulge out in some places too. So, make a routine of exercises to get
    a fresh, silky-Cellulite Velashape. However there are those who have smooth and silky.

    My homepage - get more info

  34. Colorful fruits and vegetables, such as berries, citrus fruits, the acid in grapefruit juice can help you
    get rid of their cellulitis jaw.

    Here is my web-site

  35. I wanted to thanκ yοu fог this goοd reaԁ!
    ! I definitelу еnϳoyed every bіt of it.
    I've got you book marked to look at new things you post…

    My homepage -

  36. Post-it,rapide agenda avec Voyage � travers permet free code xbox live 2012.
    tricot pour continu� download xbox live arcade demos.

    Curieux,chubby m�decin contre tourbillon pour avancement download
    xbox 360 controller driver for mac. geler de devant synchroniseur;
    comment avoir le xbox live gratuit a vie.Utile,stagnante destruction � travers d�pouiller d'ailleurs v�tements xbox live bio art creator online. Coup d' contre flatteur,
    free download xbox 360 game saves.

    My website: code pour xbox 360 live gold gratuit []

  37. 是 3 對 3 社交聯誼服務. 能幫助您找到有趣的新朋友. 您邀請兩個好朋友, 就可以和另外三個有趣的新朋友出去玩. 為您找到有類似興趣, 有同樣話題, 並且有趣的新朋友, 是我們的使命.

  38. This is axapta ERP blog for Technical and functional fields and includes Microsoft Dynamics Axapta tutorials and Dynamics Axapta Coverage. This blog also contains x++ code help for Ax developer and solution of technical and functional daily issues. This blog is specific for Microsoft dynamics programming. Enterprise portal, SharePoint services, business connectors and Enterprise Resource Planning applications and sql database.It will help to get Microsoft Business Solutions.

    axapta ERP blog

  39. Hi,
    For example if I have 3 tables A,B,C .A and B related thru foreign key relationship.And B and C related thru foreign key relationship.Will unit of work class work in this scenario?

  40. Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it. What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity!!

  41. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
    right away...

    (VIDEO) Win your ex back with TEXT messages?

  42. Very well done. Absolutely brilliant information. I'm in love with this blog. they always provide such a great information. Dynamics AX Support Australia

  43. Thanks for sharing such an informative post