The Unit of Work Design Pattern Explained

preview_player
Показать описание
In today's video, I'll explain the Unit of Work design pattern, a crucial concept for anyone who regularly interacts with databases. This pattern plays a key role by accumulating all transactions and executing them collectively. But why is this necessary? Find out this video.

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

Social channels:

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean

🔖 Chapters:
0:00 Intro
0:41 What is the Unit of Work Pattern?
3:53 The Session Object as a Unit of Work
9:44 Benefits of Using the Unit of Work Pattern
11:43 Outro

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

Interessant, ik heb ORM's gebruikt bijvoorbeeld en alhoewel ik nog niet bekend was met de onderliggende patronen, herken ik een hoop terug doordat ik deze packages ooit heb gebruikt.

NewQuietBear
Автор

I liked your calm and composed style of explaining a complex topic.Well done, your 3rd example made the most sense to me in terms of rollbacks scenario - IaC setup seems like a good place to use this design pattern. Thanks, Arjan!

iamkaransethi
Автор

You asked about other ideas for use of the unit of work..one thing that came to mind was installing programs. All program installations seem to be able to roll back everything if the installation fails for some reason.

inhahe
Автор

Session has method/contextmanager "begin" for control transaction.
SQLAlchemy has special mixin for using as dataclass "MappedAsDataclass" (for __str__/__repr__)

avazart
Автор

Unit of work is used in Guaranteed messaging between systems and applications (messaging middleware). send message and ack that sort of thing. Redis also has batch functions to perform several ops at once.

spanomatic
Автор

Would you consider doing a Saga Pattern video? Also, can you demonstrate using Saga and Unit of Work together to roll back a failed Saga?

uruzrune
Автор

Please take a look at the echo=True option to the create_engine function. This will ask sqlalchemy to output db interaction info including the sql statements being executed and more ...

Another great video. The UnitOfWork and Repository patterns are critical to building resilient, reliable persistence layers - and not just with db, but also filesystem, web service APIs, etc. !

klmcwhirter
Автор

Excellent video! I had never heard of this, gives me good ideas how to change our code!

hcubill
Автор

can you please cover django concepts as well?

aashayamballi
Автор

My favourite ORM for PHP (sorry) uses UOW... so much nicer than those other Active Record ORMs. I did an extension that logged database changes to.... the database by scanning and extending the UOW on a commit... I had Python friends (I was doing JS) at my old job who used SQL Alchemy and I was always curious as to what was inside... now I really want to try it. UOW is my favorite underrated pattern - "boo!" to Active Record.

edgeeffect
Автор

As always thanks, could you maybe do one on django?

jamesclarke
Автор

Thank you....would be ideal scenario for embedded DBs (DuckDB, targeting scientific workloads, with specific pythonic libraries)

hubstrangers
Автор

I used to work for a PoS company and I accidentally implemented Unit of Work pattern without knowing it existed :) mine was a bit janky though, a dict of bools that was tracked between methods to know from which step we had to rollback

obsidiansiriusblackheart
Автор

Do a coderoast of the nanogpt repo of Andrej Karpathy

BuFuO
Автор

Do I need a different implementation of this pattern for each use-case in my domain?

StarLord
Автор

This looks extremely similar to using an event queue. Throughout the lifetime of your program, you add events to a queue, and you delay their execution to a point where it is convenient (for any reason depending on context, in a game you might want to apply some filtering to change the order of events and process some before others), so it gives you full control on the order of execution. But, it can also help you to see which events failed, and then do the appropriate rollback.

EDIT: On second thought, it is an event queue, but used in a different context with (potentially) different intentions.

lucianop.