Domain-Driven Design for Microservices Architecture

Vũ Nhật Minh / @dtvd88

Agenda

  • About me

  • Domain-Driven Design

  • DDD approach for Microservices architecture

About me

Rainbow Stream

Domain-Driven Design

  • About me

  • Domain-Driven Design

  • DDD approach for Microservices architecture

Software development is hard

Writing code is easy part

The hard part come from real-world problems

Real-world problem belongs to a specific domain

Each domain will have domain experts 

We need to extract expert's raw knowledge to a blueprint in our mind,

a model

Tool to express and develop the model: The Ubiquitous Language

The Ubiquitous Language

Building the ubiquitous language

  • Commit to use the language in everywhere

    • All communication form: speech, writing, diagrams ...

    • In the code later

  • Communicate with domain experts and find keywords

  • Developers can think about keyword as classes in OOP

The model

Architecture

and design patterns

Think DDD

Layered Architecture

DDD Desgin Patterns

  • Extract from the ubiquitous language
    • Entities
    • Value Objects
    • Services
  • Ownership and boundaries
    • Aggregates
  • Storage
    • Repositories

Entities

Entities = objects which have identity

Attributes does not matter, only identity matter

Find out what is Entities

Entities is mutable

Value Objects

Find out what is not need to be Entities

Call them Value Objects

Value Objects is immutable and having no identity

User
UserID
Name
FacebookURL
TwitterURL
GithubURL
User
UserID
Name
SocialProfile
Social Profile
Facebook
Twitter
Github

Entity

Value Object

Identity

// Entities with same id is considered the same.
class User(val userId: Int, val name: String, val profile: SocialProfile) {
  def equals(other: User): Boolean = {
    userId == other.userId
  }
}

// Value Objects with same value is considered the same.
class SocialProfile(val facebookURL: String,
                    val twitterURL: String,
                    val githubURL: String) {
  def equals(other: SocialProfile): Boolean = {
    facebookURL == other.facebookURL &&
    twitterURL == other.twitterURL &&
    githubURL == other.twitterURL
  }
}

Services

Language's nouns can be Entities or Value Objects

Language's verbs/behaviors can be Services

Services declare an operation which is stateless

class Saiyan(val name: String, val age: Int)

// Service provide a method to perform an operation
def fusionDance(left: Saiyan, right: Saiyan): Saiyan = {
  new Saiyan(left.name + right.name, math.min(left.age,right.age))
}
  • Domain Service
  • Infrastructure Service
  • Application Service

DDD Desgin Patterns

  • Extract from the ubiquitous language
    • Entities
    • Value Objects
    • Services
  • Ownership and boundaries
    • Aggregates
  • Storage
    • Repositories

Chaotic Objects associations

Stop

Aggregates

Aggregates

Groups of associated objects regard to transactions

One Aggregate have one root

Root must be Entity

Root have global identity. Internal Entities have local indentity

User
UserID
Name
SocialProfile
Social Profile
Facebook
Twitter
GIthub
Contact Info
PhoneNumber
EmailAddress

Root Entity

Aggregate

Repositories

Repository commonly refers to a storage location, often for safety or preservation.

                                                                                   Wikipedia

In DDD, Repository is more like a "facade" to data store (Database, cache ...)

Repository hold references to root Entity of Aggregates.

Repository != DAL (Data Access Layer)

PostRepository
findPost(cond)
newPost()
updatePost()
...
Post
postId
title
content
Post
postId
title
content
Post
postId
title
content
Search Query
Search Cond

findPost(cond)

posts list

Repository

PostRepository
findPost(cond)
newPost()
updatePost()
...
Post
postId
title
content
Post
postId
title
content
Post
postId
title
content
Search Query
Search Cond

findPost(cond)

posts list

Entity or Value Object ?

DDD with Microservices

  • About me

  • Domain Driven Design

  • DDD approach for Microservices architecture

There is a very, very bad nightmare for every developer

Monolithic is hell

DDD and Microservices are here to help us escape from the hell !

Microservices introduce boundaries

DDD make bounded context

DDD for multiple context

until it make sense :)

Software Engineer life in Japan

  • About me

  • Domain Driven Design

  • DDD approach for Microservices architecture

Realize two worlds

Don't fail into traps

Use your time wisely

Invest to yourself

Build awesome things

Thanks for listening

Domain-Driven Design for Microservices Architecture

By Minh Nhat

Domain-Driven Design for Microservices Architecture

Domain-Driven Design and approach for microservices architecture

  • 5,286