PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 13 ■ DATABASE PATTERNS

Figure 13–8 shows the PersistenceFactory class. I’ll be using this to organize the various components
that make up the next few patterns.


Figure 13–8. Using the Abstract Factory pattern to organize related components


The Identity Object


The mapper implementation I have presented here suffers from a certain inflexibility when it comes to
locating domain objects. Finding an individual object is no problem. Finding all relevant domain objects
is just as easy. Anything in between, though, requires you to add a special method to craft the query
(EventMapper::findBySpaceId() is a case in point).
An identity object (also called a Data Transfer Object by Alur et al.) encapsulates query criteria,
thereby decoupling the system from database syntax.


The Problem


It’s hard to know ahead of time what you or other client coders are going to need to search for in a
database. The more complex a domain object, the greater the number of filters you might need in
your query. You can address this problem to some extent by adding more methods to your Mapper
classes on a case-by-case basis. This is not very flexible, of course, and can involve duplication as you

Free download pdf