Comments (3)
A big difference in how we handle this, is that we always use an aggreate root id for our read model.
So in you example, the basketId
would be returned by getId()
.
I think that would work for a lot of cases, but maybe you have a use case where that wouldn't work?
from broadway.
@wjzijderveld Yep that could work in situations, but not in this particular example (the last paragraph in my post).
Again, this is a contrived example and based off an example Mathias used in a presentation (I was doing his presentation examples using Broadway so that I could show someone a concrete example of an implementation, and have the presentation for them to follow along with as well), but I could see examples of where that could fail in a real world example.
In this example above, it's just dropping in basketId, productId, and productName, which could be the exact same for multiple rows in the read model. That basketId would be required for multiple rows (some which have the same productId and productName in it) so the uniqueness doesn't really come into play there and a separate id is needed. Otherwise, the InMemory repository would only store one object with the basket/product/name combo (again, assuming no quantity or other distinguishing attribute in this case).
When I was first building the example, the ProductWasAdded and results were actually ok. This was because $id was null, the save() function just used null as the id, and the repository was still able to store and return the object to me, which matched my test because I was also constructing the object to compare against and it had a null id.
The problem showed itself when I tried to execute the exact same event in when() clauses more than once. Because of the id situation, the id was null and the repo overwrote the first object with the second upon storage, so the then() failed to match what I expected (two objects, same attributes).
It was when I went to make each read model object generate a unique id to mitigate that situation that I realized I couldn't then duplicate the object in my test because I wouldn't know the id.
from broadway.
In my opinion you shouldn't generate the ID from inside the read model, and exactly for this reason, that it gets a lot harder to test.
In your example I would still inject an unique id for that item. To me it already seems weird that a read model isn't unique on itself. In this case I would name the read model something like BasketItem
and inject an extra basketItemId
that would become the id that gets returned by getId
.
from broadway.
Related Issues (20)
- drop test/Broadway/TestCase
- make PHPUnit a dependency HOT 5
- test the test helpers with examples HOT 2
- Built-in asynchronous way to run processors? HOT 3
- Example/explanation on event sourcing for aggregate roots HOT 1
- What is the overall state of the project? Is it GDPR ready? HOT 2
- Simple Command Bus - Manage Throwable exceptions
- Could you do a new release? HOT 2
- Are there any plans to update broadway? HOT 1
- Asymmetry between EventBus interface and EventListener interface HOT 2
- Broadway does not seem to survive hot upgrades HOT 2
- duck-typing vs interface HOT 1
- Replaying events to rebuild elastic search index HOT 1
- PHP 8 support HOT 2
- Processor after projector HOT 2
- Aggregator HOT 3
- Indirect development dependency used in src ConcurrencyConflictResolvers
- no recent tags? HOT 1
- Get uncommitted events without empty the aggregate HOT 4
- Give serializers flexibility to map based on event type
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from broadway.