This video titled "How To Implement Domain-Driven Design (DDD) in Go" discusses the implementation of Domain-Driven Design (DDD) in Go for building a microservices-based online Tavern. The video simplifies DDD by breaking it down into manageable pieces and references Eric Evans' book on DDD. It emphasizes the importance of collaboration between engineering teams and subject matter experts (SMEs) in DDD. The video covers the concepts of entities, value objects, aggregates, repositories, factories, and services. It demonstrates the implementation of these concepts in Go, including the creation of separate folders for entities and value objects, the use of structs for aggregates, and the implementation of the repository pattern. The video also discusses the implementation of services and configuration patterns, as well as the addition of a MongoDB repository. The video concludes by mentioning the recommended structure for a domain-driven project and suggesting a billing service as a practice exercise.
Introduction
Domain-Driven Design (DDD) is implemented in Go to build a microservices-based online Tavern. Microservices are popular for their scalability and flexibility. The video simplifies DDD by breaking it down into manageable pieces. Eric Evans' book on DDD is referenced.
Key points:
- DDD is implemented in Go for building a microservices-based online Tavern
- Microservices are popular for scalability and flexibility
- DDD is simplified by breaking it down into manageable pieces
- Eric Evans' book on DDD is referenced
Why this video
The video discusses the implementation of Domain-Driven Design (DDD) in Go. The speaker shares their interpretation of DDD and how to effectively implement it based on their experience with Go. The video covers the terminology and implementation, while a second video will focus on restructuring for better organization. The importance of following the methodology outlined by Eric Evans is emphasized.
What Is Domain Driven Design (DDD)
Domain-driven design (DDD) is a software development approach that emphasizes structuring and modeling software based on the specific domain it belongs to.
Key points:
- DDD requires thorough consideration of the domain or topic that the software intends to work on.
- Collaboration between the engineering team and subject matter experts (SMEs) is crucial in DDD.
- SMEs share their knowledge with the engineers to build a better understanding of the domain.
- The architecture and code of the software should reflect the domain, ensuring alignment with the problem being solved.
The Journey Of Dante (Domain Modelling Session)
The most profound aspect of the text is the introduction of domain-driven design (DDD) and its implementation in Go.
Key points:
- The video tells the story of Dante, a gopher who wants to build an online tavern but lacks business knowledge.
- Dante meets a subject matter expert who offers to help him with his tavern and they have a domain modeling session.
- The session covers the components needed for the tavern domain, including customers, employees, banking, and suppliers.
- The importance of using a unified language, called the ubiquitous language, for clear communication between experts and engineers is emphasized.
- The tavern is identified as the core domain, with customers being a subdomain.
- This session lays the foundation for understanding and implementing DDD in Go.
Entity and ValueObject
Entities and Value Objects are important concepts in Domain-Driven Design (DDD).
Key points:
- Entities are uniquely identifiable and mutable objects, while value objects are non-identifiable and immutable.
- In Go, entities and value objects can be implemented by creating separate folders within the domain folder.
- Entities represent real-world objects and can be accessed and modified from outside the package.
- Value objects are immutable structures used within entities.
- Distinguishing between entities and value objects helps in modeling and designing complex systems within a domain.
Aggregates
An aggregate is a combination of entities and value objects used to identify a group of related objects. Aggregates encapsulate complex business logic within a domain. In Go, aggregates are implemented using structs, with entities and value objects defining their structure. Factories are used to create instances without exposing implementation details. In the context of DDD in Go, a factory function is created for creating a new customer aggregate, validating input parameters and returning an error if necessary. The speaker emphasizes the importance of testing the factory method and demonstrates table-driven tests for both empty and valid names.
Repository Pattern
The Repository Pattern in Domain-Driven Design (DDD) implemented in Go is discussed in the video. The pattern involves hiding implementation details behind an interface, allowing for modular and changeable software. Key points include:
- The Repository Pattern is widely used in various paradigms, including DDD.
- It enables the use of different repositories without extensive code modification.
- The video demonstrates the creation of a customer repository interface and the definition of required functions.
- The presenter shows how to implement an in-memory solution for the repository.
- Functions for fetching, adding, and updating customers are provided.
- The presenter emphasizes the importance of keeping the repository related to the domain.
Services & Configuration Patterns & More Repositories
The implementation of services and configuration patterns in Domain-Driven Design (DDD) using Go is discussed in this video. The speaker demonstrates the creation of an order service and the application of configurations to customize it. They explain the use of a pointer to the order service and how to loop through configurations to apply them. The purpose of this approach is to allow for easy customization of the order service when building larger systems with multiple configurations.
Tavern Service & SubServices
Implementing Domain-Driven Design (DDD) in Go, the video focuses on the Tavern Service and its SubServices. The key points covered are:
- The Tavern Service holds the Order Service and a pointer to it.
- A Billing Service is added as a sub-service.
- A Tavern configuration function is used to configure and apply these services.
- The Order Service is modified to return the total cost of an order.
- The video demonstrates the creation and interaction of the Order Service and Tavern Service.
- The Order Service accepts orders and calculates the total price.
- The Tavern Service initializes the Tavern and handles customer and order creation.
- Testing and debugging of the implemented services are included.
- The video explains how to create a customer and add them to the repository.
- The Order Service is used to make an order and retrieve the price.
- The flexibility of DDD is highlighted by easily replacing in-memory repositories with a MongoDB implementation.
- Data structuring is done within the repository, and different databases may require different formats, but the repository should always conform to the aggregate model in the code.
Adding MongoDB Repository
Adding MongoDB Repository:
- The video demonstrates how to add a MongoDB repository in Go for implementing Domain-Driven Design (DDD).
- The speaker shows how to create a new repository by connecting to MongoDB using a connection string.
- Functions for getting and adding data to the database are implemented.
- The context package is used to handle timeouts and errors.
- The repository is configured in the order service, allowing for easy replacement of the customer repository with the MongoDB repository.
- The implementation involves setting the context, converting the aggregate to an internal format, and inserting it into the collection.
- The service does not need to be concerned about the specific repository implementation as long as it fulfills the interface.
Ending
Domain-Driven Design (DDD) in Go is discussed in this video, covering concepts such as entities, value objects, aggregates, repositories, factories, and services. The recommended structure for a domain-driven project is mentioned. A billing service is suggested as a practice exercise, with the full code available on GitHub. Viewers are encouraged to provide feedback and engage with the video.