开始使用Unity.Mvc3
本文一步一步演示如何在MVC3项目中使用Unity.Mvc3实现依赖注入。
先创建一个基本的MVC3项目。
Figure 1: The Visual Studio 2010 New Project Dialog
Figure 2: The Visual Studio 2010 New ASP.NET MVC3 Internet Application Template
通过NuGet将Unity.Mvc3加入到项目。.右击references选择Manage NuGet packages.。在弹出对话框 输入Unity.Mvc3 搜索。. 在搜索结果中选择Unity.Mvc3 安装。
Figure 3: Manage NuGet Packages Context Menu Item
Figure 4: Manage NuGet Packages Dialog
NuGet将安装Unity 和Unity.Mvc3并添加到引用,同时在项目中添加一些文件。 Bootstrapper.cs包含初始化Unity容器和DependencyResolver的代码。
using
System.Web.Mvc;
using
Microsoft.Practices.Unity;
using
Unity.Mvc3;
namespace
MvcApplication1
{
public
static
class
Bootstrapper
{
public
static
void
Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(
new
UnityDependencyResolver(container));
}
private
static
IUnityContainer BuildUnityContainer()
{
var container =
new
UnityContainer();
// register all your components with the container here
// e.g. container.RegisterType<ITestService,TestService>();
return
container;
}
}
}
|
在Global.asax 的Application_Start 方法中添加对Bootstrapper初始化方法的调用。
protected
void
Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Bootstrapper.Initialise();
}
|
:-)
添加依赖
这里以一个简单的消息service为例,演示如何通过unity.Mvc3实现依赖注入。添加IMessageService接口和实现了此接口的MessageService。
namespace
MvcApplication1
{
public
interface
IMessageService
{
string
GetMessage();
}
public
class
MessageService : IMessageService
{
public
string
GetMessage()
{
return
"Hello from the MessageService"
;
}
}
}
|
修改HomeController,在HomeController中使用ImessageService。
using
System.Web.Mvc;
namespace
MvcApplication1.Controllers
{
public
class
HomeController : Controller
{
private
readonly
IMessageService _messageService;
public
HomeController(IMessageService messageService)
{
_messageService = messageService;
}
public
ActionResult Index()
{
ViewBag.Message = _messageService.GetMessage();
return
View();
}
public
ActionResult About()
{
return
View();
}
}
}
|
最后,告诉unity我们的服务类MessageService。在Bootstrapper类的BuildUnityContainer 方法中使用RegisterType<TFrom,TTo>注册我们的接口和实现,完成接口和实现的Map。.
private
static
IUnityContainer BuildUnityContainer()
{
var container =
new
UnityContainer();
container.RegisterType<IMessageService,MessageService>();
return
container;
}
|
运行效果如下图所示。 MVC framework 告知DependencyResolver返回一个 HomeController。Resolver通过Unity获知 HomeController 需要一个明确的IMessageService接口实现。由于在初始化时注册了IMessageService接口和实现的Map, Unity 将实例化一个MessageService对象,注入到控制器中,返回给MVC framework。由此完成了依赖注入。
Figure 5: MVC View Displaying The Message Created By The The MessageService
添加依赖的依赖
增加一点复杂度。假设MessageService本事也有依赖,创建一个IMessageGenerator接口和实现.
namespace
MvcApplication1
{
public
interface
IMessageGenerator
{
string
GetMessage();
}
public
class
MessageGenerator : IMessageGenerator
{
public
string
GetMessage()
{
return
"消息来自MessageGenerator"
;
}
}
}
|
修改MessageService:.
public
class
MessageService : IMessageService
{
private
readonly
IMessageGenerator _messageGenerator;
public
MessageService(IMessageGenerator messageGenerator)
{
_messageGenerator = messageGenerator;
}
public
string
GetMessage()
{
return
string
.Concat("通过MessageService,",_messageGenerator.GetMessage()
);
}
}
|