thin controllers fat models - how to write better code

Post on 13-Apr-2017

294 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Thin Controllers -Fat models

How to write better code- MVC -

Adapted by Dr. Syed Hassan Amin

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.

Points to Ponder

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

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.

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 • 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!

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.

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 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.

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.

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.

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.

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.

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.

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.

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

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

top related