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 ,-----------------------------------------------------------------------------------