To demonstrate how to take advantage of the functionality offered by the user repository that you saw in the previous section, I'm going to use a simple MySQL table, which has been previously populated with data corresponding to a few trivial users. The sample table looks like this:
So far, nothing unexpected, right? As you can see, the table contains four columns that store the ID (an autonumeric, unsigned primary key) of each user, as well as their first and last names and their email addresses. Now, if I wanted to retrieve some of the stored users by specifying some basic conditions, the process would be this simple: <?php // include the Autoloader // create an instance of the Repository factory // create a user repository // fetch users by their first names // fetch users by their last names // fetch users by their email addresses As the above code fragment shows, putting the user repository into action and fetching different collections of user entities that match a specific condition is amazingly easy! In this case, the criteria used to retrieve the previous users are very simple. It's feasible, however, to create a repository capable of working with more complex conditions than simple first and last names. The possibilities are endless. Naturally, the repository can be used to insert new users into the above table, and even to remove existing users through the same API. Therefore, in the last section I'm going to build a final example that will show how to perform these additional operations. Now, jump ahead and keep reading. One step further: adding and deleting user entities As I noted in the section that you just read, the user repository makes it really easy to add new users to the persistence layer, and to delete existing ones. If you want to see how these operations can be executed in a few steps, simply pay attention to the code snippet below, which will hopefully be self-explanatory: <?php // include the Autoloader // create an instance of the Repository factory // create a user repository // insert a new user // delete an existing user There you have it. Thanks to the functionality provided by the repository's "insert()" and "delete()" methods, inserting and deleting users from the persistence layer is indeed a straightforward process that can be tackled in a snap. Needless to say, there's plenty of room to experiment here, and extend the current capability of the repository in such a way that it can perform more complex queries. Considering this scenario, I encourage you to tweak this class in many clever ways and add your own custom methods to it. In doing so, you'll acquire a better background in mastering one of the big pillars of Domain-Driven Design. Final thoughts Finally, we've come to the end of this series. From a strong hands-on standpoint, I attempted to demonstrated in a step-by-step fashion how to build a repository from scratch. Hopefully, during the development process, you learned not only how to create such an abstraction layer, but how to apply some concepts inherent to modern application design, such as building models that are loosely coupled to the persistence mechanism. If you feel that the implementation of Active Record, widely promulgated by Rails and even other well-known PHP frameworks out there, doesn't suit your needs any longer and want to create programs that scale up well in different conditions and with multiple infrastructures, then this series might help you start migrating to more flexible solutions. See you in the next PHP development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|