Przejdź do głównej zawartości

Domain-Driven Design with AI Assistance

Ta treść nie jest jeszcze dostępna w Twoim języku.

Domain-Driven Design (DDD) is an approach to software development that focuses on modeling the software to match the domain of the business. It’s a powerful methodology for tackling complexity, but it requires a deep, shared understanding of the domain. An AI assistant can be an invaluable partner in this process, helping you to define, implement, and protect your domain model.

With AI, you can move faster without sacrificing the conceptual integrity of your system. The AI becomes a custodian of your domain knowledge, ensuring that the code always reflects the business logic.

Applying DDD with an AI assistant involves a collaborative process where you provide the domain expertise, and the AI helps you translate that expertise into a well-structured, maintainable codebase.

1. Teach the Ubiquitous Language

The foundation of DDD is the “Ubiquitous Language”—a shared vocabulary between developers and domain experts. Your first step is to teach this language to your AI assistant by documenting it in your project’s context files (CLAUDE.md or .cursor/rules).

2. Model the Domain

Collaborate with the AI to identify the core components of your domain model: Aggregates, Entities, and Value Objects. You can describe the business processes, and the AI can help you structure them into a coherent model.

3. Define Bounded Contexts

Work with the AI to partition your domain model into Bounded Contexts. The AI can help identify logical boundaries and suggest how different contexts should communicate (e.g., via Domain Events or Anti-Corruption Layers).

4. Implement and Enforce

Instruct the AI to generate the code for your domain objects and repositories. Crucially, you can also use the AI as a “guardian” of your domain model, asking it to review code for adherence to DDD principles and the Ubiquitous Language.


Let’s consider an e-commerce application and see how you might use an AI assistant to model the “Order” Bounded Context.

  1. Establish the Ubiquitous Language. You start by defining the core terms in your CLAUDE.md or a .cursor/rules/domain.mdc file.

    # Ubiquitous Language for the Order Context
    - **Order:** Represents a customer's request to purchase products. An Aggregate Root.
    - **LineItem:** An entry in an Order for a specific product and quantity. A Value Object.
    - **OrderStatus:** The current state of an Order (e.g., Pending, Shipped, Delivered). An Enum.
    - **Customer:** The person who placed the Order. An Entity.
  2. Model the Aggregate. Now, you can ask the AI to design the Order aggregate.

    Based on our Ubiquitous Language, design the `Order` aggregate. It should include a list of `LineItems` and the `CustomerId`. It should have methods to `addItem` and `changeStatus`. Generate a class diagram for this aggregate.

    The AI will not only generate the code structure but can also create a visual representation (e.g., in Mermaid syntax) to help you validate the model.

  3. Generate the Implementation. Once you’re happy with the design, you can ask for the full implementation.

    Implement the `Order` aggregate, `LineItem` value object, and `OrderRepository` interface in TypeScript. Ensure all code strictly adheres to the Ubiquitous Language. The `addItem` method should enforce the invariant that an order cannot have more than 10 unique line items.

    The AI will generate the code, including the business logic to protect your domain’s invariants.

  4. Enforce Architectural Rules. You can use the AI for ongoing architectural governance.

    Review this pull request. Does the code in the `Shipping` Bounded Context directly access the `OrderRepository`? It should be listening for an `OrderShipped` domain event instead.

By integrating your AI assistant into your DDD process, you create a powerful partnership. The AI handles the boilerplate and helps you think through the model, while you provide the critical domain insights. The result is a codebase that is not only functional but also a true reflection of your business domain.