Share
Embed
Transcript
32 slides
-
Ettore Delprino (@edelprino) “In theory, theory and practice are the
same. In practice, they are not!!!” Albert Einstein -
Ettore Delprino (@edelprino) Active Record An object that wraps a
row in a database table or view, encapsulates the database access, and adds domain logic on that data. -
Ettore Delprino (@edelprino) Entity & Document Entities (Documents) are objects
with identity. Their identity has a conceptual meaning inside your domain. -
Ettore Delprino (@edelprino) Data Mapper The Data Mapper is a
layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other -
Ettore Delprino (@edelprino) Repository Pattern A Repository mediates between the
domain and data mapping layers, acting like an in-memory domain object collection. Objects can be added to and removed from the Repository. -
Ettore Delprino (@edelprino) Product Class class Product { protected $id;
protected $name; protected $price; protected $description public function getId() { return $this->id; } ... -
Ettore Delprino (@edelprino) Product Mapping File ...Bundle\Model\Product\Product: type: entity table:
products id: id: type: integer generator: strategy: AUTO fields: name: type: string price: type: float description: type: string -
Ettore Delprino (@edelprino) Repository Interface interface ProductRepositoryInterface { public function
create(); public function save(Product $product); public function update(Product $product); public function delete(Product $product); public function find($id); } -
Ettore Delprino (@edelprino) ORM Repository class ORMProductRepository implements ProductRepositoryInterface {
protected $doctrineORMRepository; protected $entityManager; public function __construct($doctrineORMRepository, $entityManager) { $this->doctrineORMRepository = $doctrineORMRepository; $this->entityManager = $entityManager; } public function save(Product $product) { $this->entityManager->persist($product); $this->entityManager->flush($product); return $this; } ... -
Ettore Delprino (@edelprino) Service Container product.doctrine_orm_repository: ... product.repository.orm: class: ...Bundle\Model\Product\ORMProductRepository
arguments: [@product.doctrine_orm_repository, @entity_manager] product.repository: alias: product.repository.orm -
Ettore Delprino (@edelprino) What If I want to change? Repository
Pattern, Service Container & Mapping File -
Ettore Delprino (@edelprino) Product Mapping File ...Bundle\Model\Product\Product: type: document collection:
products fields: id: unique: true id: true strategy: increment name: type: string price: type: float description: type: string -
Ettore Delprino (@edelprino) ODM Repository class ODMProductRepository implements ProductRepositoryInterface {
protected $doctrineODMRepository; protected $documentManager; public function __construct($doctrineODMRepository, $documentManager) { $this->doctrineODMRepository = $doctrineODMRepository; $this->documentManager = $documentManager; } public function save(Product $product) { $this->documentManager->persist($product); $this->documentManager->flush($product); return $this; } ... -
Ettore Delprino (@edelprino) Service Container product.doctrine_odm_repository: ... product.repository.orm: class: ...Bundle\Model\Product\ORMProductRepository
arguments: [@product.doctrine_orm_repository, @entity_manager] product.repository.odm: class: ...Bundle\Model\Product\ODMProductRepository arguments: [@product.doctrine_odm_repository, @document_manager] product.repository: alias: product.repository.odm -
Ettore Delprino (@edelprino) InMemory Repository class InMemoryProductRepository implements ProductRepositoryInterface {
protected $products; public function __construct() { $this->products = [ $this->getNewProduct(‘Prodotto0’, 10), $this->getNewProduct(‘Prodotto1’, 20) ]; } public function save(Product $product) { $this->setId($product); $this->products[] = $product; } ... -
Ettore Delprino (@edelprino) Service Container ... product.repository.orm: class: ...Bundle\Model\Product\ORMProductRepository arguments:
[@product.doctrine_orm_repository, @entity_manager] product.repository.odm: class: ...Bundle\Model\Product\ODMProductRepository arguments: [@product.doctrine_odm_repository, @document_manager] product.repository.in_memory: class: ...Bundle\Model\Product\InMemoryProductRepository product.repository: alias: product.repository.in_memory