2017 © Pedro Peláez
 

library domain-model

A way to make domain-specific models persistent

image

magomogo/domain-model

A way to make domain-specific models persistent

  • Monday, February 5, 2018
  • by Magomogo
  • Repository
  • 1 Watchers
  • 7 Stars
  • 3,902 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 14 Versions
  • 2 % Grown

The README.md

Persisted models

Build Status, (*1)

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:, (*2)

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., (*3)

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., (*4)

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

source: Person\Model | Employee\Model, (*5)

Obvious responsibilities

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

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

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

source: Persisted\Container\SqlDb, (*7)

Handling user input with 'Editors', 'A Editor' is kind of a Container., (*8)

$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:, (*9)

$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., (*10)

$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., (*11)

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

Examples

See test cases to learn recommended usage:, (*12)

...the work is in progress..., (*13)

The Versions

05/02 2018

dev-master

9999999-dev https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

05/02 2018

v1.0.1

1.0.1.0 https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

10/09 2016

v1.0.0

1.0.0.0 https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

10/09 2016

0.8.x-dev

0.8.9999999.9999999-dev https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

29/05 2016

dev-legacy

dev-legacy https://github.com/Magomogo/persisted-models/

Domain model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

05/03 2014

dev-props-in-props

dev-props-in-props https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

03/10 2013

0.7.x-dev

0.7.9999999.9999999-dev https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

26/09 2013

0.6.x-dev

0.6.9999999.9999999-dev https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko
by Anton Bondar

19/09 2013

0.5.x-dev

0.5.9999999.9999999-dev https://github.com/Magomogo/persisted-models

A way to make domain-specific models persistent

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

26/08 2013

0.4.x-dev

0.4.9999999.9999999-dev https://github.com/Magomogo/best-domain-model/

Domain model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

02/07 2013

0.3.x-dev

0.3.9999999.9999999-dev https://github.com/Magomogo/best-domain-model/

Domain model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

11/06 2013

0.2.x-dev

0.2.9999999.9999999-dev https://github.com/Magomogo/best-domain-model/

Domain model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

28/01 2013

0.1.x-dev

0.1.9999999.9999999-dev https://github.com/Magomogo/best-domain-model/

Domain model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Gnatenko

12/12 2012

dev-php5.4

dev-php5.4 https://github.com/Magomogo/best-domain-model/

Domain model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Maxim Gnatenko