Thin controllers - fat models (Proper code structure for MVC)
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Thin controllers - fat models (Proper code structure for MVC)

  • 34,386 views
Uploaded on

How to write better code when using "MVC" framework? ...

How to write better code when using "MVC" framework?

Remember to keep controllers "thin" and models "fat".

This presentation shows quite popular and bad practices when using MVC framework.
But it also tells how it should be done in order to have code easier to maintain.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
34,386
On Slideshare
33,055
From Embeds
1,331
Number of Embeds
11

Actions

Shares
Downloads
92
Comments
0
Likes
21

Embeds 1,331

http://damiansromek.pl 1,243
http://christopherconcepcion.com 59
http://abtasty.com 13
https://twitter.com 7
http://prlog.ru 2
http://translate.googleusercontent.com 2
https://twimg0-a.akamaihd.net 1
http://www.w3schools.com 1
http://dom.damiansromek.pl 1
https://si0.twimg.com 1
http://www.damiansromek.pl 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Thin controllers - fat models How to write better code - MVC - Damian Sromek damiansromek.pl 2012-03
  • 2. The WRONG way - "mvC" WARNING: Do not try it at home/work!
  • 3. mvC - Controller● Whole business logic in a Controller.Controller:Ill do everything for you.You can keep everything in one place.Thats so simple!
  • 4. mvC - Model● Models used basically just to store data in database.Model:Im just a data so why should I do anything morethan just exist?
  • 5. mvC - View● View uses the database directly or via MysqliResult etc.View:Just give me database connection and Ill do everything!Give me more power and you wont need controllers andmodels at all!
  • 6. mvC - problems?● Copy/Paste to "reuse" the code.● Long and complex methods (actions) that need to be copied in order to use polymorphism.● Very difficult to test.
  • 7. You will pay for mvC
  • 8. The RIGHT way - MVC Makes your life easier
  • 9. MVC - Controller● Just a translator for a user wishes (request) so model and view can understand and respond in proper way (response).Controller:I should be so thin you should barely notice me.Ill just tell model what user did and the view to show whathe wants - facade for business logic.
  • 10. MVC - Model● This is where whole "magic" should happen.● Model should store and manipulate data.● It should be used intensively by controllers to do what user requested and by views to show data user wants to see.● Business logic should be in models, eg. "$ageLimit > 20".Model:Im the proper guy for doing the hard work.I know how your app should work and why.
  • 11. MVC - View● Gets a model or a collection of models selected by controller (according to user request) and shows it.● It tells the controller what user has done and what does he expect to happen.View:Ill show you what you want and let you do cool things withmodels.
  • 12. MVC - Action helper● Easy and good way to reuse code between controllers.● Thanks to models its using it keeps things DRY.● Prefer action helper over methods put in controllers extended by other controllers.Action helper:I can do common things with the models according to therequest. Im the controllers ... helper.
  • 13. MVC - View helper● Helps you render HTML template that you use on many views.View helper:I can help you make things (php, js and html code) looknice.
  • 14. OOP - Model● If you think youre using OOP because you have classes - you are wrong!● "Happy" object should have just one responsibility. "Fat model" does not mean it has to have hundred lines of code.● Object should be easy to test. You should be able to "mock" things its using.● Let object be dependent - inject into it the things it needs.
  • 15. MVC - JavaScript● All those rules applies to JavaScript and other languages.● JavaScript code is also great when you use MVC!So you should/could have:MV(JS: MVC)C
  • 16. Zend Framework uses MVC - really?● Its not a "true" MVC.● MVC was designed for desktop apps.● It has many "branches".@see MVP@see Model2 ("MVC for web apps")
  • 17. Questions?I will help you google the answer if you askdifficult question ;DThank You.Damian
  • 18. BibliographyImages found on the Internet and are not myproperty.- http://survivethedeepend.com/ (Zend Framework Book: Surviving The Deep End)