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!

Example:
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.

39 comments:

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

    ReplyDelete
  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?
    Regards
    Akash

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    my blog post - homepage

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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: http://sswlink.spb.ru/

    ReplyDelete
  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://Internetsocial.cat

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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 everythinggreyhoundsblog.blogspot.co.uk

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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 pnuac.com

    ReplyDelete
  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 - spirit-of-rock.com

    ReplyDelete
  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 []

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

    ReplyDelete
  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

    ReplyDelete
  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?

    ReplyDelete