1

In working with Doctrine 2 I came across the following paragraph.

In fact, since Doctrine is aware of all your managed entities, when you call theflush() method, it calculates an overall changeset and executes the most efficient query/queries possible. For example, if you persist a total of 100 Product objects and then subsequently call flush(), Doctrine will create a single prepared statement and re-use it for each insert. This pattern is called Unit of Work, and it's used because it's fast and efficient

I am not up to speed on relational databases and ORM theory so I was wondering why is the Unit Of Work pattern considered fast and efficient?

Peter Mortensen
1,0452 gold badges12 silver badges15 bronze badges
asked Dec 16, 2011 at 11:08
0

2 Answers 2

5

One reason the Unit Of Work pattern can be efficient, as the paragraph states, is because it can batch several operations and reuse resources.

Using Unit of Work provides a context that can contain several operations into a single transaction that might otherwise be difficult to operate on together.

Also, not only can the Unit of Work use a single prepared statement for efficiency, but it may also be able to submit many inserts or updates to the database in a single call - if the database driver supports it - rather than executing each insert or update individually.

answered Dec 17, 2011 at 7:30
-1

I think that one uses the unit of work pattern with ORMs because it is a pattern that is easy to translate from your code to database transactions as both have the same logical boundaries. You can test your unit of work without a database.

Speed and efficiency is really an implementation detail.

answered Dec 17, 2011 at 17:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.