From ORM to ODM & viceversa

From ORM to ODM & viceversa

68d81416a15ab0fd47fca6b0bbf4587b?s=128

Ettore Delprino

October 18, 2013
Tweet

Transcript

  1. Ettore Delprino (@edelprino) From ORM to ODM & viceversa

  2. Ettore Delprino (@edelprino) Ettore Delprino @edelprino http://edelprino.com

  3. Ettore Delprino (@edelprino)

  4. Ettore Delprino (@edelprino)

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

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

    with identity. Their identity has a conceptual meaning inside your domain.
  8. 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
  9. 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.
  10. Ettore Delprino (@edelprino)

  11. Ettore Delprino (@edelprino) “è un mappazzone!!!” Bruno Barbieri

  12. Ettore Delprino (@edelprino) Solutions? Repository Pattern, Service Container & Mapping

    File
  13. Ettore Delprino (@edelprino) Product Class class Product { protected $id;

    protected $name; protected $price; protected $description public function getId() { return $this->id; } ...
  14. 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
  15. 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); }
  16. 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; } ...
  17. 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
  18. Ettore Delprino (@edelprino)

  19. Ettore Delprino (@edelprino) Before: After:

  20. Ettore Delprino (@edelprino) What If I want to change? Repository

    Pattern, Service Container & Mapping File
  21. 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
  22. 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; } ...
  23. 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
  24. Ettore Delprino (@edelprino)

  25. Ettore Delprino (@edelprino) “Database is a plugin!!!” Robert C. Martin

  26. Ettore Delprino (@edelprino) Is it essential? InMemoryProductRepository

  27. 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; } ...
  28. 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
  29. Ettore Delprino (@edelprino)

  30. Ettore Delprino (@edelprino) Other ideas... APIProductRepository RedisProductRepository KeyValueProductRepository

  31. Ettore Delprino (@edelprino) That’s all Questions?

  32. Ettore Delprino (@edelprino) Interesting readings: http://williamdurand.fr/2013/08/07/ddd-with-symfony2-folder-structure-and-code-first http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html http://martinfowler.com/eaaCatalog/repository.html http://stackoverflow.com/questions/17228417/symfony-2-creating-a-service-from-a-repository