Skip to main content

A static code analyzer that checks the isolation of bounded contexts in Domain-Driven Design (DDD) projects

Project description

Bounded Contexts Linter

A static code analyzer that checks the isolation of bounded contexts in Domain-Driven Design (DDD) projects. This linter ensures that dependencies between modules occur only within a single bounded context, helping to maintain proper isolation between different domains in your application.

Overview

In Domain-Driven Design, bounded contexts are a central pattern that helps in dealing with large models by dividing them into different contexts with explicit boundaries. This linter helps enforce these boundaries by:

  1. Checking that modules don't belong to multiple bounded contexts
  2. Verifying that imports between modules respect bounded context isolation

The linter recognizes two special contexts by default:

  • sharedkernel: Contains modules that can be imported by any context
  • sharedscope: Contains interfaces for interaction between contexts that can be safely imported

See the details of the work on the examples below.

Installation

Requirements

  • Python 3.11 or higher

Install from PyPI

pip install bounded-contexts-linter

Install with flake8 integration

pip install bounded-contexts-linter[flake8]

Usage

Command Line Interface

Run the linter on your project:

bc-linter /path/to/your/project [/path/to/bounded-contexts.toml]
bounded-contexts-linter /path/to/your/project [/path/to/bounded-contexts.toml]

If the configuration file path is not provided, the linter will look for bounded-contexts.toml in the current working directory.

Flake8 Integration

If you've installed the linter with flake8 integration, you can use it as a flake8 plugin:

flake8 /path/to/your/project

Make sure to configure flake8 to use the plugin by adding the following to your .flake8 file:

[flake8]
enable-extensions = BC

Configuration

The linter uses a TOML configuration file to define bounded contexts and their module patterns. By default, it looks for bounded-contexts.toml in the current working directory.

Example Configuration

[bounded-contexts]
names = ["sharedkernel", "sharedscope", "sales", "crm", "user"]

[bounded-contexts.sharedkernel]
description = "Shared kernel modules that can be imported by any context"
contains = [
    "project.domains.user.*",
]

[bounded-contexts.sharedscope]
description = "Interface modules for interaction between contexts"
contains = [
    "project.domains.*.port",
]

[bounded-contexts.crm]
description = "Customer Relationship Management context"
contains = [
    "project.domains.crm.models",
    "project.domains.crm.services",
]

[bounded-contexts.sales]
description = "Sales context"
contains = [
    "project.domains.sales.models",
    "project.domains.sales.services",
]

Pattern Syntax

The contains field in each bounded context section accepts a list of patterns that define which modules belong to that context. The pattern syntax supports:

  • Exact module names: project.domains.crm.models
  • Wildcards: project.domains.crm.*
  • Multiple wildcards: *.crm.*

Examples:

  • project.utils - Matches exactly the module named "project.utils"
  • project.domains.crm.* - Matches all modules under "project.domains.crm"
  • project.domains.*.models - Matches all modules starting with "project.domains." and ending ".models"
  • *.crm.* - Matches all modules containing ".crm."

How It Works

The linter performs the following checks:

  1. Overlapping Modules Check: Ensures that no module belongs to multiple bounded contexts (except for sharedkernel and sharedscope)
  2. Import Isolation Check: Ensures that modules from one bounded context don't import modules from another bounded context (except for sharedkernel and sharedscope)

The linter searches for modules in the specified project directory and checks their imports against the bounded contexts defined in the configuration file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bounded_contexts_linter-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bounded_contexts_linter-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file bounded_contexts_linter-1.0.0.tar.gz.

File metadata

File hashes

Hashes for bounded_contexts_linter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 cae1d0366134ae0fe60d6d355e9a1648aab88de1d832648b6611e7fcafb49ee2
MD5 66bdb066967658c2ccf77fc5b0fa1e9f
BLAKE2b-256 419d4454036eb43873d7579faf041ad0eb805ee53f81304b03e5a250c0d0c9d4

See more details on using hashes here.

File details

Details for the file bounded_contexts_linter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bounded_contexts_linter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0835edf5ed158c26fbd24bccb9224adb80e8b1c3cffa095b06f2b51c204270f
MD5 126e74883f2191a75d861b0f4b029f7a
BLAKE2b-256 c3fe21ecd5b0bc475880fb0e97e8d8a251cb74f0e75335ee786286ecc0f4f790

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page