Implementing Domain Driven Design (A practical guide for implementing the Domain Driven Design with the ABP Framework)

发布时间 2023-09-02 19:36:49作者: 天行健君子以自强

CONTENTS

Introduction..................................................

  • Goal......................................................

  • Simple Code ...................................................................

What is the Domain Driven Design?......................................................................................

  • OOP & SOLID.........................................................

  • DDD Layers & Clean Architecture........................................

  • Core Building Blocks....................................................

Implementation: The Big Picture...............................................................................................

  • Layering of a .NET Solution......................................

  • Dependencies of the Projects in the Solution ...........................

  • Execution Flow of a DDD Based Application .....................................

  • Common Principles ...........................................................

Implementation: The Building Blocks ..........................................

  • The Example Domain ..........................................................

  • Aggregates ....................................................................

  • Repositories ...................................................................

  • Specifications .............................................................

  • Domain Services ....................................................................

  • Application Services ...............................................................

  • Data Transfer Objects ...............................................................

Example Use Cases .....................................................................

  • Entity Creation .............................................

  • Updating / manipulating An Entity ...........................

Domain Logic & Application Logic............................

  • Multiple application Layers..........................
  • Examples.....................................

Reference Books...................

INTRODUCTION

This is a practical guide for implementing the Domain Driven Design (DDD). While the implementation details rely on the ABP Framework infrastructure, core concepts, principles and patterns are applicable in any kind of solution, even if it is not a .NET solution.

Goal

The goals of this book are:

  • Introduce and explain the DDD architecture, concepts, principles, partterns and build blocks.

  • Explain the layered architecture & solution structure offered by the ABP Framework.

  • Introduce explicit rules to implement DDD partterns and best practices by giving concrete examples.

  • Show what ABP Framework provides you as the infrastructure for implementing DDD in a proper way.

  • And finally, provide suggestions based on sofware development best practices and our experiences to create a maintainable codebase.

Simple Code!

Playing football is very simple, but playing simple football is the hardest thing there is. ----Johan Cruyff

if we take this famous quote for programming,we can say:

Writing code is very simple, but writing simple code is the hardest thing there is. ----???

In this document, we will introduce simple rules, those are easy to implement.

Once your application grows, it will be hard to follow these rules. Sometimes you find **b

if you follow the rules and bset practices, your code base will be simpler and easier to maintain. Your application reacts to changes faster.

What is the Domain Driven Design?

Domain-driven design(DDD) is an approach to software development for complex needs by connecting the implementation to an evolving model;

DDD is suitable for complex domains and large-scale applications rather than simple CRUD applications. It focuses on the core domain logic rather than the infrastructure details. It helps to build a flexible, modular and maintainable code base.

OOP && SOLID

Implementing DDD highly relies on the Object Oriented Programming(OOP) and SOLID principles. Actually, it implements and extends these principles. So, a good understanding of OOP & SOLID helps you a lot while truly implementing the DDD.

DDD Layers & Clean Architecture

There are four fundamental layers of a Domain Driven Based Solution.

Business Logic places into two layers, the Domain layer and the Application Layer, while they contain different kinds of business logic:

  • Domain Layer implements the core, use-case independent business logic of the domain/system.

  • Application Layer implements the use cases of the

  • Presentation Layer contains the UI elements(pages,components) of the application.

  • Infrastructure Layer supports other layer by implementing the abstractions and integrations to 3rd-party library and systems.

The same layering can be shown as the diagram below and known as the Clean Architecture, or sometimes the Onion Architecture:

In the Clean Architecture, each layer only depends on the layer directly inside it. The most independent layer is shown in the most inner circle and it is the Domain Layer.

Core Building Blocks

DDD mostly focuses on the Domain & Application Layers and ignores the Presentation and Infrastructre. They are seen as details and the business layers should not depend on them.

That doesn't mean the Presentation and Infrastructure layers are not important. They are very important. UI frameworks and database providers have their own rules and best practices that you need to know and apply. However these are not in the topics of DDD.

This section introduces the essential building blocks of the Domain & Application Layers.

Domain Layer Building Blocks

  • Entity: An Entity is an object with its own properties(state,data) and methods that implements the business logic that is executed on these properties. An entity is represented by its unique identifier(Id). Two entity object with different Ids are considered as different entities.

  • **Value Object: A Vaule Object is another kind of domain object that is identified by its properties rather than a unique Id. That means two Vaue Objects with same pro