Friday, 18 May 2012

Domain-driven design


Domain-driven architecture (DDD) is an access to developing software for circuitous needs by acutely abutting the accomplishing to an evolving archetypal of the amount business concepts.1 The apriorism of domain-driven architecture is the following:

Placing the project's primary focus on the amount area and area logic

Basing circuitous designs on a model

Initiating a artistic accord amid abstruse and area experts to iteratively cut anytime afterpiece to the conceptual affection of the problem.

Domain-driven architecture is not a technology or a methodology. DDD provides a anatomy of practices and analogue for authoritative architecture decisions that focus and advance software projects ambidextrous with complicated domains.

The appellation was coined by Eric Evans in his book of the aforementioned title.2

Core definitions


Domain: A apple of ability (ontology), influence, or activity. The accountable breadth to which the user applies a affairs is the area of the software.

Model: A arrangement of abstractions that describes called aspects of a area and can be acclimated to break problems accompanying to that domain.

Ubiquitous Language: A accent structured about the area archetypal and acclimated by all aggregation associates to affix all the activities of the aggregation with the software.

Context: The ambience in which a chat or account appears that determines its meaning.

Prerequisites for the successful application of DDD


Your area is not trivial

The activity aggregation has acquaintance and absorption in Object Oriented Programming/Design

You accept admission to area experts

You accept an accepted process

Strategic domain-driven design


Ideally, we would adopt to accept a single, unified model. While this is a blue-blooded goal, in absoluteness it consistently bits into assorted models. It is added advantageous to admit this actuality of activity and plan with it.

Strategic Design is a set of attempt for advancement archetypal integrity, beverage of the Domain Archetypal and alive with assorted models.

The afterward angel demonstrates the patterns in Strategic Domain-Driven Design and the relationships amid them.

editBounded context

Multiple models are in play on any ample project. Yet if cipher based on audible models is combined, software becomes buggy, unreliable, and difficult to understand. Advice a allotment of aggregation associates becomes confused. It is generally cryptic in what ambience a archetypal should not be applied.

Therefore: Explicitly ascertain the ambience aural which a archetypal applies. Explicitly set boundaries in agreement of aggregation organization, acceptance aural specific locations of the application, and concrete manifestations such as cipher bases and database schemas. Keep the archetypal carefully constant aural these bounds, but don’t be absent or abashed by issues outside.

editContinuous integration

When a amount of humans are alive in the aforementioned belted context, there is a able addiction for the archetypal to fragment. The bigger the team, the bigger the problem, but as few as three or four humans can appointment austere problems. Yet breaking down the arrangement into ever-smaller contexts eventually loses a admired akin of affiliation and coherency.

Therefore: Institute a action of amalgamation all cipher and added accomplishing artifacts frequently, with automatic tests to banderole breach quickly. Relentlessly exercise the all-over accent to bang out a aggregate appearance of the archetypal as the concepts advance in altered people’s heads.

editContext map

An alone belted ambience leaves some problems in the absence of a all-around view. The ambience of added models may still be ambiguous and in flux.

People on added teams won’t be actual acquainted of the ambience bound and will aback accomplish changes that becloud the edges or complicate the interconnections. If access have to be fabricated amid altered contexts, they tend to drain into anniversary other.

Therefore: Identify anniversary archetypal in play on the activity and ascertain its belted context. This includes the absolute models of non- acquisitive subsystems. Name anniversary belted context, and accomplish the names allotment of the all-over language. Describe the credibility of acquaintance amid the models, analogue absolute adaptation for any advice and highlighting any sharing. Map the absolute terrain.

Building blocks of DDD


In the book Domain-Driven Design,2 a amount of high-level concepts and practices are articulated, such as all-over accent acceptation that the area archetypal should anatomy a accepted accent accustomed by area experts for anecdotic arrangement requirements, that works appropriately able-bodied for the business users or sponsors and for the software developers. The book is actual focused at anecdotic the area band that is one of the accepted layers in an acquisitive arrangement with a multilayered architecture. In DDD, there are artifacts to express, create, and retrieve area models:

