1、Nuget Package Manager
-> 搜索Unity.mvc4
-> Install
-> 安装完成出现如下引用
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Configuracion
Microsoft.Practices.Unity.RegistrationByConvention
Unity.Mvc4
生成文件如下:
如果有生成Bootstrapper.cs文件,代码如下
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.Mvc;
using System.Configuration;
using System.Web.Mvc;
namespace Web
{
public class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
#region 依赖注入映射
//container.RegisterType<IDbConnectionFactory, SqlConnectionFactory>();
//container.RegisterType<ILanguageService, LanguageService>();
//container.RegisterType<IUserService, UserService>();
#endregion
#region web.config依赖注入
((UnityConfigurationSection)ConfigurationManager.GetSection("unity")).Configure(container);
#endregion
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
}
}
}
如果有生成App_Start\UnityConfig.cs、App_Start\UnityWebActivator两个文件,代码如下
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using Dapper.Repository;
using Dapper.Serivce;
namespace Web.App_Start
{
/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
#region 依赖注入映射
//container.RegisterType<IDbConnectionFactory, SqlConnectionFactory>();
//container.RegisterType<ILanguageService, LanguageService>();
//container.RegisterType<IUserService, UserService>();
#endregion
#region web.config依赖注入
((UnityConfigurationSection)ConfigurationManager.GetSection("unity")).Configure(container);
#endregion
RegisterTypes(container);
return container;
});
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();
// TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
}
}
}
2、Global.aspx.cs添加依赖注入代码
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Web.App_Start;
namespace Web
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
#region 依赖注入
///方式一
//Bootstrapper.Initialise();
///方式二
UnityWebActivator.Start();
#endregion
}
}
}
3、控制器UsersController.cs调用
using Dapper.Entity;
using Dapper.Serivce;
using System;
using System.Web.Mvc;
namespace Web.Controllers
{
public class UsersController : Controller
{
private ILanguageService _languageSvc;
private IUserService _userSvc;
public UsersController(ILanguageService languageService, IUserService userService)
{
_languageSvc = languageService;
_userSvc = userService;
}
public ActionResult Index()
{
var model = new Languages
{
Title = "Title",
EnglishTitle = "EnglishTitle",
CreateBy = 0,
CreatedDate = DateTime.Now,
ModifiedBy = 0,
UpdatedDate = DateTime.Now,
State = "A"
};
//_languageSvc.Add(model);
_userSvc.Add(model);
return View();
}
}
}
4、配置文件没有配置也能访问
如果Bootstrapper.cs BuildUnityContainer()有写依赖注入代码,web.config就无须配置;
如果UnityConfig.cs Lazy<IUnityContainer>有写依赖注入代码,web.config就无须配置;
5、配置文件示例
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<containers>
<container>
<types>
<type type="Dapper.Repository.IDbConnectionFactory,Dapper.Repository" mapTo="Dapper.Repository.SqlConnectionFactory,Dapper.Repository"></type>
<type type="Dapper.Serivce.ILanguageService,Dapper.Serivce" mapTo="Dapper.Serivce.LanguageService,Dapper.Serivce"></type>
<type type="Dapper.Serivce.IUserService,Dapper.Serivce" mapTo="Dapper.Serivce.UserService,Dapper.Serivce"></type>
</types>
</container>
</containers>
</unity>