During the Laravel Austin meetup this month, I gave a talk over the repository and decorator pattern, along with a demonstration on how they are useful. First off the repository and decorator pattern is not an official pattern, just the combination of using repositories with the decorator pattern, but “Repository and Decorator Pattern” sounds cooler as a presentation title than “Using repositories with decorators”.
So what exactly are repositories? Well, repositories allow us to abstract away the database layer. With repositories, we can remove business logic/database logic from your controllers, and prevent our code from becoming coupled to a single database implementation.
Lets create a few repositories, implementing the following interface: Note: The following examples use Laravel specific code, but the concepts can be used anywhere
With this interface, you could create repositories that use Eloquent, Mongo, Redis, Text files… I think you get the point.
Now instead of referencing Eloquent, or the repository directly, lets inject the interface instead. This way, it doesn’t matter what implementation we use, as long as it is an instance of UserRepositoryInterface.
So now that we have a basic idea of what repositories are, what about decorators, what are they needed for? Let me pose a hypothetical.
Your application stores users on a MySQL database, and things are running smoothly until you start to notice your application running slow. You notice the slow down is due to constantly looking up users in the database, so what do you do? Add more database servers? Shard your MySQL instance? All of these would work, but they only mask the problem, and expanding horizontally like that begins to add up in server bills. So lets add some caching instead, so that we don’t have to hit our database servers as often.
Your first reaction might be to add caching to the UserController around the repository calls, but handling caching really isn’t the responsibility of a controller. So, lets edit the EloquentUserRepository directly, and add caching to that. Well, even that has its issues. Every time you touch a class to add functionality, you run the risk of introducing new issues, so how should we do this? That’s where decorators come in!
Decorators allow you to wrap classes to extend functionality, without editing the original class. If structured correctly, you can decorate a class over and over again to add new functionality. How do you accomplish this? Well, thats where the UserRepositoryInterface comes in handy, it can be used as a contract to ensure that the decorator implements all the required functions. Lets create a decorator to add caching to our application.
As you can see, we inject an instance of UserRepositoryInterface into the CachingUserRepository class that handles the caching functionality, while using the injected UserRepositoryInterface to load users if the cache has expired. This way we can maintain our seperation of concerns, and keep our code nice and clean. How would this be constructed? Well, when we bind our concrete implementation to our IoC container, it might look something like this:
In closing, this pattern allows you to write clean and highly extendable code, that follows SOLID principles. But, before you jump into using this for every single project you start, remember this is better suited for large applications. Obviously there is nothing wrong with “over engineering” a project for the sake of learning a new concept, but I would slow down a personal weekend project worrying about clean code. Refactoring exists for a reason, and you can always go back later and clean up that weekend project if it grows into something more.
Would you like to learn more about using decorators and repositories? You should check out this video from LaraCasts. A few other videos from LaraCasts about these topics that I highly recommend are: