-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 6, 7: Is it good to let UoW hold specified repo? #18
Comments
well, as Martin Fowler said https://www.martinfowler.com/bliki/DDD_Aggregate.html: So transaction of one aggregate(accessed by repo) should be operated in one uow session, it make sense to specify an aggregate root model to a uow do avoid crossing aggregate boundary. (as the fake code above, two aggregates order and customer are put in one uow session, it's not good if we are ok about the design of each aggregate.) I'm just thinking about avoiding my own project from over design, from some declaretive db model classes to lots of model classes, repo classes and uow classes. Any way it's a good start to think in ddd way. |
this is a good point actually. since you shouldn't be making changes to two different aggregates within the same unit of work, maybe it doesn't make sense to have multiple repositories available on the i suspect we mostly do it for convenience. having a different UoW class for each repo would feel overcomplicated, bearing in mind they would be the same in all other respects... |
incidentally, i'm curious about what you're putting in |
This is the answer. It's simple to get the repositories off the Unit Of Work, having started the transaction. I've worked on systems where the UOW and repositories were completely disconnected from a client perspective, but you still need to wire them up somehow, so it just means more magic.
No, a unit of work is just a bunch of things that have to happen together. It's not specific to any one aggregate.
Here we're modifying two aggregates though, right? You probably want something like
|
by now, it's just a simple protocol says "we need uow in our service":
I use explicit injection to let the uow to hold repo, there are 2 questions(Q1, Q2) in following details:
the test:
I think a pure UoW need to hold and trace modified things, so if we don't have sqlalchemy session, it's necessary to put things we changed in to an UoW (like uow._added_repos, uow._updated_repos), but it's not necessary to let service layer access repository strictly via UoW. Another similar thread: https://softwareengineering.stackexchange.com/questions/406729/is-unit-of-work-pattern-really-needed-with-repository-pattern I‘ll focus on the usage of uow about domain event. |
I incline to this, for me it's enough to see sqlalchemy session (commit, flush, rollback) as uow, about transaction.
Yes, this point is clear with domain event |
If you like. It really doesn't matter. What's important is the principles: each piece has one job, our interesting logic lives in the domain model, and the domain model is kept pure of infrastructural concerns.
Why would you need to use an object after you've finished the unit of work in which you loaded it? By definition, it's not in use any more. That aside, you can change this behaviour by using a different session maker. That's all the flask-alchemy library is doing IIRC, it's storing a session on the web request so that it can be used throughout the request lifecycle. |
@bobthemighty thanks, these won't be big problems, I take time to make basic conception more clear. |
It seems that the service layer should play with repos(based on aggregation root), and use uow to deal with transactions, looks like: https://stackoverflow.com/questions/9808577/multiple-generic-repositories-in-unitofwork).
Let's say we have a use case that one customer in a biz organization need to confirm his/her purchase order, which will change the order status then the product supplier will send product to the customer with express, also record the total cost of the customer (may be by sending an event message to user management domain)
Suppose we have "customer" repo with models of "user", "organization", "customer" aggregated, and "purchase_order" repo with models of "sku", "customer", "supplier", or may be an "order" model with some value objects such as "shipping address" aggregated.
We do not need to create "customer uow", "purchase order uow" right? Should we just do:
The text was updated successfully, but these errors were encountered: