• Nem Talált Eredményt

ASP.NET MVC Framework

In document Programming Technologies (Pldal 40-43)

5. Architectural design patterns

5.2. ASP.NET MVC Framework

The ASP.NET MVC Framework had given an alternative for the ASP.NET Web Forms - based developments to develop MVC-based web applications. The ASP.NET MVC Framework is an easy and well-testable visualizer framework, that (just as the ASP.NET Web Forms) integrates the options of the existing ASP.NET, such as master pages and inbuilt user handling, namely membership provider - based identification. The bases of MVC are defined by the System.Web.Mvc namespace, which is a supported part of the System.Web namespace.

The MVC is a basic software design pattern that is familiar to many developers. Some web applications are using the advantages of the MVC framework for a long time, while others are still using the regular post back-based ASP.NET Web Forms system. And some others combine the advantages of both. We will later discuss when the MVC development mode is beneficial.

The MVC framework contains three main components:

• Models: The model objects are the parts of the applications that implement the logic that carries the data. The model objects often receive data from the database and store them in themselves. For example, a Product object can call down data from the database, can work with them, than it can update the Product table in the SQL server with the modified data.

In the case of lesser applications, the models are of conception things than physically implemented. For example, if the application only reads and visualizes the data, than the model layer and the class structure

belonging to it is not specifically implemented. In this case, the model layer is represented by data storing objects only.

• Views: The Views are the visualizer components of the user interface (UI). The UI is usually made of the data coming from the model layer. One such can be the editing view of the Product table, which consist of textboxes, buttons, dropdown menus, etc and show the actual state of the Product object.

• Controllers: The controllers are the components that handle the user interaction; work with the model objects and selects the appropriate view for visualization. In a MVC application, the view only shows information;

the controller handles and reacts to the user interaction. For example, the controller is handling the query string values; forward them to the model that set the appropriate database query according to the forwarded values.

Figure 3 : The MVP design pattern

The MVC pattern help to create applications that separate the different parts of the application (input logic, business logic, visualization logic) while securing a loose connection surface between the separated parts. The pattern also defines where each logic layer should be located in the application. The visualization or UI layer belongs to the views, the business logic to the controllers while the input logic belongs to the models. This separation helps handling the complexity while developing an application, as it allows us to study things in a given time from a given angle during the implementation. For example, during the development of the view layer, we don‟t need to bother with the operations done to the data in the business logic part, as the views are only used to visualize them.

In addition, the MVC pattern makes the testing of the application easier than in a Web Forms - based development model. For example, in a Web Forms - based application, the same class can be responsible for the visualization and the user interaction. It can be difficult to write automated test for a Web Forms - based application, as to test an independent page, we need to instance the class of the page, all the child controllers and the dependent classes to. As we need so much instancing to run the page, it‟s very difficult to write tests that deal with the different parts of the page independently. We can declare that it‟s much more difficult to integrate testing to a Web Forms - based environment than to a MVC-using application. In addition, in a Web Forms - based environment, we need a web server for the testing. As the MVC framework separates the components and uses interfaces between them, it is easier to write tests to these discrete components due to the mentioned isolation.

The loose connection between the three main components of an MVC application allows parallel development.

This means that one developer can work and focus on the views and the looks, a second on the controlling logic and a third one on the business logic at the same time.

5.2.1. When should we create a MVC application?

We should watchfully choose when to use the ASP.NET MVC framework to develop an application instead of the ASP.NET Web Forms, as the ASP.NET MVC does not substitute the Web Forms model; we can use both in one application if we like.

Before choosing the MVC framework instead of the Web Forms model, we should ponder their advantages.

5.2.2. The benefits of an MVC-based web application

The ASP.NET MVC framework offers the following advantages:

• It makes the development of complex applications easier by separating them into three parts: model, view, and controller.

• It does not use view states and server-side forms. This makes MVC ideal for those who want total control over the behavior of the application.

• It processes the request arriving to the web application through a main controller pattern and forwards them to the appropriate controller from here. (You can get more information about the main controller at the MSDN website, below the Front Controller section.)

• It helps Test-Driven Development (TDD).

• It works well for web application that the development team improves and supports, where the designers of the views need to have strong control over the behavior of the application.

5.2.3. The benefits of Web Forms – based applications

The advantages of Web Forms frameworks:

• It supports event-handling models and preserves the states in HTTP protocol, which is advantageous when developing a so-called “line-of-business” web application. The Web Forms - based application guarantees dozens of event-handlers, that we can reach from hundreds of server-controls.

• It uses the Page Controller patters that bestow different attributes to the individual pages. More information about the Page Controller can be found on the MSDN website.

• It uses view states and server-side forms that make the managing of state-handling information easier.

• It works well for smaller developer teams, has many components to help quick application development.

• On the whole, it‟s less complex from the application development‟s point of views, as the components (the Page class, the controllers) are tightly integrated and so require less coding than the MVC model.

5.2.4. The attributes of the ASP.NET Framework

The ASP.NET MVC Framework offers the following functions:

• The separation of the application‟s tasks (input logic, business logic views logic), testability and support of Test-Driven Development. All of the core elements of the MVC are interface-based, which allows testing with so called “mock” objects that imitate the actual object‟s behavior in the application. It enables unit-test - based testing without running the controllers through the ASP.NET procedures, so unit testing will be more flexible and faster. We can use any unit-test framework that is compatible with the .NET framework.

• • It is an expendable and extendable framework. The components of the ASP.NET MVC are developed to be easily customizable and tradable.

• The URL-mapping component allows the development of such applications that have understandable and searchable URLs. The URLs don‟t contain file name extensions. They are search- and user-friendly.

• Supports the use of existing ASP.NET pages (.aspx files), user controls (.ascx files) and master pages (.master files). We can use the existing ASP.NET powers, such as the use of nested master pages and the use of the server-side code inside the marking language of the ASP.NET with the help of the <%=%> expression.

• Support of existing ASP.NET powers. The ASP.NET MVC offers the use of the inbuilt powers, such as form authentication, Windows authentication, user handling (membership and roles), session handling, etc.

In document Programming Technologies (Pldal 40-43)