Unity usage reprinted

Source: Internet
Author: User

Create an empty ASP. NET MVC3 project and add references to the Unity2.0 dynamic library.

Method 1: Download Untity2.0 on MSDN. After installation, it is installed under C: \ Program Files \ Microsoft Unity Application Block 2.0 by default.

Microsoft. Practices. ServiceLocation. dll

Microsoft. Practices. Unity. Configuration. dll

Microsoft. Practices. Unity. dll

Microsoft. Practices. Unity. Interception. Configuration. dll

Microsoft. Practices. Unity. Interception. dll

Method 2: Use the Package Manager Console

Tool-Library Package Manage-Package Manager Console, execute the following command:

 

 

 

 

To use the Unity dependency injection container in the newly created ASP. NET MVC3 project, you must add a class in the project to implement the IDependencyResolver interface and call the specific Unity dependency injection container.

 

The Code is as follows:

Using System. Web. Mvc;

Using Microsoft. Practices. Unity;

Public class UnityDependencyResolver: IDependencyResolver
{
Readonly IUnityContainer _ container;
Public UnityDependencyResolver (IUnityContainer container)
{
This. _ container = container;
}
Public object GetService (Type serviceType)
{
Try
{
Return _ container. Resolve (serviceType );
}
Catch
{
Return null;
}
}
Public IEnumerable <object> GetServices (Type serviceType)
{
Try
{
Return _ container. ResolveAll (serviceType );
}
Catch
{
Return new List <object> ();
}
}
}

 

In Global. asax. cs, create a Unity dependency injection container. In the Application_Start method, use the UnityDependencyResolver class created earlier to register the Unity container as the Service Locator of ASP. net mvc.
The Code is as follows:
Protected void Application_Start ()
{
AreaRegistration. RegisterAllAreas ();

RegisterGlobalFilters (GlobalFilters. Filters );
RegisterRoutes (RouteTable. Routes );

Var container = new UnityContainer ();
Container. RegisterType <IMessages, Messages> ();
DependencyResolver. SetResolver (new UnityDependencyResolver (container ));
}
Container use at the same timeRegisterTypeThe type is registered. In this example, the IMessage type and the specific implementation of Messages are registered.
 
Add HomeController. cs with the following code:
The Code is as follows:

Public class HomeController: Controller
{

Private readonly IMessages messages;
Public HomeController (IMessages messages)
{
This. messages = messages;
}
Public ActionResult Index ()
{
ViewBag. Message = messages. Welcome ();
Return View ();
}

}
 
Added the interface IMessges and the specific implementation class Messages. The Code is as follows:
The Code is as follows:

Public interface IMessages
{
String Welcome ();
}

Public class Messages: IMessages
{
Public string Welcome ()
{
Return "welcome to use Unity2.0 in ASP. NET MVC3 ";
}
}
 
Run the application. The Unity dependency injection container parses the dependencies defined by HomeControler. The Index page shows "welcome to use Unity2.0 in ASP. NET MVC3"

Code download

 

 

Extended (exercise), exercise 3, and exercise ,----------------------------------------------------------------------------------- Source code Change to the configuration file method 1. Configure 2. Configure ,-----------------------------------------------------------------------------------

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.