Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From ORM to ODM & viceversa

From ORM to ODM & viceversa

Ettore Delprino

October 18, 2013
Tweet

Other Decks in Programming

Transcript

  1. Ettore Delprino (@edelprino) “In theory, theory and practice are the

    same. In practice, they are not!!!” Albert Einstein
  2. 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.
  3. Ettore Delprino (@edelprino) Entity & Document Entities (Documents) are objects

    with identity. Their identity has a conceptual meaning inside your domain.
  4. 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
  5. 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.
  6. Ettore Delprino (@edelprino) Product Class class Product { protected $id;

    protected $name; protected $price; protected $description public function getId() { return $this->id; } ...
  7. 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
  8. 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); }
  9. 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; } ...
  10. 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
  11. Ettore Delprino (@edelprino) What If I want to change? Repository

    Pattern, Service Container & Mapping File
  12. 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
  13. 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; } ...
  14. 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
  15. 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; } ...
  16. 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