thin controllers - fat models (proper code structure for mvc)
Post on 20-May-2015
39.577 Views
Preview:
DESCRIPTION
TRANSCRIPT
Thin controllers - fat models
How to write better code- MVC -
Damian Sromek
damiansromek.pl
2012-03
WARNING: Do not try it at home/work!
The WRONG way - "mvC"
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!
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?
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!
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.
You will pay for mvC
Makes your life easier
The RIGHT way - MVC
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.
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.
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.
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.
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.
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.
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
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")
Questions?
I will help you google the answer if you ask difficult question ;D Thank You.Damian
Bibliography
Images found on the Internet and are not my property. - http://survivethedeepend.com/ (Zend Framework Book: Surviving The Deep End)
top related