Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Enterprise PHP: mappers, models and services


Published on

One of the greatest failures PHP has is not that it is a bad language, but that it isn't marketed as a good language. Since it's so easy to do it bad, a lot of people do just that. However, there is hope. Businesses ARE using PHP for Enterprise! You just need to apply a solid foundation to the language to get to that Enterprise level.

With our mind on the Enterprise sphere, it's time to take PHP programming to a more advanced level. To get the most out of this talk, you should already be familiar with object oriented programming concepts in PHP. To begin with, I'll talk about the need for a service layer, data mappers, and business object models. Then I'll demonstrate examples of how to do it right. As an added bonus, I'm not going to just tell you why it's best practice and a "good idea" - I'm going to SHOW you. This might be the first time in your life that you realize that changes in architecture, supporting infrastructure, and business requirements don't have to be as painful as you once thought!

  • Be the first to comment

Enterprise PHP: mappers, models and services

  1. 1. Services, Mappers,Models: EnterpriseThinking in PHPAaron Saray - MKEPUG
  2. 2. Why trust this guy? ● 21 years: programmer ● 12 years: internet programmer ● PHP Design Patterns - WROX ● Manager of Web Team @ LPi ● So handsome
  3. 3. Disclaimer!!I dont have a BS CS. But that doesnt matter.Best practices exist in programming.Not everyone programs the same way.There is more than one way to solve a problem.Understand the core concepts, dont get hungup on the minutiae.For every solution I have, you can do it adifferent way too. Yours may work too!
  4. 4. What are we talkingabout today?● Services, Mappers, Models ○ Go by a few different names ○ Mix and max● Future proofing our solutions ○ separation of concerns ○ enterprise thinking● Making life easier by investments up front
  5. 5. What are Models?● Represent a true entity in your business ○ user, visitor ○ box, ps3, blu-ray movie● They know about themselves in their current state ○ they dont know if theyre incomplete ○ they dont know how theyre populated● Do logic from the business ○ logical transformation that makes sense in your realm
  6. 6. What are Models? (cont)Examples:● User: ○ first name, last name, age ■ calculates fullname ■ calculates user requires adult consent● PS3 ○ serial number, model number ■ calculates date manufactured from serial #
  7. 7. What are Models? (cont 2)Out of the realm of model:● Have I been saved to the database?● Have I been created from a form input or a MySQL result set?● How can I get more information about myself?
  8. 8. What are Mappers?● Mappers map data to a model● Mappers know about models. Models do not know about mappers.● Mappers are dependent on the data source ○ Swap data sources? Introduce a new mapper!
  9. 9. What are Mappers? (cont)● Mapper interprets a request and creates a corresponding call to MySQL ○ request for a package with ID #7, create MySQL call for that, get results, apply to the model I received.● Mapper applies data results to a model in the proper way● Mappers are very foreign to most ○ Most Frameworks provide generic ones with "magic" ○ Most custom code lacks mappers
  10. 10. What are Services?● Services are the way that the rest of the code requests objects● Services know about a Model, and its corresponding Mapper. Models and Mappers dont know about services.● Services interpret requests, create new models, and invoke mappers to populate models.
  11. 11. What are Services? (cont)● Services understand logical identifiers of models ○ fetchById()● Services can create containers or single result sets ○ fetchPaginator(), fetchByEmail()● Services retrieve and persist data ○ fetching / saving / updating
  12. 12. What are Services? (cont2)● Services rarely change once created ○ Mappers change with new data sets ○ Models change with new logic ○ Retrieval methods are relatively stoic ○ Persistence methods CAN change more often
  13. 13. Show me an example! Show User Details for User # 5 UserService::fetchById(5) new User(); UserMapper MySQL query ID = 5Yay! Apply fetchAssoc to model
  14. 14. Example Code:● Its easy for me to show you a perfect situation ○ But unless youre making something brand new, its not really feasible● Lets look at some novice PHP code ○ and refactor to Enterprise Thinking code!
  15. 15. What were accomplishing?● Will use MySQL to retrieve user information● Want to show the full name, email address, of user 5 on the screen● Dont do this!
  16. 16. Controller Code
  17. 17. Class Code - Part 1
  18. 18. Class Code - Part 2
  19. 19. So Whats WrongWith This?● From my introduction, you can spot some things● What are they?
  20. 20. Bad Things: Seems like the User model is in charge of retrieving data based off the ID
  21. 21. More Bad Things Im not sure if this data is coming from the database - where the rest is. Its a one-off Yup, the user has to populate itself, plus it has to understand that this is a key.
  22. 22. More Bad Things: The model is now forever tied to the data source, and must be modified if it changes. Here logic is being done The model is mapping itself to the data set results. in the model. Thats fine. If a data source is changed, even the creation of However, its using map- "full name" is lost. able data. :(
  23. 23. Save me!● Model should be only concerned with itself ○ full name is logic● Mapper should connect and retrieve MySQL data and apply it to the model● Service should be initialized in the controller and retrieve the proper user of ID 5
  24. 24. A Simpler User Model No population. Only stores data, and calculations (see below). Creating a full name is actually a logical calculation. For example, American name, this is correct. Latino heritage full name may contain multiple last names.
  25. 25. Mapper for MySQL Only for MySQL - this is important!! Mapper creates connection. Mapper understands ID, but has to receive a new user instance. Cant create one itself. Retrieve data, and apply it to the object.
  26. 26. User Service Initialize a User for the mapper. Get new mapper. Send to mapper the user and the data access pointer (user id). Return the properly populated model to the requestor.
  27. 27. Updated Controller Code Instead of creating a new User object, create a new service object. Then, ask the service for the user of ID #5. Dont forget! The full name is not really a property - so now its a method call in our code.
  28. 28. What was gained?● Model now only deals with business information● Service can easily ask for and interpret requests for particular models● Bound our mapper to MySQL ○ made it easier to swap out data sources
  29. 29. Ruh Roh!● Requirements change!● Choose your poison: ○ your boss said to new partner "it wont be a big deal, of COURSE we can use your feed instead of our database" ○ your client says "omg omg omg we now need to use this feed because its the newest thing do it now!"
  30. 30. Requirement Change:Instead of using MySQL, now all user datamust be retrieved from a XML feed request.Using a GET request, call the feed URL with aparameter of the user ID.You can expect an XML document with onenode with the attribute as the ID, and nodes forthe user information.
  31. 31. What We Used toHave to Do● Back in the day ○ Open up the User class and modify code ○ Possibly delete old code and lose it ○ Modify the model even though nothing has changed ○ Cry
  32. 32. What we will do now● Create a new mapper class named after the Remote XML feed ○ note: we dont have to get rid of any old code ■ what if boss changes his mind back?● Change one line in Service class● To summarize: ○ One line change of existing code ○ Rest is brand new code ■ SUCH A SMALL IMPACT
  33. 33. Fine: I did change thecontroller The only change is including the new file instead. A good autoloader would have made this a non-issue. But, I had to show you this. Ignore it as a required change
  34. 34. One line change in Service See? Simple little change. If I want to go back to the old code, I can change this mapper back.
  35. 35. New Mapper Class Same method call, just different data retrieval and preparation.
  36. 36. Final Thoughts● Probably should create a mapper interface - so that all mappers now have a mapFromId() method● It is easy to swap back and forth with different data sources● Business req changes are now easier ○ least impact on each individual part
  37. 37. The End Aaron Saray Milwaukee PHP Web DeveloperQuestions? @aaronsaray