ASP. NET mvc3 practice series (II): interface-oriented programming to improve system testability.

Source: Internet
Author: User

ASP. net mvc adopts the MVC Architecture, and the architecture itself makes the applicationProgramIt is easier to test, but it does not mean that you can write programs that are easy to test. We all know that unit testing plays an important role in system development.

Let's write such a program. The system obtains information about a bad man's lover and sends it to his wife.

1. Create a lover ASP. NET mvc3 Project

We need an entity class to store information about men, lovers, and wives.

 

Then we need a loverrepository to get someone's lover. Here we want to get data from the database. Here we first return fixed data

 

Create a homecontroller,CodeAs follows:

Create an index View

Create a send View

F5 run

2. refactor this applet.

We can see that if we want to test the action sent in homecontroller, we will not be able to test if loverrepository is not fully developed or fails.

The unit test fails.

How can we isolate loverepository? We all know that interface-oriented programming can improve system testability.

Open loverepository. CS and right-click it to refactor it to an interface:

To facilitate testing, we set this interface property to public.

In this case, we can implement a fakerepository to inherit the iloverrepository interface, and the test passes.

However, there are several problems:

First, we have exposed the repository attribute of homecontroller.

Second, we have a new object in the class. If we get lovers from the database this time, we must modify the Controller code next time we want to get data from a file or web service.

Third, we need to write a fake class by ourselves.

To solve these problems, we will discuss how to use IOC in ASP. NET mvc3 to solve the object dependency problem.

 

Finally, make a small advertisement: the Xi'an branch is eager to hire the following personnel. Interested parties can contact me directly. The left-side bar shows my contact information. The blog Park recruitment channel has a detailed description.Http://job.cnblogs.com/offer/13800/

Basic requirements:
More than 4 years of C # development experience, solid. Net Foundation, and skillful use of. net3.5 new features.
Proficient in ASP. NET web development.
Familiar with WCF.
Familiar with Orm, LINQ to SQL/Entity Framework or nhib.pdf.
Familiar with JavaScript and jquery.
Familiar with web standards and HTML & CSS.
Familiar with SQL Server, familiar with T-SQL, stored procedures.
Proficient in English reading and writing, can understand English requirements, can communicate smoothly with the customer in English (MSN/Skype)
Good coding habits.

Be familiar with any of the following priorities:
Knowledge of agile developers is preferred
Proficiency in English Listening and Speaking is preferred
Familiar with ASP. net mvc is preferred.
Familiar with WPF is preferred
Familiar with Silverlight first
Experience in system architecture is preferred, and experience in unit test or TDD is preferred.

Related Article

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.