söndag 5 juni 2016

Layers in DDD

Separation of Concerns

The main objective of DDD is to create separation of concern to keep the system stable.

The term Separation of Concerns means in computer programming to use different mechanisms to separate things that do not have with each other to do. The most known is might be the MVC pattern with separates Models, Views and Controllers. Here we are not talking about MVC: We are talking about DDD but of course these patterns can be combined to some extent.

The separation takes place in two different directions in DDD: Bounded contexts that I will return to in a later post. DDD people often talk about something called a hexagonal design. As I never have found any good explanation of why the authors use the hexagon symbol I do not use it, but it might be good to know that the term exists. The hexagonal design is intended to divide up the application in the parts and control how they communicate with each other. Sometimes called the pattern also is called Ports and Adapters, which is not exactly the same thing, because it mainly deals with the interface layer, but they overlap each other.

The layers I use are:

  • Infra-structure 
  • Application 
  • Interface 
  • Domain 

Application layer

The application layer is responsible for driving the work flow of the application. This layer can be used for transactions, high-level logging, and security.

The application layer is often thin compared to the domain logic – it is just coordinating domain objects without actually performing the work. The application layer also provides the interface layer access to the domain layer.

Interface Layer

This layer holds everything interacts with other systems, such as web services, RPC, user interface or web applications. It handles the interpretation, validation and translation of incoming data. It also handles serialization of output data, such as JSON over REST.

It is this layer that is called the Ports and Adapters layer. We can have a number of possible ways of connectivity (ports) and adapters that adapts the domain objects to the protocols and acts as an anti-corruption layer.

Domain objects are never directly visible outside the domain. They have other representations in the protocol.

If you want to combine DDD with a MVC or MVP pattern in a desktop application this can be where to place your views and controllers, as one of the ports. Another way is to think about the MVC-part of your application as a separate client connecting to the interface layer through the Interface layer.

The domain layer

The domain layer is the heart of the program, and that is where all the interesting stuff happens. In Java, do you normally do one package per bounded context, and each unit includes entities, value objects, domain events, a repository interface, and sometimes factories.

The core of the business logic is here. The structure and the naming of assemblies, classes and methods in the domain layer follows the ubiquitous language, and you should be able to explain to a domain expert how this part of the program works by drawing a few simple graphs and use real class and methodG names from the source code.

The interface to the upper layers and other bounded contexts are provided by one or more services, adapted by the interface- or port and adapters- layer when someone wants to connect the service from outside the domain. The interface is defined by the service.

Repositories and the factories have to use the infrastructure to get access to the database. They thus define interfaces for Data Objects (DO) and Data Access Objects (DAO) used to store entities in the database. The implementation is later done in the infrastructure, but the domain is in charge of the data and logic. The domain drives the development.

The relationship between the repositories, factories and the DAO might be hard to understand. The responsibility of the DAO is simple storage. All domain logic should ideally be in the repository. If you for example need to validate data before storing the repository should handle this and then use the DAO to store the data of the entity. Most calls to repositories have the structure of first doing some domain logic and then store the result with the DAO as delegate.

One DAO should only handle one type of DO-s. Likewise a repository ideally also should only handle one type of entities. To handle more complex operations, including many types of entities, you should create a domain-service that uses the factories and repositories. A domain service should cover one bounded context.

We will get back to the relationship between a domain entity and a data-object. For now we can say that the entity is the abstraction of the data we handle and the data-object is the actual data stored by the infrastructure.

This is what makes some people think the DDD-model is a bit cumbersome: We have to define a lot of interfaces in the domain that later is implemented in the infrastructure. The different abstraction levels also add to the complexity. The system also have a lot of classes in the domain handling the same entities: The services, repositories and factories. The system becomes a bit heavy to handle with these extra layers of abstraction. The benefit is the separation of concern and the possibility to change infrastructure without having to change the domain logic.

Infrastructure Layer

Addition to the three vertical layers, there are also infrastructure. It supports all of the three other layers. This is done in different ways, which facilitates communication between the layers. Simply put, the infrastructure consists of everything that exists independently of our application: external libraries, database, application server, messaging and so on.

All communication between the layers to be abstracted so that it is independent of changes in other layers. The clearest example of this is that the domain layer uses Repository to manage storage. This repository then define interfaces for the database connection against infrastructure, but the implementations are in the infrastructure layer.

Although it can be difficult to give a hard definition of what type of code belong to the infrastructure layer for each given situation, it should be possible to completely replace infrastructure and still be able to use the domain layer and possibly the application layer to solve the key business problems.

