What is the Book About

Matthias shows how to focus on the business needs, on use cases and how to write it down into the code. He calls this code “core code”. The core code is decoupled from databases, frameworks or libraries. This is the general idea of the book — isolate the code containing business logic.

Part I explain why should we focus on separating core code from surrounding infrastructure code and step by step shows how to achieve that. This separation leads to readable, maintainable, testable, extendable code that will survive at least years. …


Thanks Myfuture.com, https://www.flickr.com/photos/myfuturedotcom/6053042920

Typical Shopping Cart

This article is about a shopping cart on a typical online shop.

Let’s imagine it

We’ll have to save identification of the product and the amount, it’s easy.


Thanks Qui Nguyen Khac, https://pixabay.com/users/nguyenkhacqui-2851627/

Don’t repeat yourself

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system

— Andy Hunt and Dave Thomas, The Pragmatic Programmer

Car in API

We develop multiple systems that communicate via API in JSON format. The most transferred information are information about cars.

An example of such transferred car

{
"vin": "4Y1SL65848Z411439",
"make": {
"key": "vw",
"name": "Volkswagen",
"shortName": "VW"
},
"model": {
"key": "arteon-shooting-brake",
"name": "Arteon Shooting Brake",
"shortName": "Arteon"
},
"color": {
"key": "black",
"name": "Black"
}
}

We have to express this JSON in objects. We use PHP in our company, but we want to develop modern type-safe…


Thanks Lisa Johnson, https://pixabay.com/users/elljay-962436/

We use access modifiers to hide certain properties or methods — public, protected, private. Final is used to disallow children. But why would we hide something or disallow anything? Why to do such things?

We have to often implement new features or modify existing ones. This results in extending current code, extending parameters of methods and ultimately to the code refactoring.

Discount Functionality

Let’s assume following code — it deals with Orders and we allow applying percentage discount to the whole order. This percentage discount is applied to all order items.

The getDiscount() method is public because it seems nice to…


How to apply

1. Hear the story
2. Understand terms
3. Extract scenarios
4. Select use cases
5. Model concepts
6. Write tests + code
7. Connect to the infrastructure

1. Hear the story

The first and most important step of programming. A person who knows the domain, the topic we are going to program, has to explain it to us. Such a person is known as a domain expert. We have to listen carefully.

Car dealer is the domain expert. The car dealer tells us what it means to sell cars, what happens all the time and what happens rarely. …


Let’s think for a while — where do we use class/interface name?

  • During creating object from the class (new Product(...))
  • In usage

Creating objects

We aren’t able to create objects from abstract classes and interfaces, so it is always without prefix/suffix. I hope no one suggest to call a concrete class a new ClassProduct(...), but rather new Product(...).

OOP

I’d like to introduce a philosophical note here. What is object-oriented programming about? I believe that OOP is about

Encapsulating data by behavior.

Behavior means we use methods of objects, not their properties.

Usage

Let’s consider following code

class CartItem {
private…


Domain-driven design is about the domain. Domain services and domain factories do not exist in the domain. In general we shouldn’t use them.

They are artificial constructions and this causes a lot of problems with code understanding, maintainability and also divergence between the domain, the model and the code.

Domain Service

We encapsulate domain use cases into domain aggregates, entities and value objects. This works well for many use cases. But when a single use case involves multiple aggregates, we have a problem.

It may happen that use case doesn’t naturally belong to any aggregate. …


Product Name Story

We worked on an e-shop and the product had exactly one name. One day we faced confronted with the reality — the e-shop has to be multilingual. I imagined a new database table for every language field immediately, and I was disgusted by all the tables that had to be created. But luckily I asked myself — What is the domain?

Use cases

The domain can be extracted from stories and use cases. We discussed this in our team and we extracted a couple of use cases:

  • Change the product name in the language.
  • Show the product name in the language
  • If…


The repository is a persistent collection that we know from repository article. We expect that we know the cart model from model and implementation articles. But it doesn’t make any harm if we repeat the cart model again.


Collections and Reality

Let’s imagine that we have a system which runs continually, has enough memory and which is for a single user only. With this kind of system, we can have all objects in memory collections and everything is shiny. Memory collections are enough — they allow us to store, receive and remove objects.

But real world is different. We usually build web applications with request-process-response-die life.

We have to load objects out of persistent memory like a database, work with them and persist them again while the process is done. …

Svaťa Šimara

Developer interested in Domain-Driven Design & Modeling

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store