php-kitchen/yii2-domain

Yii2 的领域驱动设计(DDD)关键概念实现。

v0.0.35 2020-03-05 20:24 UTC

README

Build Status Tested By Latest Stable Version Monthly Downloads Total Downloads License

Yii2 Domain 是为 Yii2 实现的领域驱动设计(DDD)模式。

包括以下模式的实现

  • 存储库
  • 实体
  • 规范(通过 ActiveQuery 的非常粗略的实现)
  • 策略

需求

PHP >= 7.1 是必需的。

入门

运行以下命令将 Yii2 Domain 添加到项目的 composer.json 文件中。有关特定版本,请参阅 Packagist

composer require php-kitchen/yii2-domain

或者您可以从以下位置复制此库:

有关更多信息和使用指南,请访问 项目文档

概述

此库的目的是将领域驱动设计(DDD)原则引入 Yii2 项目,并解决具有中等和大型领域区域的域层在应用程序中的 ActiveRecord 问题

每个模型都表示为一个独立的目录,其中包含存储库、实体、记录和查询类。所有这些类都表示域模型。

典型模型(以用户模型为例)的目录结构

  user/                    contains all of the classes that represents domain model
        UserRepository     model repository
        UserEntity         model entity(represents domain entity - not the DB table that containd entity information) 
        UserRecord         DB record that contains entity information
        ProfileRecord      DB record with additional information thta also a part of the UserEntity.
        UserQuery          query class of model e.g. - specification of the entity

代码示例

简单搜索和存储

$repository = new UserRepository();
$entity = $repository->findOneWithPk(1);
// do some manipulations with entity
$repository->validateAndSave($entity);

复杂条件搜索和删除

$repository = new UserRepository();
$entity = $repository->find()
		->active()
		->withoutEmail()
		->one();
// do some manipulations with entity
$repository->delete($entity);

注意

此库旨在解决 Yii 2 的 ActiveRecord 问题。如果您正在启动一个新项目并寻找一个可以构建良好架构的解决方案,则不要使用此库 - 您需要一个允许您构建高质量解决方案的良好框架,而 Yii 2 并不是允许您构建高质量架构和实现丰富域层的框架。

"Yii2Domain" 库是一个支架,旨在解决由 ActiveRecord 在现有项目中引起的域层问题。在新项目中构建域层的解决方案并不多。

贡献

如果您有任何问题,建议改进或只是想与社区和开发者交谈,请加入我们的 Discord 服务器https://discord.gg/Ez5VZhC