Implement MVC mode in ASP.NET (5)

zhaozj2021-02-16  49

Implement Model-View-Controller mode in ASP.NET (5)

test

Separating the model portion from the ASP.NET environment enables the model portion to be tested more easily. In the ASP.NET environment, you must test a lot at the same time, such as whether the HTML code is correct, and reading the HTML code is very bored. Separate the model part so you can make a separate unit test on the model portion. The following is an example of unit testing of the model section below NUnit (http://nunit.org).

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 (10, 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"];

Ask.ASSERTEQUALS (4, Recording.Rows.count);

DataRow Firstrow = Recording.Rows [0];

String Title = (String) FIRSTROW ["Title"];

Ask.ASSERTEQUALS ("UP", Title.trim ());

}

}

in conclusion:

Implementing the MVC mode in ASP.NET has the following advantages and disadvantages:

Advantage:

Can reduce dependence. The programmer can implement all the code in an ASP.NET page. Single page implementation, for some small and long-lived programs are applicable. However, if you want to share the code between the increasing pages, it is very effective.

Ability to reduce the copy of the code. The GetRecordings and GetTracks methods in the Databasegateway class can be used directly by other pages, reducing the case where the code must be copied to different pages.

Ability to separate the responsibility of different people. Modifying the appearance of the page and the technique used by the code to modify the data accessed is different, and the model is separated from the view to the work of experts responsible for different work.

Make performance optimization may be possible to divide different classes into different classes to make performance optimization. In the previous example, data is read from the database since each request page is requested. Therefore, the data can be cached in some case, thereby increasing the performance of the entire program. If you don't separate the code, you cannot do this.

Easy testivity The model is separated from the view into unit testing in an ASP.NET environment.

Disadvantages:

转载请注明原文地址:https://www.9cbs.com/read-25074.html

New Post(0)