Replies: 1 comment 1 reply
-
I can't tell which one you should use. Solution depends on your business requirements, so you probably should ask your product owner what registration flow you need (or decide it yourself if it's your personal app).
What's exactly the problem here? You should be able to update foreign keys in a transaction without any problems if you insert company first then foreign key.
Your controller doesn't have to generate an ID. You can just call a static method on an aggregate which will generate an id, then you just use it, something like: const company = Company.create(); // <- creates id in a company
const companyId = company.id; But you will probably have to create a service for it, don't make your HTTP controllers execute your business logic. |
Beta Was this translation helpful? Give feedback.
-
Hello, first of all, thanks for the awesome work!
I reviewed many discussions but didn't find my case, and I would like to hear other opinions about what would be the best. I will describe my case as best as I can also with solutions that I considered. I'm looking for a solution that will follow DDD.
I have agreed that the User and Company,
companyId
column as FK to the company)The project allows the registration of new user
Solutions that I considered/tried
1. Company created by Owner
- Once the Owner logs in, they can fill in data about the company, and the company is created
- Event about the created company is emitted together with owner ID
- The user module has a domain event handler for the creation of the new company and will update the foreign_key that references the company (here I have the problem that I cannot update FK using a company ID that isn't committed, as I'm in the transaction)
2. Creating a company once the new Owner is registered
- once the new user (owner) is registered, automatically create a company too
- Company ID is generated inside the HTTP controller for registration (this is what I don't like about this solution, static method create should generate the ID)
3. Registering company
- registration will create a company
- creation of a company will trigger an event that will have all the data about the owner
- The User aggregate is created based on the event "CompanyCreated", the event has all the data about the user (chosen password, etc)
So that's the solution that I considered, wdyt?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions