Implementing the MVC pattern in asp.net (v)

Source: Internet
Author: User
asp.net implementation of Model-view-controller mode in asp.net (v)



Test

Separating the model parts from the ASP.NET environment can make the model part easier to test. When you test in a asp.net environment, you have to test many things at once, such as whether the HTML code is correct, and the work of reading HTML code is very boring. Separating the model parts allows you to do a separate unit test of the model part. The following is an example of NUnit (http://nunit.org) unit testing of the Model section.

Using System;



Using Nunit.framework;

Using System.Collections;

Using System.Data;

Using System.Data.SqlClient;



[Testfixture]

public class Gatewayfixture

{

[Test]

public void Tracks1234query ()

{



DataSet ds = databasegateway.gettracks ("1234");

Assertion.assertequals (DS). tables["Track"]. Rows.Count);

}



[Test]

public void Tracks2345query ()

{

DataSet ds = Databasegateway.gettracks ("2345");

Assertion.assertequals (3, DS. tables["Track"]. Rows.Count);

}



[Test]

public void Recordings ()

{

DataSet ds = Databasegateway.getrecordings ();

Assertion.assertequals (4, DS. Tables["Recording"]. Rows.Count);



DataTable recording = ds. Tables["Recording"];

Assertion.assertequals (4, recording. Rows.Count);



DataRow FirstRow = recording. Rows[0];

string title = (string) firstrow["title"];

Assertion.assertequals ("Up", title. Trim ());

}

}



Conclusion:

Implementing the MVC pattern in asp.net has the following advantages and disadvantages:

Advantage:

can reduce reliance. Programmers can implement all of the code in a asp.net page. A single page implementation, for some small and long life cycle of the program is applicable. But if you want to share code between the pages that are growing, separating the different parts of your code is very effective.

Can reduce the duplication of code. The Getrecordings and Gettracks methods in the Databasegateway class can be used directly by other pages, reducing the need to copy the method's code to different pages.

Be able to separate the responsibilities of different people. The technique used to modify the appearance of the page is different from the code that modifies the data access, and separating the model from the view enables the experts responsible for the different tasks to work together.

Make performance optimization possible by dividing the different responsibilities of the system into different classes. In the previous example, the data was read from the database each time the page was requested. Therefore, the data can be cached in some cases, thereby improving the performance of the entire program. This is not possible without separating the code.

Testability separates the model from the view to make it possible for unit tests to be performed outside the asp.net environment.

Disadvantages:

Increases the number and complexity of the code. This example adds new files and code based on the early implementation of a single page, adding to the overhead of maintenance. Once the system is modified, the code for all three roles is modified. In some cases, modifications in one file are more convenient than those in some files. So when considering whether to use the MVC pattern. This extra overhead must be counted, and for some small programs, this overhead is not worth it.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.