Introduction

Today we are going to discuss domain driven design (DDD) approach and why we need this and its usage, on top of that, why the current industry is focusing on this approach.

What is Domain Driven Design Approach?

Firstly this approach navigates from top to bottom scenarios such as the main focus for this approach relates to the business instead of the technology stack. The domain should be as it is currently what the customer use case scenario. Typically as a starter, we use technology first instead of business, and we call this as a tactical solution, which starts from the bottom to the top approach, however, in this case, it is vice versa.

The focus of this approach is to understand the business use case and then drill down to the granular layer. Consider a large system such as ERP, it has huge domains and subdomains such as Manufacturing, Supply chain, HRMS as domain and Payroll, Leave Management, LMS, and talent management are subdomains of HRMS and etc.,

Types of Designing Software

Architecture Glance

Key points while designing domain Model

Concepts of Domain-Driven Design

Name Brief Description Example
Ubiquitous Language It emphasizes the use of a common, shared language between domain experts and developers. This language, known as the "ubiquitous language," should be used in all aspects of the software development process, including code, documentation, and conversations. It helps to bridge the gap between domain experts and developers, ensuring a clear and consistent understanding of the problem domain. While exposing an API, the action should be a proper business use case. For eg.,
  • GetEmployeeByEmployeeId(int) instead of GetEmpData(int)
  • Create a class name and object to adhere to the business, here meant to say tech stack or coding should align.
Bounded Context A bounded context represents a specific area or subsystem within the overall system where a particular domain model and ubiquitous language apply. It defines the boundaries within which the models, concepts, and language are consistent and meaningful. Bounded contexts help manage the complexity of large domains by breaking them down into smaller, more manageable parts. Setting the boundary for the domain model. For eg., It only does its job not everything else.
Aggregates Aggregates are clusters of closely related objects that are treated as a single unit. They are responsible for enforcing consistency and invariants within the domain. An aggregate has a root entity, known as the aggregate root, which acts as the entry point for accessing and modifying the objects within the aggregate. Aggregates help maintain transactional consistency and encapsulate complex business logic It's a kind of orchestra, which connect all the required object to run your business.
Entities Entities are objects that have a unique identity and are defined by their attributes and behaviour. They represent concepts with continuity over time and are typically mutable. Entities encapsulate the core business logic and behaviour of the domain. Entities could be customers, orders, or products
Value Objects Value objects are objects that do not have a unique identity but are defined by their attributes. They represent concepts based solely on their attribute values and are immutable. They help enforce semantic integrity and provide meaningful abstractions. Value objects could be dates, addresses, or monetary values.
Domain Services Domain services encapsulate operations or behaviours that do not naturally fit within a specific entity or value object. They typically represent domain operations that require the collaboration of multiple objects or involve external dependencies. Domain services can be stateless or stateful and play a crucial role in implementing complex business processes. LMS service will do all kinds of learning and the payroll service will take care of only salary dispatch.
Domain Events Domain events represent significant occurrences or changes within the domain. They capture and communicate meaningful business activities or state transitions. Domain events are used to maintain loose coupling and enable different parts of the system to react to changes asynchronously. They can be published and subscribed to by interested components within the system. Once the order is placed in the e-commerce platform, the ordering service will pick the events from the queue it will proceed further. Once it's done it will set it back to the queue which is taken care of by the rest of the service accordingly.
  • Ordering => Queue => Payment
  • Payment done => Queue => Inventory
  • Inventory done=> Queue => Shippment
Repositories Repositories provide a bridge between the domain model and the data persistence layer. They encapsulate the logic for retrieving and storing aggregates from/to the underlying data store. Repositories abstract away the details of data access, allowing the domain model to remain focused on business logic without being tightly coupled to specific persistence mechanisms. This act as a connector to get the data from infrastructure layer.