dekeysoft / yii2-domain

此包已被废弃,不再维护。作者建议使用https://github.com/php-kitchen/yii2-domain 包。

Yii2 的 DDD 核心概念实现。


README

logo.png

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

Yii2 Domain 是 Yii2 的领域驱动设计模式实现。

包括以下模式的实现

  • 仓库
  • 实体
  • 规范(通过 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服务器