ASP. NET mvc3 practice series (III): Using dependency injection (IOC) in mvc3)

Source: Internet
Author: User

Practice series in ASP. NET mvc3 (II): interface-oriented programming to improve system testability. We mentioned earlierProgramAlthough unit testing is possible.

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 modifyCode.

In this section, we introduce a dependency injection tool ninject.

Click to install

Modify homecontroller to the following code:

In this way, we do not need to expose the attributes. Modify the corresponding unit test as shown in. Test passed successfully

However, an error occurred while running the program, because MVC encountered an error when instantiating the controller. Because there is no parameter constructor parameter, we must instantiate the constructor with the parameter.

 

We need to instantiate the Controller ourselves, so we need to create a controllerfactory and let the application use our own controllerfactory.

Then specify the controllerfactory in global. asax. CS.

Press F5 to run the program again.

So far, this application is easy to test and oriented to interface programming. Easy to expand.

 

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:
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.