Models tend to be normalized -‐> Denormaliza@on is the black art of RDBMS • DBAs not involved in the domain model design • Extra work required to interact with individual en@@es managed by aggregates Order Customer OrderLine1 Insert Order OrderLine2 OrderLine3 OrderLine4 OrderLine5 Insert OrderLine Insert OrderLine Insert OrderLine Insert OrderLine Insert OrderLine
OrderLine1 OrderLine2 OrderLine3 OrderLine4 OrderLine5 • MongoDB’s document orienta@on eliminates the need for denormaliza@on • Complex object graphs s@ll result in a single read or write to the database
En66es State modifica@on logic Repositories CRUD, Simple Queries, Schema Versioning Services Complex behavior related to mul@ple en@@es or other systems Where does the behavior go?
-‐ Has the MongoDB ObjectId -‐ Defines the type -‐ Schema Version -‐ Created and Updated TimeStamps -‐ Created and Updated User Ids -‐ Aggregates the domain model en@ty
domain model aggregate -‐> repository -‐> collec@on Create a base repository class using generics Create separate concrete classes for each schema version Use a reflec@on driven automapper Create a separate service to handle more complex opera@ons -‐ Keep repositories simple!
aggregates e.g. Algorithms, merge, split Infrastructure Services: Communicate with outside world via adapters e.g. Complex opera@ons spanning mul@ple repositories
to MongoDB for complex transac@ons -‐ Queries spanning mul@ple collec@ons -‐ Modifica@ons to mul@ple documents -‐ Map/reduce Leverage repositories when possible -‐ Keep repositories simple
Pop using FindAndModify for single consumer For mul@ple consumers, use documents as changelog entries -‐ Include a @mestamp in the document so each consumer can keep track of what it has seen Reuse repository func@onality Periodically purge old entries in batch Separate collec@ons for each queue
Things to consider: Use GridFS! Make Blobs immutable so they are idempotent -‐ Use internally generated id for filename -‐ Two phase write – local disk first, then GridFS Make GridFS filenames self describing -‐ Embed type on end of filename -‐ Embed encoding as extension (.gzip, .zip, etc) Use GridFS metadata to further describe blob Store GridFS filename as an ajribute of a domain model en@ty