260 likes | 444 Views
DDD and Beyond … on Rails! !. Why Rails?. Ruby is awesome! Vibrant community & ecosystem CoC - less boilerplate code Supports rapid development Asset pipeline Built-in ORM – ActiveRecord And more …. Why DDD?. Powerful practices & patterns for tackling complex problems
E N D
Why Rails? • Ruby is awesome! • Vibrant community & ecosystem • CoC - less boilerplate code • Supports rapid development • Asset pipeline • Built-in ORM – ActiveRecord • And more …
Why DDD? • Powerful practices & patterns for tackling complex problems • Both tactical and strategic • Align business & technology • Zero translations (Ubiquitous Language) • Domain experts contribute to software design • Code becomes a living description of the business • Increased clarity and understanding • Collaboration that results in well understood models • Strategic development • Prioritize resources & investment toward more complex or differentiating areas of the business • Organized architecture • Align physical systems with distinct areas of the business
DDD on Rails Rails DDD Tackle complex domains Align business & technology Clarity& understanding Strategic development Organized Architecture • Ruby is awesome! • Vibrant community • Less boilerplate • Rapid development • Asset pipeline • ActiveRecord * ActiveRecord is a DDD antipattern ….
DDD: Strategic Patterns • Separate areas of the business • Ideally one Domain Model per Subdomain Subdomains Ubiquitous Language • Language used & understood by all stakeholders in a project • Used to express a Domain Model • Sets the context for terms in a Ubiquitous Language • Ideally one Bounded Context per Subdomain Bounded Context
Ubiquitous Language • Program Mgmt. Context • Organizers create Conferences • Organizers call for Proposals • Candidates submit Proposals • Once received, Reviewers rate Proposals • Organizers choose Proposals for Session topics • Access Control Context • Public Users register to create an Account • Accounts are assigned to one or more Roles • Users sign in to begin an authenticated Session
Ubiquitous Language User Story:As a candidate I would like to submit a proposal to a conference So that it may be available for review
Isolating Contexts Gem, Engine, App Gem, Engine, App
DDD: Tactical Patterns • Entity • Aggregate • Repository • Value Object • Domain Event • Domain Service * Building blocks of a Domain Model
Organizer creates a conference. ActiveRecord DDD Entity
Organizer calls for proposals. ActiveRecord DDD Entity
Candidate creates and submits a proposal. ActiveRecord DDD Entity
Proposals can only be received once the organizer has called for proposals. ActiveRecord
Proposals can only be received once the organizer has called for proposals. DDD Entity
ActiveRecordvs DDD Entity ActiveRecord DDD Entity Shaped by UL Blank canvas to express UL Enforces invariants Single responsibility Encapsulate business logic • Shaped by DB schema • CRUD semantics baked in • No Encapsulation • Multiple responsibilities • Data access • Business logic • REST resource/view data
Repositories • Collection semantics for an Aggregate Root • add(conference), find_by(id) etc. • Modify one Aggregate per transaction * • Consistency boundary • Watch out for false invariants • Easily implemented with NoSQL solutions • Object/Document/Key-values etc.
Event Sourcing • Raise explicit event for every state change • Persist serialized events to an event store • To retrieve persisted entity • Deserializeevents and replay • Conference.allocate.replay(events)
Event Sourcing & CQRS DB Query Store AsyncHandlers Events Read-only Domain Model Application Application Queries Commands Client
More Information • A great DDD Introhttp://www.infoq.com/minibooks/domain-driven-design-quickly • More on CQRShttp://cqrs.wordpress.com/2010/09/08/welcome-to-cqrs-info/ • My Open Source Projecthttps://github.com/domaindrivendev/puree