Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The role of the database in a Domain-Driven Des...

The role of the database in a Domain-Driven Design world

What is the role of the database in a Domain-Driven Design world?

Manuel Scapolan

June 14, 2014
Tweet

More Decks by Manuel Scapolan

Other Decks in Programming

Transcript

  1. Software Architect and Developer on large object-oriented business applications. Currently

    I’m particularly interested in agile software development, Domain-Driven Design, CQRS and NoSQL databases. Software Architect at
  2. It is said that once open a time the Baobab

    was the evergreen, luxury, full of fruit tree of abundance. Man adored it as if it were a divinity, until gods got gealous and so one day they put it upside-down to punish its arrogance. Since then the Baobab shows its roots.
  3. • Domain-Driven Design promotes a behaviorally rich domain model where

    data represent the state that the entities reach when a business operation ends • They are attributes of the entities and value objects and nothing else • If your entities only expose data, it’s not a domain model, it’s an anemic domain http://martinfowler.com/bliki/AnemicDomainModel.html 1 1
  4. • It isolates the domain from the responsability of persisting

    the changes of the object model • It decouples the client from the storage technology • It provides testability using collection in memory « Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. » Martin Fowler – PoEAA
  5. • The data model and the domain model use different

    paradigms, object vs relational, we have an impedance mismatch • We can partially resolve the mismatch with an ORM (object-relational mapper) • Sacrifice some richness of object relationships to keep close to the relational model http://www.agiledata.org/essays/culturalImpedanceMismatch.html
  6. • The Persistence Layer serves the Domain and depends on

    it. So the persistence needs to know about the domain but not vice-versa • Design the persistence entities for storage purposes and to match the ORM's constraints (like making all the properties virtual). So you'll have Domain Entities and Persistence Entities, each with their own different purposes and implementations • If you correctly define each context boundary, you will obtain small entities without needing any lazy loading • If you can't simply use SQL into your repository, map a set of DTO to your database schema, and use them into factories to initialize entity classes
  7. • We need a data model as close as possible

    to domain model • I think about NoSQL databases, especially document databases: • Every aggregate is a document • Schema-free • No ORM, no mapping • Scale-out techniques, like sharding
  8. vs

  9. • Break the taboo of the data duplication • I

    can have a different persistence strategy in every bounded context • The technology can depend on the form of the domain model or on performance requirements • Concentrate your energy on core domains Use the right tool
  10. • Start from the premise that there isn’t a database

    • Choose a data model as close as possible with your domain model and isolate it from the persistence layer with a repository • Hide the database from other bounded contexts or external systems • If necessary, provide an API that exposes public data • Maintain also the database aligned with the ubiquitous language • Stay focused!
  11. The origin of some images hasn’t been specified but they

    have Creative Commons licence. Slide 3 – https://www.flickr.com/photos/philmcelhinney/1000986005/ Slide 7 – https://www.flickr.com/photos/zuhair_ahmad/5042955425/ Slide 11 – https://www.flickr.com/photos/zunami/3160114349/ Slide 12 – https://www.flickr.com/photos/barryskeates/6803420982/sizes/k/ Slide 17 – https://www.flickr.com/photos/siovene/5931171740/ Slide 26 – https://www.flickr.com/photos/booleansplit/12329159165/