Modularity and Layered Data Model

0 %
100 %
Information about Modularity and Layered Data Model
Technology

Published on April 26, 2014

Author: AttilaJenei

Source: slideshare.net

Description

Modularity, Soft References, Layered Data Model, Entity - Table - TableGateway

PHP examples

Modularity and Layered Data Model Attila Jenei 2014. ! www.attilajenei.com

Modularity

Modularity Our problems: • Long development time • Generic functionality in all projects • Near identical operation of same features • Hard maintainability

Modularity What we need? • Compatible components • High reusability • Exchangeable parts • Configurable system • Improved maintainability

High-level Modularity We can • Remove hard-coded class calls • Turn Singletons into services • Use dynamic table names for data layer • Define keys for soft referencing

Soft References Keys to • Class names - simple “new” • Factories - complex initialization • Aliases - reusability • Initializers - additional initialization

Soft References Managers • Handling references • Lazy loading for performance and low-memory profile • Shared or own instances • We can use cloning to prevent recurring initializations

PHP Example $user = new User; ! // turns to ! $user = clone $services->get(‘ModelUser’);

PHP Example $path = Config::get(‘ViewPath’); ! // turns to ! $path = $services->get(‘MyConfig’)->get(‘ViewPath’);

Example Config array(
 ‘invokables’ => array(
 ‘ModelUser’ => ‘ProjectModelUser’
 ),
 ‘factories’ => array(
 ‘ModelUserTable’ => function ($sm) {
 return ProjectModelUserTable(
 $sm->get(‘ModelUserTableGateway’));
 },
 ‘ModelUserTableGateway’ => function ($sm) {
 return ProjectModelUserTableGateway(
 $sm->get(‘ZendDbAdapterAdapter’),
 ‘user’,
 $sm->get(‘ModelUser’));
 },
 ‘ZendDbAdapterAdapter’ => ‘ZendDbAdapterAdapterServiceFactory’,
 ),
 );

