In the real world, a blog application usually manages several entities and value objects. To keep things clear and understandable, however, in this case my blog application's domain layer will be composed only of plain blog entries. Even though this schema is really primitive, it's necessary first to create a class that models generic entities, and then one that specifically defines the data structure and constraints of the pertinent entries. With that said, here's the source code of the former. Check it out: (Blog/Entity/EntityAbstract.php) <?php namespace BlogEntity; abstract class EntityAbstract
(Blog/Entity/EntityException.php) <?php namespace BlogEntity; class EntityException extends Exception {} If you found it easy to grasp the underlying logic of the autoloader, you should feel the same way about the "EntityAbstract" class above, since I've used it previously in other Dev Shed tutorials as well. Nevertheless, if you're not familiar with the operations that this class performs, don't worry; they're very simple to follow. This class uses the "__set()" and "__get()" PHP magic methods to assign and retrieve, via the corresponding mutators/getters (when possible), the values of the fields of an entity. The $_allowedFields property controls which fields are allowable for a particular entity, which makes it easy to define specific constraints for certain types of domain objects. With this abstract parent laid down comfortably on top of the hierarchy, it's really simple to create a refined implementation of it to model blog post entries. Given that, in the next section I'm going to spawn a subclass from the parent, which will define the metadata and constraints that will be applied to the blog entries. To see how this subclass will look, keep reading. Going one step further: modeling blog post entities As I just explained, creating a class that specifically models blog post entries is a straightforward process. In fact, it's reduced to subclassing the previous abstract parent and implementing (optionally) the corresponding mutators/getters for the entries. This is all that it takes. To understand this modeling process more clearly, pay attention to the definition of the following child class, which not surprisingly has been named "BlogPost." Here it is: (Blog/Entity/BlogPost.php) <?php namespace BlogEntity; class BlogPost extends EntityAbstract Apart from seeing that PHP needs to support type hinting for primitive types like integers and strings, understanding the underlying logic of the "BlogPost" class is a breeze. Simply put, the class acts like a container that permits you to assign, via the corresponding mutators, the "id," "title," "content" and "author" to a given blog entry. Effectively, the methods impose some generic restrictions to these properties, which can be modified to suit more specific needs. So, feel free to play around with them. In addition, you should notice that the setter for the "author" treats this property as a plain string. I've done this deliberately to keep the code sample short and uncluttered. However, in a real situation, the author should be a separate entity with its own attributes and constraints, which should be modeled by a separate subclass and handled by a specific data mapper. Make sure to stick to this approach, especially if you plan to use a domain layer in the development of your next killer PHP application. So far, so good. At this point, the domain layer of this sample blog program is finally complete. But hold on a second! Wasn't this supposed to be a series that explores the use of service locators? Well, it is. It's just that the classes developed so far need no dependencies, as they only model entities. Don't feel concerned, though; in the next tutorial I'll be building the data access and mapping layers of the blog, which will need to set up some object graphs to work correctly. Remember that in this first approach, those object graphs will use only injected collaborators. But, in upcoming parts I'll be demonstrating how to get similar results using static and dynamic service locators. Final thoughts In this third installment of the series, I went through the development of the domain layer of a sample blog application, which in this particular case (for the sake of simplicity) will be able to fetch, save, and delete blog post entries from the underlying storage layer. In its current state, the blog program looks pretty skeletal. It's only able to model the aforementioned blog entries. But this is about to change. As I said before, in the next tutorial I'll be defining its data access and mapping layers. The latter will make use of dependency injection to construct its object graphs. But, keep in mind that I plan to deploy the application using static and dynamic service locators as well -- so be ready to be faced with a huge amount of code samples. Don't miss the upcoming part!
blog comments powered by Disqus |
|
|
|
|
|
|
|