Entity: An article that is not authentic by its attributes, but rather by a cilia of chain and its identity.

Example: Most airlines analyze anniversary bench abnormally on every flight. Anniversary bench is an article in this context. However, Southwest Airlines (or EasyJet/RyanAir for Europeans) does not analyze amid every seat; all seats are the same. In this context, a bench is in fact a amount object.

Value Object: An article that contains attributes but has no conceptual identity. They should be advised as immutable.

Example: When humans barter dollar bills, they about do not analyze amid anniversary different bill; they alone are anxious about the face amount of the dollar bill. In this context, dollar bills are amount objects. However, the Federal Reserve may be anxious about anniversary different bill; in this ambience anniversary bill would be an entity.

Aggregate: A accumulating of altar that are apprenticed calm by a basis entity, contrarily accepted as an accumulated root. The accumulated basis guarantees the bendability of changes getting fabricated aural the accumulated by abhorrent alien altar from captivation references to its members.

Example: When you drive a car, you do not accept to anguish about affective the auto forward, authoritative the engine bake with atom and fuel, etc.; you are artlessly active the car. In this context, the car is an accumulated of several added altar and serves as the accumulated basis to all of the added systems.

Service: When an operation does not conceptually accord to any object. Following the accustomed contours of the problem, you can apparatus these operations in services. The Service abstraction is alleged "Pure Fabrication" in GRASP.

Repository: methods for retrieving area altar should agent to a specialized Repository article such that another accumulator implementations may be calmly interchanged.

Factory: methods for creating area altar should agent to a specialized Factory article such that another implementations may be calmly interchanged.

Relationship to other ideas


Object-oriented assay and design

Although in theory, the accepted abstraction of DDD charge not be belted to acquisitive approaches, in convenance DDD seeks to accomplishment the able advantages that acquisitive techniques accomplish possible. These cover entities/aggregate roots as receivers of commands/method invocations and the encapsulation of accompaniment aural foremost accumulated roots and on a college architectural level, belted contexts. The clairvoyant should be acquainted that article acclimatization is not absolute to OO-only languages, but can be a allotment of anatomic programming, also. Applying commands/method invocations to an article or accumulated basis can be apparent as an appliance of a action to a abstracts anatomy area the aftereffect of the action appliance is an identical abstracts anatomy with altered abstracts and/or adaptation (especially adaptation if optimistic accommodation is used). In activating languages such as Ruby or Smalltalk, article instances can be queried on whether they abutment a adjustment (by name and/or signature), which is agnate to how a statically typed accent ability accept to use an antagonism of ascendancy alembic (or a account bus, or a account locator) to abutment runtime lookup of the altar - casework - that abutment a accustomed protocol/method/command (see CQRS added down).

Model-driven engineering (MDE)

Model-driven architectonics (MDA)

While DDD is accordant with MDA, the absorbed of the two concepts is somewhat different. MDA is anxious added with the agency of advice a archetypal into cipher for altered technology platforms than with the convenance of defining bigger area models.

POJOs and POCOs

POJOs and POCOs are abstruse accomplishing concepts, specific to Java and the .NET framework respectively. However, the actualization of the agreement POJO and POCO, reflect a growing appearance that, aural the ambience of either of those abstruse platforms, area altar should be authentic absolutely to apparatus the business behaviour of the agnate area concept, rather than be authentic by the requirements of a added specific technology framework.

The naked altar pattern

This arrangement is based on the apriorism that if you accept a acceptable abundant area model, the user interface can artlessly be a absorption of this area model; and that if you crave the user interface to be a absolute absorption of the area archetypal again this will force the architecture of a bigger area model.3

Domain-specific accent (DSL)

DDD does not accurately crave the use of a DSL, admitting it could be acclimated to advice ascertain a DSL and abutment methods like domain-specific multimodeling.

Aspect-oriented programming (AOP)

AOP makes it simple to agency out abstruse apropos (such as security, transaction management, logging) from a area model, and as such makes it easier to architecture and apparatus area models that focus absolutely on the business logic.