söndag 22 maj 2016

Working with DDD - Domain, Model and Language

There are a lot of concepts that is important for DDD. The most important ones are the Domain, the Model and the Ubiquitous Language.

Domain

A sphere of knowledge (ontology), influence or activity. The subject that the user is working a program on the domain of is software. The domain in DDD is the problem space. The corresponding solution space is called the model.

Model

A system of abstractions that describe selected aspects of a domain and can be used to solve the problems associated with that domain.
With DDD, we are looking to create models of the domain. Things which often places great effect at the beginning of a project, such as persistence, user interface and protocol. Model does not mean a graph or set of graphs e.g. UML. Charts are useful, but they are not the model, just different views of the model. The model is one that we choose to implement in software, represented in the code used to construct the system. The code itself is the model.
This gives certain expectations of the code. It shall be easy to understand and read. If the domain experts can understand a snippet of code describing their domain, the room for error shrinks. The standards set by Robert C. Martin in Clean Code is one way to accomplish this.

Ubiquitous Language

A language built around the domain model and is used by all team members to connect all activities the team with the software.
This does not exclude the use of diagrams, symbols and text analysis. These are important tools for finding the model, and describe it before it gets into the code. Texts and informal sketches is a good starting point. We do not need any special design model or specific sets of symbols. This is because we are starting from the domain, the reality, and have to describe it in a way that is shared by developers and those who order the application, the domain experts. It is therefore extremely important to have a common description of reality and a ubiquitous language. This work is the basis for the whole architecture.

söndag 15 maj 2016

Domain Driven Design

A couple of year old trend is DDD. I love a lot of the ideas behind DDD, but the people talking about it can be really annoying. They talk a lot how DDD is not the strategic and tactical patterns that most of the books on the topic covers on 90% about the pages. They have seen some kind of light that is greater. I like to take a more pragmatic look of it: What can I use of this to build great software.
Domain Driven Design (DDD) is a method of creating a software architecture. The basis for domain-driven design is to start from domain experts' knowledge and allows the software a model of the domain and its processes:
  • base the design on a model of the domain 
  • you have a creative collaboration between developers and domain experts to refine a model 
  • establishing a ubiquitous language shared between developers and domain experts 
The idea is to start from the business and not in the technical part.

Domain-driven design also presents a set of patterns for building applications. These are not DDD, they are examples of how to model the domain and separate from other parts of the system. It is important to distinguish these patterns from the technical patterns such as presented by the 'Gang of Four' (Erich Gamma et al.) and think that patterns of DDD rather as abstractions than implementation proposals. The types of patterns often coincide, but we look at them from different directions. From a perspective of ideas and from an implementation perspective. DDD is about creating a sustainable architecture, not primarily about implementation, but for the pragmatic programmer the books presents a lot of implementation ideas to use.

I have learned DDD through practice. A skilled developer introduced me to it and we worked on a project so that it was hands-on learning. Some parts of the model I present here is based more on that practical experience than  Eric Evans ideas. Thus, this may differ from his theoretical model. If you want to learn DDD in an orthodox way: Read his texts! I have read some books on DDD and in  some coming posts I will present a model that is my compromise between theory and practice.

söndag 8 maj 2016

Why is This Blog Called Grouchy-developer

I am almost 50 and has been developing software since I was 14. Year by year I am getting more tired and grumpy about all trendy stuff and hypes. This does not mean that I am against progress, I just get annoyed when people get that religious newly saved missionary personality. The worst kind call themselves "evangelists". You know the judgemental types talking bad about all old stuff and thinks that just they have seen the light.

Naturally a lot of bad software has been written and we learn new stuff, but and tools and languages has evolved. Good and bad developers have always existed.

I also get annoyed with people that are not evolving. If we develop software we should develop ourselves also. This might look as a contradiction to my first paragraph, but it is not. It is the non-humble approach to development that annoys me not the new ideas. A lot of developers just copy old ideas year after year, too afraid to evolve.

I did a for-fun-personality test and became Dr. Perry Cox in Scrubs. It seems that us old professionals gets a bit grouchy with years. We are all fascinated by guys like him and even like Dr. Gregory House. We want to be the brilliant minds solving problems often in a lone wolf kind of way. In reality we might be more like Statler and Waldorf sitting on the balcony in Muppet Show.

"WALDORF: They aren’t half bad.  
STATLER: Nope, they’re ALL bad!"