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

DATA ACCESS USING REPOSITORY PATTERN

DATA ACCESS USING REPOSITORY PATTERN

This presentation is about utilizing the repository pattern to create more maintainable and testable Laravel 5 web applications.

Ifeora Okechukwu

August 18, 2018
Tweet

More Decks by Ifeora Okechukwu

Other Decks in Programming

Transcript

  1. DATA ACCESS USING THE REPOSITORY PATTERN Abstracting Data Queries for

    PHP (Laravel) Web Applications By Patrick Ifeora Okechukwu #JS #PHP #Frontend #BackEnd
  2. WHO AM I ? • Twitter – https://www.twitter.com/isocroft • Facebook

    – https://www.facebook.com/ifeora.okechukwu • GitHub – https://www.github.com/isocroft • LinkedIn – https://www.linkedin.com/in/ifeora-okechukwu-a3b30b69 Music Enthusiast, Marvel Fan-boy, Unrepentant Internet Addict, Open- Source Junkie and Developer Advocate & Paramount Doer at #Coolcodes https://web.facebook.com/coolcodes . I Work for Oparand as Technical Lead. #BestPractices #WebApplications #ScalableArchitecture
  3. AGENDA • Brief Intro to Coding Patterns • Repository Coding

    Pattern + Benefits • Repositories in Laravel • Laravel 5 Package (bosnadev/repositories) • Types of Repositories • Querying using the concept of a “Criteria” (criteria pattern) • Setting up Repositories in Laravel 5.6 • Repositories + Fractal / Eloquent Collections • Online Resources • Closing Remarks & Q/A
  4. BRIEF INTRO TO CODING PATTERNS Coding Patterns are repeatable solutions

    to a commonly occurring problem in software design and implementation. There are 3 broad categories: 1. Creational e.g Singleton, Abstract Factory 2. Structural e.g. Repository, Façade 3. Behavioral e.g. Composite, Visitor Repository pattern is of the Structural coding pattern family. It is actually a special kind of Façade (highly specialzed) for data access and transformation. A repository (which is the outcome of implementing the pattern in code) mediates between the domain-layer and the data-mappers (relational or not).
  5. REPOSITORY CODING PATTERN + BENEFITS There are a number of

    benefits to using the repository coding pattern as an added layer in MVC structured applications like Laravel. 1. If you use ORMs (Doctrine / Eloquent), when properly abstracted out, you can swap ORMs without affecting the interface through which your business logic interacts with your data access/storage logic. 2. When implemented properly using interface/abstract class type-hints and extension APIs, repositories are very easily testable using mocks. 3. Using other coding patterns with repositories is also very easy like the Adapter and Decorator patterns.
  6. REPOSITORIES IN LARAVEL According to Wikipedia, a repository commonly refers

    to a storage location, often for safety or preservation. In Laravel, repositories act as both a factory and a collection store which filter and transform data units. This is made possible because repositories are powered by ORMs (Doctrine / Eloquent) making it possible to hide/abstract complex implementation details for database queries. In a multi-layered architecture, repositories belong to the application-service layer. Repositories should be completely isolated (i.e. loosely coupled) from the details of the business logic (which reside in the domain layer) using the SOLID pattern popularized by Robert C. Martin (Uncle Bob). Repositories should have singular tasks (i.e. they do one thing only). They should have at most one Model (in an MVC setting). When you require to use more than one model (e.g. for join queries), you should use a decorator. Repositories should only be instantiated as singletons and never in any other way.
  7. LARAVEL 5 PACKAGE(bosnadev/repositories) It might be a lot of pain

    to setup repositories from scratch (in fact it is a pain). Owing to this, it’s a nice thing to have the boiler plate code as a packaged snippet. For this purpose, a particular composer package comes to mind – bosnadev/repositories You can use other complimentary packages like – watson/rememberable
  8. TYPES OF REPOSITORIES There are 2 types of repositories :

    1. Collection-Oriented Repositories: These are repositories structured and created to operate as in-memory array/dictionary store. (e.g repositories powered by riak, memcached) 2. Persistence-Oriented Repositories: These are repositories structured and created to operate as persistent entity stores. (e.g repositories powered by ORMs – Eloquent, Doctrine)
  9. QUERYING USING THE CONCEPT OF A CRITERIA A Criterion is

    a specification of a set of conditions and/or filters that MUST be met before a repository can return data collections based on the requirements of the domain-layer at any point in time. One or more Criteria can be applied to the repository to create complex set of conditions. Criteria make it possible to reuse these conditions and/or filter in multiple repositories.
  10. REPOSITORIES + FRACTAL / ELOQUENT COLLECTIONS Eloquent Collections have a

    plethora of methods for readily transforming data from Eloquent. Same applies to fractal which uses transformers to expressively transform data from Eloquent into a desired structure.
  11. TLDR; You can use both Fractal or Collections for more

    complex transformations all within your repositories