承接 magomogo/domain-model 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

magomogo/domain-model

最新稳定版本:v3.0.0

Composer 安装命令:

composer require magomogo/domain-model

包简介

A way to make domain-specific models persistent

README 文档

README

Build Status

Intro

  • I don't like ActiveRecord because of tight coupling with the database,
  • I am not so comfortable with Data mappers because a mapper breaks model encapsulation,
  • I like the idea to keep 'The State' separately from the logic.

Here is the way

The concept

To implement an entity, one should create two classes: Model and Properties. Model class contains domain-specific logic only. All state that should persist is located in Properties. Model has its Properties aggregated:

class Model implements ModelInterface
{
    /**
     * @var Properties
     */
    private $properties;

    public function __construct($properties)
    {
         $this->properties = $properties;
    }
}

This library takes care about the Properties, all its public fields and also relations can be saved/loaded in the Containers. Currently there is SqlDb, CouchDb, and Memory container.

It is recommended to declare Model's constructor signature that doesn't allow to create an instance that makes no sense from the business logic point of view.

$person = new Person\Model($propertiesBag);
$employee = new Employee\Model($company, $propertiesBag);

source: Person\Model | Employee\Model

Obvious responsibilities

To achieve persistency we don't need to store A model, it is necessary to store its properties.

// save/update
$dbContainer = new Persisted\Container\SqlDb($connection);
$person->save($dbContainer);

// load
$persistedPerson = Person\Model::load($dbContainer, $id);

source: Persisted\Container\SqlDb

Handling user input with 'Editors', 'A Editor' is kind of a Container.

$editor = new ProfileEditor($person);
// validation here
$editor->edit($userInput);

$editedPerson = Person\Model::load($editor);
$editedPerson->save($dbContainer);

Strong separation between different types of object relations

For example person properties can have contact info aggregated, it gets stored and updated together with person:

$contactInfoModel = new ContactInfo\Model($contactInfoProperties);

$personProperties = array(
    'name' => 'John',
    'contactInfo => $contactInfoModel
);

On the other hand, there is a person who's working in a company. These objects are connected by foreign key and created/updated separately.

$company->save($dbContainer);
$employee = new Employee\Model($company, $employeeProperties);

// this won't update the company, but create one-to-many reference company -> person in the container
$employee->save($dbContainer);

A model can have a list of another models connected. This so-called many-to-many relation is possible using Collections.

$collection = new Keymarker\Collection;
$collection['Example'] = new Keymarker\Model(new Keymarker\Properties(array('name' => 'Example'));

Examples

See test cases to learn recommended usage:

...the work is in progress...

统计信息

  • 总下载量: 10.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-12-12