thin controllers fat models - how to write better code

21
Thin Controllers - Fat models How to write better code - MVC - Adapted by Dr. Syed Hassan Amin

Upload: dr-syed-hassan-amin

Post on 13-Apr-2017

294 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Thin Controllers Fat Models - How to Write Better Code

Thin Controllers -Fat models

How to write better code- MVC -

Adapted by Dr. Syed Hassan Amin

Page 2: Thin Controllers Fat Models - How to Write Better Code

Introduction

• MVC is one of the most common design patterns

• It is used in Android, iOS and web applications. • Most developers are confident about having

good, practical knowledge of this pattern. • MVC pattern is perhaps the most abused

design pattern.

Page 3: Thin Controllers Fat Models - How to Write Better Code

Points to Ponder

• What is anti-pattern ?• What is dumb data holder ?

Page 4: Thin Controllers Fat Models - How to Write Better Code

Fat Controller Anti Pattern

• The anti pattern that identifies misuse of MVC pattern is Fat controller.

• Fat controller has overly complex logic and domain concepts.

• Domain logic should be completely separate from controllers so that it can be easily extended, and reused.

• Putting domain concepts in controller stinks, and results in strong coupling between domain logic, controllers and view.

Page 5: Thin Controllers Fat Models - How to Write Better Code

WARNING: Do not try it at home/work!

The WRONG way - "mvC"

Page 6: Thin Controllers Fat Models - How to Write Better Code

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 7: Thin Controllers Fat Models - How to Write Better Code

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 8: Thin Controllers Fat Models - How to Write Better Code

mvC - View

• View uses the database directly • Just give me database connection and I'll do

everything!• View– Give me more power and you won't need

controllers and models at all!

Page 9: Thin Controllers Fat Models - How to Write Better Code

mvC - problems?

● Copy/Paste to "reuse" the code.● Long and complex methods (actions) that

need to be copied in order to usepolymorphism.

● Very difficult to test.

Page 10: Thin Controllers Fat Models - How to Write Better Code

You will pay for mvC

Page 11: Thin Controllers Fat Models - How to Write Better Code

Makes your life easier

The RIGHT way - MVC

Page 12: Thin Controllers Fat Models - How to Write Better Code

MVC - Controller

● Just a translator for a user wishes (request)so model and view can understand andrespond 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 whathe wants - facade for business logic.

Page 13: Thin Controllers Fat Models - How to Write Better Code

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 toshow 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 14: Thin Controllers Fat Models - How to Write Better Code

MVC - View

● Gets a model or a collection of modelsselected by controller (according to userrequest) and shows it.

● It tells the controller what user has done andwhat does he expect to happen.

View:I'll show you what you want and let you do cool things withmodels.

Page 15: Thin Controllers Fat Models - How to Write Better Code

MVC - Action helper

● Easy and good way to reuse code betweencontrollers.

● Thanks to models it's using it keeps thingsDRY.

● Prefer action helper over methods put incontrollers extended by other controllers.

Action helper:I can do common things with the models according to therequest. I'm the controllers ... helper.

Page 16: Thin Controllers Fat Models - How to Write Better Code

MVC - View helper

● Helps you render HTML template that youuse on many views.

View helper:I can help you make things (php, js and html code) looknice.

Page 17: Thin Controllers Fat Models - How to Write Better Code

OOP - Model

● If you think you're using OOP because youhave classes - you are wrong!

● "Happy" object should have just oneresponsibility.

"Fat model" does not mean it has to havehundred lines of code.

● Object should be easy to test. You should beable to "mock" things it's using.

● Let object be dependent - inject into it thethings it needs.

Page 18: Thin Controllers Fat Models - How to Write Better Code

MVC- Web Applications• When developing web applications, there are many traps regarding

use of controllers :

– Controller per Page : In this case you will end up creating too many controllers. However, controllers can handle more than one page.

– Controller per Entity : This approach is too granular and may become even more challenging when we have more than one entity per view. Rails and Code Ignitor have this sort of controller per model approach.

• It is advisable to write single test class per controller and prefer using MVC pattern. PHP OOP provides great functionality to avoid tight coupling.

Page 19: Thin Controllers Fat Models - How to Write Better Code

MVC - JavaScript

● All those rules applies to JavaScript andother languages.

● JavaScript code is also great when you useMVC!

So you should/could have:MV(JS: MVC)C

Page 20: Thin Controllers Fat Models - How to Write Better Code

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 21: Thin Controllers Fat Models - How to Write Better Code

Questions