Answer the question
In order to leave comments, you need to log in
How does the Unit of Work approach work?
Good time everyone, I would like to understand the principle of operation and the structure of the UoW pattern. If possible, explain in code, preferably in php in a miniature and very primitive example
Answer the question
In order to leave comments, you need to log in
Unit of Work is a pattern that defines a logical transaction i.e. atomic synchronization of changes in objects placed in the UoW object with storage (database).
If we turn to the original description of this pattern by Martin Fowler, we can see that the object that implements this pattern is responsible for accumulating information about which objects are included in the transaction and what are their changes relative to the original values in the storage. The main work is done in the method commit()
that is responsible for calculating changes in the objects stored in UoW and synchronizing these changes with the storage (database).
The Unit of Work pattern is usually not completely independent, it is usually closely related to the Identity Map pattern., whose task is to maintain a map of the created objects taken from the store in order to ensure that one piece of information from the store is represented by exactly one instance of the data object in the application. This avoids change conflicts. does not allow the situation when two objects representing the same data item in the store are changed differently. Information from the Identity Map is used in the commit()
Unit of Work pattern method to calculate the difference between the original data and the accumulated changes.
Since in order to calculate the difference (and, accordingly, determine what and how should be changed in the storage) it is necessary to know what data and how it is stored in objects - as a rule, the implementation of the Metadata Mapping pattern is also required., which describes the relationship between the contents of the store (for example, tables and columns of the database) and classes / properties of objects.
Also, if the data in the storage is not independent (for example, relationships between tables in a database), it may be necessary to implement a number of patterns responsible for storing information about relationships between data (these are the patterns of the Object-Relational Structural Patterns section in the patterns catalog ).
To summarize: Unit of Work itself is quite simple in its external interface, but making it work correctly requires providing a lot of additional data, so I can’t give miniature examples.
If we talk about PHP, then the best implementation of these patterns in PHP is definitely Doctrine ORM. In particular, in the Working with Objects section of the Doctrine documentation, you can find a good description and many examples of using the patterns described above.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question