@ddd-framework/core

中文文档
1.0.13发布于 2024/12/23MITTypeScriptCommonJS零依赖近 30 天下载 7.9万

@ddd-framework/core is a package within the comprehensive @ddd-framework framework. It provides essential functionalities and classes for implementing Domain-Driven Design (DDD) specifically for Node.js.

原始使用文档
文档目录6 节
  1. Introduction
  2. Documentation
  3. Installation
  4. Features
  5. Usage
  6. License

@ddd-framework/core

@ddd-framework/core is a package within the comprehensive @ddd-framework framework. It provides essential functionalities and classes for implementing Domain-Driven Design (DDD) specifically for Node.js.

Introduction

The @ddd-framework/core package is a fundamental component of the @ddd-framework. It offers a range of features and tools that are crucial for incorporating DDD principles into Node.js applications. This package plays a significant role in streamlining the development process, improving code organization, and creating robust and maintainable applications based on DDD principles.

Documentation

For more detailed TypeDoc-generated documentation on @ddd-framework/core, please refer to the official documentation.

Installation

To install @ddd-framework/core, ensure you have the following prerequisites:

  • Node.js stable version
  • pnpm

Once the prerequisites are installed, run the following command:

$ pnpm i @ddd-framework/core
...

This will install the @ddd-framework/core package and its dependencies.

Features

The @ddd-framework/core package offers a set of features that align with the principles of Domain-Driven Design (DDD) to facilitate the development of Node.js applications:

  • Aggregates and Entities: Provides decorators and classes such as AggregateRoot and Entity to model and manage domain aggregates and entities. These classes encapsulate behavior, enforce business rules, and enable consistency within the domain model.
  • Domain Events: Offers the DomainEvent decorator to represent and handle domain events. Domain events are used to capture meaningful occurrences within the domain and allow decoupled communication between different parts of the application.
  • Value Objects: Includes the ValueObject class to define immutable value objects. Value objects encapsulate small, cohesive pieces of data with their own rules and behaviors, contributing to the integrity and expressiveness of the domain model.
  • Domain Primitives: Provides domain primitive classes such as DateValue that encapsulate specific domain concepts and ensure type safety and consistency within the domain model.
  • Repositories: Provides the Repository class, which serves as an abstraction to access and persist aggregates and entities in a domain-agnostic manner. Repositories enable a consistent and standardized way to retrieve and store domain objects.
  • Unit of Work: Offers the UnitOfWork and UnitOfWorkManager classes to implement the Unit of Work pattern. These classes provide a way to manage the transactional consistency and coordination of changes across multiple aggregates or entities within a single operation.
  • Exception Handling: Provides a range of exception classes, including ArgumentException, InvalidOperationException, and more, to handle exceptional situations and errors that can occur during domain operations.

By leveraging these features from the @ddd-framework/core package, developers can effectively implement DDD concepts such as aggregates, entities, domain events, and repositories in their Node.js applications, leading to more maintainable, expressive, and domain-focused code.

Usage

To use @ddd-framework/core, import the desired classes, interfaces, or functions from the package. For example:

import {
  AggregateId,
  AggregateRoot,
  Entity,
  EntityId,
  IllegalStateException
} from '@ddd-framework/core';
import { faker } from '@faker-js/faker';

@AggregateRoot()
export class GiftCard extends Entity {
  @AggregateId()
  public id: string;

  public remainingValue: number;

  public transactions: GiftCardTransaction[] = [];

  constructor(id: string, initialBalance: number) {
    super();
    this.id = id;
    this.remainingValue = initialBalance;
  }

  @Entity.Invariant()
  public handle(cmd: TransactCommand | ReimburseCardCommand) {
    if (cmd instanceof TransactCommand) {
      const transaction = new GiftCardTransaction(
        faker.string.uuid(),
        cmd.transactionValue
      );

      this.remainingValue -= transaction.transactionValue;

      this.transactions.push(transaction);

      this.raise(
        new TransactionEvent(
          this.id,
          transaction.transactionId,
          transaction.transactionValue
        )
      );
    } else if (cmd instanceof ReimburseCardCommand) {
      const transaction = this.transactions.find(
        (i) => i.transactionId === cmd.transactionId
      );

      if (transaction) {
        this.remainingValue += transaction.transactionValue;
        transaction.handle(cmd);
      }
    }
  }

  protected validateInvariants(): void {
    if (this.remainingValue < 0)
      throw new IllegalStateException('Gift card cannot be overdrawn');
  }
}

License

@ddd-framework/core is released under the MIT License. Feel free to customize it further to fit your needs.

版本与发布记录

@ddd-framework/core 共发布 19 个版本,首次发布于 2022/08/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.13latest2024/12/231.9 年前
1.0.122024/12/221.9 年前
1.0.112024/12/221.9 年前
1.0.102024/12/221.9 年前
1.0.92024/12/221.9 年前
1.0.82024/08/072 年前

关于 @ddd-framework/core 的常见问题

@ddd-framework/core 是什么?

@ddd-framework/core is a package within the comprehensive @ddd-framework framework. It provides essential functionalities and classes for implementing Domain-Driven Design (DDD) specifically for Node.js.

如何安装 @ddd-framework/core?

在项目目录运行 npm install @ddd-framework/core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:reflect-metadata(^0.1.13 || ^0.2.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ddd-framework/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ddd-framework/core 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ddd-framework/core 可以商用吗?

@ddd-framework/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ddd-framework/core 的下载量有多少?

根据 npm 官方下载统计,@ddd-framework/core 在 2026/10/12 – 2026/11/10 这 30 天内被下载 79,014 次,日均约 2,634 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ddd-framework/core 还在维护吗?

最新版本 1.0.13 发布于 2024/12/23(1.9 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/08/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。