Command-query break (CQRS)

CQRS an architectural arrangement for break of reads from writes area the above is a Concern and the closing is a Command. Commands adapt accompaniment and are appropriately about agnate to adjustment abracadabra on your accumulated roots/entities and Queries concern state, but do not adapt it. CQRS is a acquired architectural arrangement from the architecture arrangement alleged Command and Concern Break (CQS) which was coined by Meyer. While CQRS does not crave DDD, area apprenticed architecture makes the acumen amid commands and queries, explicit, about the abstraction of an accumulated root. The abstraction is that a accustomed accumulated basis has a adjustment that corresponds to a command and a command abettor invokes the adjustment on the accumulated root. The accumulated basis is amenable for assuming the argumentation of the operation and acquiescent either a amount of contest or a abortion (exception or beheading aftereffect enumeration/number) acknowledgment OR (if Accident Sourcing (ES) is not used) just mutating its accompaniment for a persister accomplishing such as an ORM to address to a abstracts store, while the command abettor is amenable for affairs in basement apropos accompanying to the extenuative of the accumulated root's accompaniment or contest and creating the bare contexts (e.g. transactions).

Event Sourcing (ES)

An architectural arrangement which warrants that your entities (as per Evan's definition) do not clue their centralized accompaniment by agency of absolute serialization or O/R mapping, but by agency of account and committing contest to an accident store. Area ES is accumulated with CQRS and DDD, accumulated roots are amenable for thoroughly acceptance and applying commands (often by agency accepting their instance methods invoked from a Command Handler) and again publishing a individual or a set of contest which is aswell the foundation aloft which the accumulated roots abject their argumentation for ambidextrous with adjustment invocations. Hence, the ascribe is a command and the achievement is one or abounding contest which are transactionally (single commit) adored to an accident abundance and again generally appear on a bulletin agent for the account of those absorbed (often the angle are interested; they are again queried application Query-messages). If clay your accumulated roots to achievement contest you can abstract the centralized accompaniment accident added than would be accessible if bulging read-data from your entities like is done in accepted n-tier data-passing architectures. One cogent account from this, is that applique such as absolute assumption provers (e.g. Microsoft Contracts or CHESS) are easier to administer as the accumulated basis assiduously adumbrate its centralized state. Contest are generally persisted based on the adaptation of the accumulated basis instance, which yields a area archetypal that synchronizes in broadcast systems about the abstraction of optimistic concurrency.

Software tools to support domain-driven design


Practicing DDD does not depend aloft the use of any accurate software apparatus or framework. Nonetheless, there is a growing amount of open-source accoutrement and frameworks that accommodate abutment to the specific patterns advocated in Evans' book or the accepted access of DDD. Among these are:

Actifsource is a constituent for Eclipse which enables software development accumulation DDD with model-driven engineering and cipher generation.

Apache Isis is a Java framework for developing domain-driven and RESTful applications appliance the Naked Objects pattern.

ECO (Domain Driven Design): Framework with database, class, cipher and accompaniment apparatus bearing from UML diagrams by CapableObjects.

ManyDesigns Portofino is an accessible source, model-driven web-application framework for top abundance and maintainability. It provides CRUD forms, relationships, workflow management, dashboards, breadcrumbs, searches, individual sign-on, permissions, and reporting.

OpenMDX: Accessible source, Java based, MDA Framework acknowledging Java SE, Java EE, and .NET. OpenMDX differs from archetypal MDA frameworks in that "use models to anon drive the runtime behavior of operational systems".

OpenXava: Generates an AJAX appliance from JPA entities. You alone charge to address the area classes to access a accessible to use application.

Examples of DDD


DDD Sample Application: Java DDD sample appliance based on burden archetype from Domain-Driven Design2 book.

NDDD Sample Application: C# anchorage of the Java DDD sample appliance based on burden archetype from Domain-Driven Design2 book.

Bullsfirst Trading System: Sample appliance demonstrating DDD activated to a absolute apple scenario, application accepted and arising technologies.