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


Published on

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.

Published in: Technology
  • Seriously, I can't believe how many people you offended in one single slideshow... Thought you were smart there? You just have no clue, do you?
    Are you sure you want to  Yes  No
    Your message goes here
  • I don't understand action helper and how to implement that with MVC. Could you please explain more or raise some examples ?
    Are you sure you want to  Yes  No
    Your message goes here
  • @john484437 He uses 2 images with a female on them. "How do you think a female developer might possibly feel". Is it his/her responsibility to think how other people might possibly feel? "When you exploit trashy, insulting, degrading images of women (unnecessarily) to make some point." Soo... the images of the women are degrading because the women on it look trashy/insulting, are you saying that a women who shows skin is trashy/insulting? "This offends me because you clearly think your audience is 100% male." How can you know that, is that maybe what you think? All the female developers I know and have worked with, don't get put off by these kind of presentations in fact they would relate to it much more. Also this industry being a boys-only club is bullshit, no body thinks that. The problem is that there are just more men who want to go in tech, just as there are more women wanting to go into health care. Why aren't you promoting there should be more men in health-care? As you said, you don't speak for women, so don't. Any woman who wants to be a developer can be. If you, male or female, really want to study something and you get put off because someone, somewhere on the internet uses a picture in a presentation. Then maybe, just maybe this wasn't really what you wanted to do.
    Are you sure you want to  Yes  No
    Your message goes here
  • Wow, what an insulting and degrading presentation! I can't believe you put your name on this. You do realize that the software development industry is widely considered (at least in the U.S.) to be a boys-only club, don't you? When you exploit trashy, insulting, degrading images of women (unnecessarily) to make some point, you contribute to that climate of hostility. I suspect that you didn't intend this effect, but come ON, man?! How do you think a female developer might possibly feel sitting through a presentation of these slides? And before you summarize my thoughts as some dogmatic adherence to political correctness, let me just put that to rest now. This offends me because you clearly think your audience is 100% male, and because you don't stop to consider the current state of the industry, and how your exploitative slideshow works against all of us. I don't pretend to speak for female developers here. As a developer who is concerned about the equity and reputation of his profession, this sexist garbage is part of the problem, not part of the solution.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

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

  1. 1. Thin controllers - fat models How to write better code - MVC - Damian Sromek 2012-03
  2. 2. The WRONG way - "mvC" WARNING: Do not try it at home/work!
  3. 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. 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. 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. 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. 7. You will pay for mvC
  8. 8. The RIGHT way - MVC Makes your life easier
  9. 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. 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. 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. 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. 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. 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. 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. 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. 17. Questions?I will help you google the answer if you askdifficult question ;DThank You.Damian
  18. 18. BibliographyImages found on the Internet and are not myproperty.- (Zend Framework Book: Surviving The Deep End)