locked
Why unit of work is required RRS feed

  • Question

  • User88744855 posted

    would you mind to discuss the significance of unit of work pattern and its advantage. anyone can explain with small sample code the advantage of unit of work pattern. thanks

    Friday, February 6, 2015 2:33 AM

Answers

  • User-1611549905 posted

    The Unit of Work pattern is actually implemented by modern high-end ORMs such as Entity Framework and NHibernate themselves. These ORMs keep track of all the entities that you have added, deleted or modified, and then when you call DbContext.SaveChanges (Entity Framework) or ISession.Flush (NHibernate), persists them to the database.

    You sometimes see people wrapping Entity Framework or NHibernate in a class that implements an IUnitOfWork interface and consists of nothing more than a direct call into DbContext.SaveChanges/ISession.Flush. This isn't actually an example of the Unit of Work pattern but an abstraction layer around it. It is almost never necessary and usually only exists because That Is How We Do Things™.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 4:05 AM
  • User753101303 posted

    Hi,

    I would suggest http://rlacovara.blogspot.fr/2009/04/entity-framework-patterns-unit-of-work.html which seems a simple and straightforward explanation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 6:44 AM
  • User-760709272 posted

    The DBContext acts as a unit of work.  As you make changes it tracks these changes and when you Submit the changes they all get done as one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 9:36 AM

All replies

  • User-1611549905 posted

    The Unit of Work pattern is actually implemented by modern high-end ORMs such as Entity Framework and NHibernate themselves. These ORMs keep track of all the entities that you have added, deleted or modified, and then when you call DbContext.SaveChanges (Entity Framework) or ISession.Flush (NHibernate), persists them to the database.

    You sometimes see people wrapping Entity Framework or NHibernate in a class that implements an IUnitOfWork interface and consists of nothing more than a direct call into DbContext.SaveChanges/ISession.Flush. This isn't actually an example of the Unit of Work pattern but an abstraction layer around it. It is almost never necessary and usually only exists because That Is How We Do Things™.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 4:05 AM
  • User88744855 posted

    it would be better if u would explain the whole things with sample in such a way anyone can understand who never use unit of work before. thanks

    Friday, February 6, 2015 6:34 AM
  • User753101303 posted

    Hi,

    I would suggest http://rlacovara.blogspot.fr/2009/04/entity-framework-patterns-unit-of-work.html which seems a simple and straightforward explanation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 6:44 AM
  • User-760709272 posted

    it would be better if u would explain the whole things with sample in such a way anyone can understand who never use unit of work before. thanks

    He's telling you not to bother implementing Unit of Work if you use Entity Framework :)

    Friday, February 6, 2015 7:58 AM
  • User-1611549905 posted

    AidyF

    He's telling you not to bother implementing Unit of Work if you use Entity Framework :)

    No, I'm saying not to abstract UnitOfWork if you're using an ORM that implements it already.

    Friday, February 6, 2015 8:09 AM
  • User88744855 posted

    @AidyF: thanks for reply.

    Entity Framework has any built-in mechanism for unit of work ?

    briefly discuss what kind of purpose solved by unit of work ?

    thanks

    Friday, February 6, 2015 9:35 AM
  • User-760709272 posted

    The DBContext acts as a unit of work.  As you make changes it tracks these changes and when you Submit the changes they all get done as one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 9:36 AM
  • User753101303 posted

    This is pretty well explained IMO in the earlier article. They talk about the approach which is to send individual statements to perform each and every udpate separately one after the other and then the code that follows is using Entity Framework when you built an object graph for all changes and then save them as a single "unit" (it does the same thing behind  the Hood but it is abstracted away and done within as single transaction for you).

    So as noted by others, if using Entity Framework you already use the "unit of work" pattern.

    Friday, February 6, 2015 10:50 AM