A repository is an abstraction layer that usually sits between the domain and the mapping layers of a given application. It provides client code with a set of methods that can be used to manipulate collections of domain objects that match a specific criteria. Superbly described by Martin Fowler in his already classic book Patterns of Enterprise Application Architecture, a repository can be really helpful for encapsulating queries on related domain objects behind an intuitive API. Doing this produces an illusion of having all of those object collections residing in memory at the same time. As with other software design patterns, a repository can be implemented in multiple programming languages. This includes PHP -- though PHP-based repositories aren't very popular right now, except in cases where this level of abstraction is indispensable. Regardless, building a repository from scratch in PHP is an extremely productive process. It permits you to put other principles and patterns of modern application development to work together, such as data mappers, factories and entities, beyond the typical implementation of Active Record. Note that a typical implementation of Active Record couples domain objects "too dangerously" with the underlying persistence mechanism (usually a relational database). With that premise in mind, in the first part of this series I took the first step toward the construction of a simple user repository, namely the development of the domain layer. To keep the process easy to follow, the layer was comprised of two sample classes. The first one was an abstract parent, tasked with defining the structure and behavior of generic entities, while the second class was a refined implementation of the parent, responsible for modeling basic user objects. Needless to say, before I manage to create a functional user repository, there are some additional steps that must be taken. These include (among others) building the data access and mapping layers. Therefore, in this second tutorial of the series, I'm going to create the former; for the sake of simplicity, it will be made up of a simple MySQL abstraction class. Ready to see how this sample data access layer will be built in a few easy steps? Then jump in and begin reading! Reviewing the sample classes defined so far Before I start defining the data access layer mentioned in the introduction, let's quickly review the classes created in the preceding part of the series. As you'll recall, these classes comprise the domain layer of this example. Their responsibility is to create simple entities, like the user objects that will be handled later on by the repository. With that said, first, here's a basic autoloader. It lazy-loads source classes via the SPL stack. Check it out: (Autoloader.php) <?php class Autoloader /**
(ClassNotFoundException.php) <?php class ClassNotFoundException extends Exception{} As you'll surely agree with me, the logic implemented by the above "Autoloader" class is very easy to grasp, so I'm not going to waste your valuable time explaining how it works again. Instead, look at the following two classes. The abstract one, "EntityAbstract," is tasked with defining the structure and behavior of generic entities, while the concrete "User" class is charged with modeling user objects. Here's the former: (EntityAbstract.php) <?php abstract class EntityAbstract
(EntityException.php) <?php class EntityException extends Exception {} Done. With the above abstract parent encapsulating most of the functionality required to create generic entities, deriving a subclass that models user objects according to a number of predefined constraints is this easy: (User.php) <?php class User extends EntityAbstract Mission accomplished, at least for now. Having shown the pair of classes that make up the domain layer of this example (plus the autoloader, which normally should reside in some kind of bootstrap module or class), the next step is to define the corresponding data access layer. As mentioned before, this layer will be integrated by a basic MySQL abstraction class. Don't be concerned for the moment if these layers seem to be disconnected from each other. When I put them to work side by side, you'll see how nicely they'll fit in the whole schema imposed by a repository. So, if you want to see the definition of the aforementioned data access layer, read the following segment. It's only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|