Autofac是傳說中速度最快的一套.NET高效的依賴注入框架。Autofac的介紹與使用請去參考Autofac全面解析系列(版本:3.5)。 這里介紹的已經挺詳細的啦。
下面我就先來說說MVC4中使用Autofac吧,至於工廠模式與依賴注入的區別的話,這個我簡單的解釋一下,也只是我的個人觀點。使用依賴注入最主要的就是為了解耦,當然工廠模式也可以實現實現大部分的解耦,這個是毋庸置疑的。工廠模式實現方式是向客戶端提供一個接口,使客戶端不要指定具體產品對象,創建多個產品族的產品對象。將具體實例的創建延遲到對應實現的子類中。但是當我們每次要去拿這個接口的時候就要通過這個工廠來拿了,那么就是說雖然我們排除了對實現接口的具體的方法的依賴,但是我們對工廠模式產生接口的方式產生了依賴。那么依賴注入就可以解決這個依賴了。我們在使用接口的時候可以完全的不需要考慮去取這個接口然后再繼續使用接口。我們只要直接拿接口來用就可以了。這就是我個人對工廠模式和依賴注入的解釋了,是不是把你們給搞混淆了。
下面我就在MVC4中使用下Autofac吧。首先我們要先去NuGet下載一個Autofac MVC4的東西
點擊安裝。
將AuthoryManage.AutofacRegister 也引入Autofac包
導入成功之后,我先在AuthoryManage.InterfaceRepository這個類庫中添加一個接口,

using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthoryManage.InterfaceRepository { public interface IBaseRepository { string GetData(); } }
然后在AuthoryManage.Repository中分別加入下面這些類
下面是代碼:

public class BaseRepository :IBaseRepository{ public string GetData() { return "這里是MsSql"; } }

public class BaseRepository : IBaseRepository { public string GetData() { return "這里是MySql"; } }

public class BaseRepository : IBaseRepository { public string GetData() { return "這里是Oracle"; } }
記得添加對AuthoryManage.InterfaceRepository的引用。
接下來寫下Service層的代碼
public interface IBaseService { string GetData(); }

public interface IBaseService { string GetData(); }
最值得注意的Service實現類代碼,請看:

public class BaseService:IBaseService { private readonly IBaseRepository _repository; public BaseService(IBaseRepository repository) { this._repository = repository; } public string GetData() { return _repository.GetData(); } }
這是建好之后的Service層結構
也要記得引用對應DLL,那么接下來就看看控制器這邊怎么寫吧。
我就簡單先建立一個Home控制器吧。並添加對service的引用。

public class HomeController : Controller { // // GET: /Home/ private IBaseService _service; public HomeController(IBaseService service) { this._service = service; } public ActionResult Index() { ViewBag.SSSS = _service.GetData(); return View(); } }
視圖代碼如下:

@{ ViewBag.Title = "Index"; } <h2>@ViewBag.SSSS</h2>
接下來就去Global中實現我們的注入:

using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Autofac.Integration.Mvc; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace AuthoryManage.Web { // 注意: 有關啟用 IIS6 或 IIS7 經典模式的說明, // 請訪問 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var builder = new ContainerBuilder(); var assembly = Assembly.GetExecutingAssembly(); var repository = System.Reflection.Assembly.Load("AuthoryManage.MsSqlRepository"); builder.RegisterAssemblyTypes(repository, repository) .AsImplementedInterfaces(); var service = System.Reflection.Assembly.Load("AuthoryManage.Service"); builder.RegisterAssemblyTypes(service, service) .AsImplementedInterfaces(); builder.RegisterControllers(typeof(MvcApplication).Assembly); //容器 var container = builder.Build(); //注入改為Autofac注入 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } }
要記住,在反射程序集的時候你要引用相對應的程序集。然后我們在運行一下程序來看下界面效果:
我們把global.asax里面的代碼更改下,改成對Oracle的程序集注冊,

using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Autofac.Integration.Mvc; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace AuthoryManage.Web { // 注意: 有關啟用 IIS6 或 IIS7 經典模式的說明, // 請訪問 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var builder = new ContainerBuilder(); var assembly = Assembly.GetExecutingAssembly(); var repository = System.Reflection.Assembly.Load("AuthoryManage.OracleRepository"); builder.RegisterAssemblyTypes(repository, repository) .AsImplementedInterfaces(); var service = System.Reflection.Assembly.Load("AuthoryManage.Service"); builder.RegisterAssemblyTypes(service, service) .AsImplementedInterfaces(); builder.RegisterControllers(typeof(MvcApplication).Assembly); //容器 var container = builder.Build(); //注入改為Autofac注入 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } }
看下運行效果:
需要注意的是我們需要引用下AuthoryManage.OracleRepository這個dll引用,那么就是說我實現的方法更改了 我UI層也要去改動對應的引用。