Model View Controllers (MVC)

Preview:

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

Citation preview

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

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.

MVC and the Web

View

Controller

Model

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!

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!

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)

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

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; ?>

Common Tools

• Active Record and Object Relational Mapping (ORM)

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

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

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

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.

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.

Questions?

Recommended