A Simple Service Manager public function get($name)
 {
 if (isset($this->instances[$name])
 {
 return $this->instances[$name];
 }
 
 if (isset($this->invokables[$name]))
 {
 $className = $this->invokables[$name];
 if (class_exists($className))
 {
 $instance = new $className;
 }
 else
 {
 throw new Exception(‘Unknown class: ’ . $className);
 } 
 }

A Simple Service Manager else
 if (isset($this->factories[$name])
 {
 $factory = $this->factories[$name];
 if ($factory instanceof FactoryInterface)
 {
 $factory = array($factory, ‘createService’);
 }
 
 if (is_callable($factory)
 {
 $instance = call_user_func($factory, $this, $name);
 }
 else
 {
 throw new Exception(‘Wrong factory: ’ . $name);
 }
 }

A Simple Service Manager else
 {
 throw new Exception(‘Unknown key: ’ . $name);
 }
 
 if ($instance)
 {
 $this->instances[$name] = $instance;
 
 return $instance;
 }
 
 throw new Exception(‘No instance: ’ . $name);
 }

Data & Responsibility

Higher-level OOP • Database engine independent • Unknown “outside world” • Focus (only) on own tasks • An entity class holds the information and has its related methods

Entity • Represents the information • ≠ database record • Takes part in execution

Entity An Object represents a Database Record. A Database Record stores the information of an Object.

Example: CMS • Page as entity • Control flow ! • Column as entity • Display flow

Data Model

Layers ENTITY Information TABLE Entities TABLE GATEWAY Database Interface HYDRATOR Object - Information

Entity Class abstract class Entity
 {
 protected $serviceLocator;
 protected $storedPrimaryKey;
 protected $table;
 
 final public function getServiceLocator()
 {
 return $this->serviceLocator;
 }
 
 final public function getStoredPrimaryKey()
 {
 return $this->storedPrimaryKey;
 }
 
 final public function getTable()
 {
 return $this->table;
 }

Entity Class final public function setServiceLocator(Ser…ace $serviceLocator)
 {
 $this->serviceLocator = $serviceLocator;
 return $this;
 }
 
 final public function setStoredPrimaryKey(array $storedPrimaryKey)
 {
 $this->storedPrimaryKey = $storedPrimaryKey;
 return $this;
 }
 
 final public function setTable(Table $table)
 {
 $this->table = $table;
 return $this;
 }

Entity Class public function delete()
 {
 if (!$this->storedPrimaryKey)
 {
 throw new Exception(‘Entity is not stored’);
 }
 
 $this->table->delete($this->storedPrimaryKey);
 $this->storedPrimaryKey = array();
 
 return $this;
 }
 
 abstract public function exchangeArray(array $data);

Entity Class public function save()
 {
 $this->table->save($this);
 $reloaded = $this->table->fetchAll($this->storedPrimaryKey)->current();
 
 if ($reloaded)
 {
 $this->exchangeEntity($reloaded);
 }
 else
 {
 throw new Exception(‘Error on reload’);
 }
 }
 }

Example: User Entity class User extends Entity
 {
 protected $name;
 protected $userID;
 
 public function getName() {…}
 
 public function getUserID() {…}
 
 public function setName($name) {…}
 
 public function setUserID($userID) {…}
 
 .
 .
 .


Example: User Entity public function exchangeArray(array $data)
 {
 $this->name = isset($data[‘name’]) ? $data[‘name’] : null;
 $this->userID = isset($data[‘userID’]) ? $data[‘userID’] : null;
 $this->storedPrimaryKey = array(‘userID’ => $this->userID);
 
 return $this;
 }
 
 public function exchangeEntity(User $entity)
 {
 $this->name = $entity->name;
 $this->userID = $entity->userID;
 $this->storedPrimaryKey = $entity->storedPrimaryKey;
 
 return $this;
 }

Example: User Entity public function exchangeArray(array $data)
 {
 $this->name = isset($data[‘name’]) ? $data[‘name’] : null;
 $this->userID = isset($data[‘userID’]) ? $data[‘userID’] : null;
 $this->storedPrimaryKey = array(‘userID’ => $this->userID);
 
 return $this;
 }
 
 public function exchangeEntity(User $entity)
 {
 $this->name = $entity->name;
 $this->userID = $entity->userID;
 $this->storedPrimaryKey = $entity->storedPrimaryKey;
 
 return $this;
 } Hydrator

Table Class abstract class Table
 {
 protected $serviceLocator;
 protected $tableGateway;
 
 public function __construct(TableGateway $tableGateway)
 {
 $this->tableGateway = $tableGateway;
 }
 
 final public function getServiceLocator() {…}
 
 final public function getTableGateway() {…}
 
 final public function setServiceLocator(…tor) {…}
 
 .
 .
 .

Table Class public function delete($where)
 {
 $this->tableGateway->delete($where);
 
 return $this;
 }
 
 final public function fetchAll($where = null)
 {
 return $this->tableGateway->select($where);
 }
 }

Example: User Table class UserTable extends Table
 {
 public function save(User $entity)
 {
 $data = array(‘userID’ => $entity->getUserID(), ‘name’ => $entity->getName());
 
 if ($entity->getStoredPrimaryKey())
 {
 $this->tableGateway->update($data, $entity->getStoredPrimaryKey());
 }
 else
 {
 $this->tableGateway->insert($data);
 $data[‘userID’] = $this->tableGateway->getLastInsertValue();
 }
 
 $entity->setStoredPrimaryKey(array(‘userID’ => $data[‘userID’]));
 }

Example: User Table class UserTable extends Table
 {
 public function save(User $entity)
 {
 $data = array(‘userID’ => $entity->getUserID(), ‘name’ => $entity->getName());
 
 if ($entity->getStoredPrimaryKey())
 {
 $this->tableGateway->update($data, $entity->getStoredPrimaryKey());
 }
 else
 {
 $this->tableGateway->insert($data);
 $data[‘userID’] = $this->tableGateway->getLastInsertValue();
 }
 
 $entity->setStoredPrimaryKey(array(‘userID’ => $data[‘userID’]));
 } Hydrator

TableGateway Class abstract class TableGateway extends AbstractTableGateway
 {
 protected $entityPrototype;
 protected $serviceLocator;
 
 public function __construct(Adapter $adapter, $table, $entityPrototype)
 {
 $this->adapter = $adapter;
 $this->table = $table;
 $this->entityPrototype = $entityPrototype;
 $this->resultSetPrototype = new ResultSet;
 $this->resultSetPrototype->setArrayObjectPrototype($entityPrototype);
 $this->sql = new Sql($adapter, $table);
 }
 
 .
 .
 .

TableGateway Class final public function getServiceLocator() {…}
 
 final public function setServiceLocator(…) {…}
 
 public function create()
 {
 return clone $this->entityPrototype;
 }
 }

AbstractTableGateway Class Have to implement basic operations: • select() • insert() • update() • delete()

Example: User - Group Relation class User extends Entity
 {
 .
 .
 .
 
 protected $group;
 
 public function getGroup()
 {
 if (!is_object($this->group) && !empty($this->group))
 {
 $this->group = $this->serviceLocator->get(‘ModelGroupTable’)
 ->fetchAll(array(‘groupID’ => $this->group))->current();
 }
 
 return $this->group;
 }

Example: User - Group Relation public function getGroupID()
 {
 return is_object($this->group) ? $this->group->getGroupID() : $this->group;
 } public function setGroup($group) {…}
 }

Summary • Modules - maintainability, reusability • Network of simpler components - complex system • Entity-based - focus on information • Layered Data Model - thinnest database engine related layer, more portability

http://www.attilajenei.com www@attilajenei.com Attila Jenei 2014.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Layered Data: a Modular Formal Definition without Formalisms

Layered Data: a Modular Formal De nition without Formalisms Alban Linard 12, Benoît Barbot 3, Didier Buchs 1, Maximilien Colange 12, Clément Démoulins 5 ...
Read more

Six layers Architecture Model for Object Oriented Database

Six layers Architecture Model for Object Oriented Database . ... the complex structure data is managed by object oriented database. ... modularity ...
Read more

Cybertelecom :: Layered Model

Data Link (ATM, Ethernet, FDDI, ... "One way of visualizing the layer modularity is to see the layer stack as an ... A Layered Model for Internet ...
Read more

Modularity - Wikipedia

Modularity is the degree to which a system ... data links, or other criteria. In ... Modular building as both an industrial production model and an object ...
Read more

A Data Centric Approach for Modular Assurance The Fourth ...

A Data Centric Approach for Modular Assurance The Fourth Layered Assurance Workshop ... A component complies with the data model in terms of
Read more

Modularity - msdn.microsoft.com

Data; Gaming; Internet of ... Not having modularity makes it difficult for the team to introduce new features and makes the system ... Layered approach to ...
Read more

A Modular and Layered Cosimulator for Networked Control ...

A Modular and Layered Cosimulator for Networked ... after developing a new model for the ... capabilities of transmitting data and guaranteeing ...
Read more

4: Modular Application Development Using Prism Library 5.0 ...

Prism's Support for Modular Application Development. ... to implement and test your own modularity ... from another data source by calling the ...
Read more

On Modular Architectures – On Software Architecture - Medium

On Modular Architectures ... deployment model, ... design time modularity and run time modularity.
Read more

Multi-layered Graph Clustering in Finding the Community Cores

Multi-layered Graph Clustering in Finding ... To model this problem, ... graph is built in accordance with the data acquisition of the problem domain.
Read more