Reusability of the Control

Asked

Viewed 239 times

3

I came across the following phrase using MVC:

"(...) the View and Controller Objects are specifically Written to each type of interface (and technology), and are generally not reusable between interfaces."


"(...) View and Controller objects are written specifically for each type of interface (and technology), and in most cases are not reusable between different interfaces."

The Control is reusable or depends on the language?

Source

3 answers

3


The Control is reusable or depends on the language?

It depends on the code. I’ll explain to you talking about the term Reuse of code, also called software reuse, which is the use of existing software, or software knowledge, for building a new software.


Reuse of software

The term software reuse means you can reuse parts of the system you’ve already developed. In this context fits, specifications, modules architecture and source code. Although reuse is related to increased levels of quality and productivity, reusing a code is not simple. This is because, you need to create "snippets" that can be reused. For example, a website template with the menus "Home, Who we are, Contact".


Reusability in the MVC

The central idea behind MVC is code reuse and concept separation. It seeks to organize the project in a way that facilitates reusability and maintenance, and good frameworks that use the MVC standard will further improve this capability.

In the layer of Model, are the classes that communicate with the Database, representing this information. The layer View then it will display the new Model data to the user. The Controller is responsible for managing events and triggering model classes to make changes to information.

When using the pattern, changes are contained in its layers, or affect as few other layers as possible. The MVC flow varies according to the implementation of each framework, however it usually follows the schema:

  • The user interacts with the interface
  • The controller manages the interface events, invoking an appropriate action
  • The model is notified of the action by changing the state of the model
  • The vision is notified of the change and is updated


References:

1

To Controller is usually dependent on the language/technology used by View and Model, so if you change the way the two communicate with the Controller, will hinder a reuse of it. I will try to make it clearer with a simple example:

Let’s say we have a web application similar to Twitter or Facebook, where we list posts from one or more people related to a user. And according to that it goes down the scroll bar of the page, more posts are loaded dynamically.

We will name here our MVC components:

View

As we are in developing a web application, we can say that our View, i.e., the components with which the user can interact, is our markup (HTML).


Controller

Again, because we are working with a web application, the component that will intermediate between our View and our Model will be a browser-supported scripting language, most likely Javascript.


Model

Our Model is a service written in any server language, with which we communicate via HTTP. This service is responsible for the business logic of the application. In our case it will return us the posts of people related to the user who is using our application.


Now that we have our application designed and we know the responsibilities of the technologies, we see clearly what will happen when the user uses the scroll bar:

  • Our Controller will be activated by the event of scroll browser.
  • Our Controller will then get the information needed to make an HTTP request for our service. (Probably via ajax, which is a feature offered by most browsers for executing HTTP requests without reloading the page.)
  • Our Model will receive the request, apply the business logic and return to our Controller.
  • Our Controller will update our View with the information of Model.

Now imagine that we have to make an application with the same behavior, using the same service (Model) only now for Desktop, or Mobile. Which components we would have to change in our current application?

Most Desktops or Mobiles offer features to use the HTTP protocol, meaning that we can communicate with our model of any platform, ie it remains unchanged. But not all offer a Javascript interpreter or HTML reader. So we’ll have to adapt the View and Controller for new platforms/technologies.

This is a type of application that it is common not to reuse Controller because the platform with which the user will interact changes.

I hope I’ve helped.

0

It is difficult to reuse Views and Controllers. Usually they are coupled with technology. I will give examples with Microsoft technologies that I use. It’s not a very technical answer, but I hope it helps.

Reuse of the Model

I can create a model in C# or VB.NET that I can use in an ASP.NET MVC, ASP.NET Webforms, WPF and Windows Forms solution. Just create this model in a Class Library project. I could use a model made in Delphi for example with DllImport, just as another technology can use this model if I compile for interoperability and this technology is able to consume Dlls.

Controller

Now my ASP.NET MVC Controller will inherit from System.Web.Mvc.Controller. My WPF controller should be something totally different that knows nothing about `System.web. I can’t take advantage of the controller.

The Controller intermediate between the View and Model. To do this it must use different artifacts to recover data from View: The ASP.NET controller must be able to manage HTTP, POSTS, GETS protocols etc to read data View.

Frameworks

For example if I create a Model in Codeigniter(PHP), a model that extends CI_Model, i won’t be able to use this model in another PHP application without Codeigniter installed. I generated a framework coupling here. What I mean is, code reuse is not that simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.