model view controllers (mvc)

16
Model View Controllers (MVC) Barack Karavani

Upload: lajos

Post on 20-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

Model View Controllers (MVC). Barack Karavani. Outline. What is MVC? Why use the MVC Pattern? MVC and the Web Benefits of MVC in Web Applications Modern MVC Frameworks. What is MVC. MVC is a pattern Patterns are commonality that we find amongst software - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Model View Controllers (MVC)

Model View Controllers (MVC)

Barack Karavani

Page 2: Model View Controllers (MVC)

Outline

• What is MVC?• Why use the MVC Pattern?• MVC and the Web• Benefits of MVC in Web Applications• Modern MVC Frameworks

Page 3: Model View Controllers (MVC)

What is MVC

• MVC is a patterno Patterns are commonality that we find amongst

software Architectural Pattern - Provide solutions to

various issues in Software Engineering Design Patterns - Provide solutions to repeated

issues in Software Development

• MVC solves common issues by separating important parts into three subsystems.

Page 4: Model View Controllers (MVC)

Why MVC

• Decouple the interaction between user interfaces and data stores.

• Provide a clear separation of concern between components.

• Promote the development of reusable components.

Page 5: Model View Controllers (MVC)

MVC and the Web

View

Controller

Model

Page 6: Model View Controllers (MVC)

Benefits of MVC in Web Applications

• Websites and Web Applications are great candidates for the MVC Pattern.o Inherently clear distinction for models,

views, and controllers.• Multiple frameworks exist in various

languages which support and encourage the MVC Pattern and other great coding principles!

Page 7: Model View Controllers (MVC)

Popular MVC Web Frameworks

• Open Source frameworks are actively being developed

• Promote the MVC Patterno Definitions of MVC may change depending on the

framework philosophy, READ THE DOCS!

Page 8: Model View Controllers (MVC)

Popular MVC Web Frameworks

• MVC Frameworks exist both on the client-side and server-side

Client Side (JS):● Backbone● AngularJS● EmberJS

Server Side:● Django (Python)● Laravel (PHP)● Rails (Ruby)

Page 9: Model View Controllers (MVC)

Benefits of MVC in Web Applications

• By promoting the use of reusable components, we prevent ourselves from writing “spaghetti” code. o Spaghetti Code - One-off code which interleaves business logic

(model/controller) with view responsibilities Hard to maintain Hard to read Does not scale well

• Provide commonly used tools to web developers

Page 10: Model View Controllers (MVC)

Example of Spaghetti Code <? if(($child_assent || $adol_assent) && $form['short_name'] == 'parental_consent' && $canEdit): ?><div class="form-helpers"><? else: ?><div class="form-helpers hide"><? endif; ?>

Page 11: Model View Controllers (MVC)

Common Tools

• Active Record and Object Relational Mapping (ORM)

• Routes• Templating• Form and Input Validation• Security and Authentication• Testing & Test Cases

Page 12: Model View Controllers (MVC)

Views in Laravel, Django, Rails

• Typically views for web applications consist of the HTML/CSS/JS side of thingso Do not interact directly with the model

• All three frameworks implement their version of a templating system.o Contain either simple logic or none at allo Prevents spaghetti code (by not allowing ANY code)o Promotes developing small templates to be reused

and combined in multiple places

Page 13: Model View Controllers (MVC)

Templating{% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li>{{ poll.question }}</li> {% endfor %} </ul>{% else %} <p>No polls are available.</p>{% endif %}

<% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.content %></td> </tr><% end %>

Django Rails

Page 14: Model View Controllers (MVC)

Models in Laravel, Django, Rails

• Models are defined as objects.o Instances of models represent rows within the

database table.

• Frameworks will automatically build database tables based upon models.

• Controller can interact with the models as native language objects.

Page 15: Model View Controllers (MVC)

Controllers in Laravel, Django, Rails

• Controllers are functions that are called when a user accesses certain pages.o One controller can be called for multiple URLs.

• Processes input data from the user.• Returns data to be styled in the view for the

user to interpret.

Page 16: Model View Controllers (MVC)

Questions?