Domain Driven Design in Python

Domain-Driven Design (DDD) is an approach to drive complex software projects using a set of principles, software design practices and techniques by directly aligning the code with the concepts in the domain [Evans2003]. The concept was established by Eric Evans in his book “Domain-Driven Design, tackling complexity in the heart of software”. Vaughn Vernon notes that Evans' book “is essentially a large pattern language” [Vernon2013].

The basic set of object-oriented Design Patterns became popular by the "Gang of Four" [Gamma1995]. Thereafter, in the context of enterprise applications, Martin Fowler [Fowler2002] presented a set of higher level patterns usually applied in enterprise application development. Eric Evans takes the concept further by placing the Domain Model in the heart of enterprise applications and presenting a set of strategic and tactical software design patterns tied together by a Ubiquitous Language.

Martin Fowler described the Domain Model as “an object model of the domain that incorporates both behaviour and data” and coined the term Anemic Domain Model to describe the opposite. By applying domain-driven design in a software project, the focus shifts from the Database to the Domain Model leading to more maintainable and clear designs [Nilsson2006]. In the prominent software design and implementation paradigm (in Python but also in the Java EE world) code is organized in procedures that make direct or indirect calls to the database. Those procedures many times are disguised as object methods that operate against other objects that hold the data and have no business logic. This is referred by Fowler as the Transaction Script and is the base of the Anemic Domain Model.

Domain-driven design is usually described in the context of statically-typed and object-oriented programming languages like Java and C#. In contrast, practical resources and examples are sporadic in dynamic programming languages such as Python. In addition, there is often strong criticism in Python discussion forums that many software design patterns are not applicable in Python because the patterns are circumventing weaknesses of statically typed programming languages like Java and C++.

Ubiquitous language

A prominent element within Domain-Driven Design is the Ubiquitous Language. Briefly, the term refers to the shared language exercised by a team of domain experts and software developers which is in addition reflected in the code [Evans2003].

In software projects, domain experts and software developers have to collaborate in order to describe a model of the domain. Typically, domain experts use the business terminology to describe the domain. However, many times they tend to use more than one words for the same thing or the same word to mean many different things depending on the context. On the contrary software developers translate the input from the users into abstract concepts, algorithms, inheritance, database tables in order to support the delivered software. Furthermore, domain experts have to translate a developer's technological jargon back to their business concepts in order to understand the software being implemented. This leads both collaborators being lost in translation.

In order to bridge the gap a business analyst might be employed to act as the translator between the domain experts and the developers. However, this could lead to two different spoken languages and if the analyst misinterprets a requirement the final application might not fulfil its goal which could be discovered quite late [Haywood2009].

Many teams recognize the deficiencies of using a different vocabulary to describe the Domain Model and intuitively commit in using a shared language. This language needs to be ubiquitous and evolve out of the collaboration between domain experts and software developers [Evans2003]. The developers should reflect the Ubiquitous Language in the source code, not only in naming classes and attributes but also when coding behaviour [Evans2003]. Due to the use of a common language, whenever there is a misconception the developers could share their thoughts by presenting their diagrams (simple sketches or even UML diagrams) while the domain experts could spot easily mistakes and contribute knowledge about the domain [Nilsson2006].

Whenever the shared language changes, this is an indication of a modification in the domain which should trigger code transformations [Evans2003]. The team should work hard to keep the Ubiquitous Language clear from ambiguities and in sync with the software solution [Nilsson2006] by re-factoring the code to conform to the new model [Evans2003].

Note

The above is an abstract of my MSc Thesis titled "Applying Domain-Driven Design in Python - designing a self-hosted Read-it-Later service" (January 2014).

Read-it-Later service

Preliminary research shows that there are no real-world examples of applying Domain-Driven Design in Python based software projects. In future posts I will present the principles behind domain-driven design and demonstrate its applicability in the python ecosystem. In order to showcase the business value of DDD, a Read-it-later application will be developed as a real-world example.

Read-it-Later is a service that allows users to save an online article to read it later. The article's title and main body, stripped from advertisement and unnecessary content, is retrieved from the target system and stored into the Read-it-Later service database. The domain of a Read-it-Later service is not overly complicated but it is complex enough to demonstrate various DDD principles and building blocks. The most basic use case is: a user visits a web page with an interesting article, decides to read it later and clicks to a bookmarklet [1] to trigger the saving to the Read-it-Later service.

References

[Evans2003] (1, 2, 3, 4, 5, 6) Evans, E. (2003) Domain-Driven Design: Tacking Complexity In the Heart of Software. Boston: Addison-Wesley.
[Gamma1995] Gamma, E., Helm, R., Johnson, R. & Vlissides, J. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. New York: Addison-Wesley.
[Fowler2002] Fowler, M. (2002) Patterns of Enterprise Application Architecture. Boston: Addison-Wesley.
[Haywood2009] Haywood, D. (2009) Domain-Driven Design Using Naked Objects. Raleigh: Pragmatic Bookshelf.
[Nilsson2006] (1, 2, 3) Nilsson, J. (2006) Applying Domain-Driven Design and Patterns. Boston: Addison-Wesley.
[Vernon2013] Vernon, V. (2013) Implementing Domain-Driven Design. Boston: Addison-Wesley.

Footnotes

[1] Bookmarklet is a simple web browser bookmark containing JavaScript code that sends an HTTP request to the Read-it-Later service.

Comments

Comments powered by Disqus