Thin controllers - fat models (Proper code structure for MVC)
-
Upload
damiansromek -
Category
Technology
-
view
39.575 -
download
2
description
Transcript of Thin controllers - fat models (Proper code structure for MVC)
![Page 1: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/1.jpg)
Thin controllers - fat models
How to write better code- MVC -
Damian Sromek
damiansromek.pl
2012-03
![Page 2: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/2.jpg)
WARNING: Do not try it at home/work!
The WRONG way - "mvC"
![Page 3: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/3.jpg)
mvC - Controller
● Whole business logic in a Controller. Controller: I'll do everything for you.You can keep everything in one place.That's so simple!
![Page 4: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/4.jpg)
mvC - Model
● Models used basically just to store data in database.
Model: I'm just a data so why should I do anything more than just exist?
![Page 5: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/5.jpg)
mvC - View
● View uses the database directly or via MysqliResult etc.
View:Just give me database connection and I'll do everything!Give me more power and you won't need controllers and models at all!
![Page 6: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/6.jpg)
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.
![Page 7: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/7.jpg)
You will pay for mvC
![Page 8: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/8.jpg)
Makes your life easier
The RIGHT way - MVC
![Page 9: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/9.jpg)
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.I'll just tell model what user did and the view to show what he wants - facade for business logic.
![Page 10: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/10.jpg)
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:I'm the proper guy for doing the hard work.I know how your app should work and why.
![Page 11: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/11.jpg)
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:I'll show you what you want and let you do cool things with models.
![Page 12: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/12.jpg)
MVC - Action helper
● Easy and good way to reuse code between controllers.
● Thanks to models it's 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 the request. I'm the controllers ... helper.
![Page 13: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/13.jpg)
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) look nice.
![Page 14: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/14.jpg)
OOP - Model
● If you think you're 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 it's using.
● Let object be dependent - inject into it the things it needs.
![Page 15: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/15.jpg)
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
![Page 16: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/16.jpg)
Zend Framework uses MVC - really?
● It's not a "true" MVC.● MVC was designed for desktop apps.● It has many "branches". @see MVP@see Model2 ("MVC for web apps")
![Page 17: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/17.jpg)
Questions?
I will help you google the answer if you ask difficult question ;D Thank You.Damian
![Page 18: Thin controllers - fat models (Proper code structure for MVC)](https://reader033.fdocuments.in/reader033/viewer/2022052301/555c3d5bd8b42a2c068b4b2b/html5/thumbnails/18.jpg)
Bibliography
Images found on the Internet and are not my property. - http://survivethedeepend.com/ (Zend Framework Book: Surviving The Deep End)