jeudi 24 novembre 2016

Use special 'propose' object's instead of arrays

its gonna be a fully theoretical thread.

Let's talk about change arrays to specially object's, to compare and work on it.

For example we have a EntityClass, EntityInterface, SomeRepository, SomeManager, SomeCommand.

Entities is a clear object, example:

class EntityClass implements EntityInterface {
    public $name;

    public funtion getName() {
        return $this->name;
    }

    public function assign($data) {
        $this->name = $data['name'];
    }
...
}

Repository ofc. have method's to save object in source and get it from source.

Manager have all of 'busines logic', it can modify a entities using command pattern, one command for one properties, so a busines logic for all properties its store in separate's command's, and manager fire it's.

Now, in manager start all logic and fun. Little shorthand:

  1. Repository create new entity by getOne().
  2. We create a new instance of manager and pass by constructor some dependencies like array of data from controller and Entity.
  3. Array of data have information about changes, example: ['data' => 'New name']; mapper resolve what command should have been fired by manager for given array.
  4. Manager execute all commands for this request and passing raw array to each command.

Now, why we passing a array ? when we are in OOP, why don't use special variation of EntityClass ??

Now let's add a new interface EntityProposeInterface, and change a raw array to class implements this interface, and pass them.

for example we can add to SomeManager speciall method to morph entity like this (PHP7+):

class SomeManager {
   public function morph($entity) {
       $buff = new class extends EntityClass implements EntityProphoseInterface{};
       $buff->assign($entity->toArray());
       return $buff; 
  }

And now we have a EntityProphose lets make some changes on it, now manager change a EntityProphose from raw data array, and all commands and other method's in our code work's on object kinde EntityProphose instead of array.

But ofc. our repository cant save object's instance of EntityProphoseInterface.

And that's all... Is there some design pattern name ? or something like this ?

Or this idea is very bad ?

I hope it's clear for all of you, if not please ask. Any ideas ? advice ?

Aucun commentaire:

Enregistrer un commentaire