14 Nov 2023



Intermediate

In Domain-Driven Design (DDD), a shared kernel is a concept where two or more bounded contexts agree on a common set of elements, such as code, databases, or APIs, to facilitate communication and collaboration between them. This shared kernel can help avoid duplication of effort and ensure consistency across different parts of a system. Here are a few examples of how a shared kernel might be used in DDD:

  1. Common Codebase:

    • Scenario: Two bounded contexts, Sales and Inventory, need to collaborate on pricing calculations.
    • Shared Kernel: A common library or module containing pricing algorithms and related code is shared between the Sales and Inventory contexts.
  2. Shared Database Schema:

    • Scenario: Order Management and Shipping are two bounded contexts that need to exchange information about orders and their status.
    • Shared Kernel: A shared database schema or a set of database views is defined and maintained by both contexts to ensure consistency in the representation of order data.
  3. Common API:

    • Scenario: An e-commerce system has separate bounded contexts for Product Catalog and Shopping Cart, but they need to exchange information about available products and user cart contents.
    • Shared Kernel: A common API, possibly a set of RESTful endpoints, is defined and agreed upon by both contexts to enable communication between the Product Catalog and Shopping Cart.
  4. Shared Data Transfer Objects (DTOs):

    • Scenario: Billing and Customer Management are two contexts that need to exchange customer information.
    • Shared Kernel: A set of shared data transfer objects (DTOs) is defined to represent customer data, ensuring that both contexts understand and use the same structure when exchanging customer information.
  5. Common Messaging Protocol:

    • Scenario: Bounded contexts for Order Processing and Shipping need to communicate asynchronously through a message broker.
    • Shared Kernel: A common messaging protocol, such as a specific message format or set of message types, is agreed upon and shared between the two contexts to facilitate seamless communication.
  6. Shared Identity Management:

    • Scenario: User Authentication and Authorization are separate contexts that need to ensure consistent identity management.
    • Shared Kernel: A shared identity management system is used to authenticate and authorize users consistently across both contexts.
  7. Common Event Types:

    • Scenario: Multiple bounded contexts, such as Order Management and Customer Relationship Management (CRM), need to react to certain events, like order placed or customer updated.
    • Shared Kernel: Define a common set of event types (e.g., OrderPlacedEvent, CustomerUpdatedEvent) that are shared and understood by both contexts, allowing them to react consistently to these events.
  8. Unified Logging and Monitoring:

    • Scenario: Different bounded contexts generate logs and monitoring data, and there's a need for a unified approach to tracking and analyzing system behavior.
    • Shared Kernel: Implement a shared logging and monitoring infrastructure that all contexts contribute to, ensuring a consistent approach to logging and monitoring across the system.
  9. Standardized Code Conventions:

    • Scenario: Development teams working on different bounded contexts follow different coding conventions, leading to inconsistency in code styles.
    • Shared Kernel: Establish and document a set of standardized code conventions, such as naming conventions, code formatting, and documentation standards, which are shared and adhered to by all teams working on various contexts.
  10. Common Business Rules Engine:

  • Scenario: Bounded contexts dealing with similar business rules, like discounts or promotions, want to ensure consistency in rule execution.
  • Shared Kernel: Implement a common business rules engine that encapsulates and executes shared business rules. All relevant contexts can use this engine to apply consistent rules.
  1. Master Data Management:
  • Scenario: Multiple contexts require access to common master data (e.g., product catalog, customer information) but may have their own specific data as well.
  • Shared Kernel: Establish a shared master data management system that contains and maintains the common data shared across contexts, ensuring consistency and integrity in master data.
  1. Shared User Interface Components:
  • Scenario: Different parts of a system, each represented by a bounded context, need to provide a consistent user experience.
  • Shared Kernel: Create a shared library or set of components for user interface elements (e.g., buttons, forms, styles) to maintain a consistent look and feel across different contexts.
  1. Common Security Policies:
  • Scenario: Various contexts within a system need to enforce similar security policies, such as authentication and authorization mechanisms.
  • Shared Kernel: Implement a shared security framework that includes common authentication and authorization mechanisms, ensuring a unified approach to security across different contexts.
  1. Agreed-upon Data Serialization Format:
  • Scenario: Bounded contexts need to exchange data, and a consistent data serialization format is crucial for interoperability.
  • Shared Kernel: Agree on a common data serialization format, such as JSON or Protocol Buffers, for communication between contexts.
domain-driven-design-ddd
shared-kernel
examples