欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET MVC如何使用Unity實現(xiàn)Ioc詳解

 更新時間:2018年07月10日 10:04:21   作者:瞭望島  
這篇文章主要給大家介紹了關(guān)于ASP.NET MVC如何使用Unity實現(xiàn)Ioc的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

為什么有這篇文章

最近在學(xué)ASP.NET MVC項目中使用Ioc,選用了Unity作為依賴注入的容器組件,在網(wǎng)上找了相關(guān)的文章簡單實現(xiàn)了依賴注入,但想用文件配置的方式進行容器注入的注冊,發(fā)現(xiàn)相關(guān)的文章實現(xiàn)的方式不適用,因為網(wǎng)上的文章大多是使用Unity 4.0.1的版本,而目前最新的Unity版本是5.8.6,使用配置進行容器注入的代碼已然不同。

Ioc和Unity

IOC(Inversion of Control),即“控制反轉(zhuǎn)”,是一種設(shè)計思想。有了IoC后,把創(chuàng)建和查找依賴對象的控制權(quán)交給了容器,由容器進行注入組合對象,所以對象與對象之間是松散耦合,這樣也方便測試,利于功能復(fù)用,更重要的是使得程序的整個體系結(jié)構(gòu)變得非常靈活。

Unity是微軟Patterns & Practices 部門開發(fā)的一個輕量級的依賴注入容器。

代碼準(zhǔn)備

新建一個MVC項目,使用默認命名WebApplication1。在Model中新建下面3個類:

public class User
{
 public int Id { get; set; }
 public string UserName { get; set; }
 public string Password { get; set; }
 public string Email { get; set; }
}
public interface IUserDao
{
 List<User> GetAllUsers();
}
public class EFUserDao : IUserDao
{
 public List<User> GetAllUsers()
 {
  List<User> list = new List<User>();
  //使用EF從數(shù)據(jù)庫中讀取數(shù)據(jù)...
  return list;
 }
}

HomeController中的Index()中編寫代碼:

using WebApplication1.Models;
public class HomeController : Controller
{
 public ActionResult Index()
 {
  IUserDao dao = new EFUserDao();
  var list = dao.GetAllUsers();

  //do something...

  return View();
 }
}

以上代碼主要實現(xiàn)從數(shù)據(jù)庫中獲取用戶列表數(shù)據(jù)到控制器中。

使用Unity

在項目引用上右擊,管理Nuget程序包,搜索到Unity并安裝。

HomeController中代碼改動

using WebApplication1.Models;
using Unity;
public class HomeController : Controller
{
 public ActionResult Index()
 {
  IUnityContainer container = new UnityContainer();
  container.RegisterType<IUserDao, EFUserDao>();
  var dao = container.Resolve<IUserDao>();

  var list = dao.GetAllUsers();
  //do something...

  return View();
 }
}

上面代碼先聲明一個Unity的容器,然后注冊所需要的對象,最后調(diào)用。

按上面的方式,每次使用GetAllUsers()前都需要聲明下,這里應(yīng)該封裝下。Unity在ASP.NET MVC中的使用已經(jīng)將代碼封裝好了。

ASP.NET MVC使用Unity

使用Nuget安裝Unity.MVC。

 

安裝完成后會在~/App_Start/目錄下自動生成UnityMvcActivator.cs和UnityConfig.cs文件。

打開UnityConfig文件,修改RegisterTypes()方法的代碼

public static void RegisterTypes(IUnityContainer container)
{
 // NOTE: To load from web.config uncomment the line below.
 // Make sure to add a Unity.Configuration to the using statements.
 // container.LoadConfiguration();

 // TODO: Register your type's mappings here.
 container.RegisterType<IUserDao, EFUserDao>();
}

注意引用

using WebApplication1.Models;

修改HomeController代碼(使用構(gòu)造函數(shù)注入)

public class HomeController : Controller
{
 IUserDao _iUserDao;

 public HomeController(IUserDao iUserDao)
 {
  this._iUserDao = iUserDao;
 }

 public ActionResult Index()
 {
  var list = _iUserDao.GetAllUsers();

  //do something...

  return View();
 }
}

此方式是將依賴注入寫在了代碼中。然而并不靈活,每添加一組類,都要在UnityConfig中進行注冊并編譯一遍代碼。我們更需要的是在配置文件中注冊類型。

使用配置文件

修改UnityConfig文件中RegisterTypes()方法的代碼:

public static void RegisterTypes(IUnityContainer container)
{
 // NOTE: To load from web.config uncomment the line below.
 // Make sure to add a Unity.Configuration to the using statements.
 container.LoadConfiguration();

 // TODO: Register your type's mappings here.
 // container.RegisterType<IUserDao, EFUserDao>();
}

需要引用

using Microsoft.Practices.Unity.Configuration;

 

更改Web.Config的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
 </configSections>
 <unity>
 <containers>
  <container>
  <types>
   <type type="WebApplication1.Models.IUserDao, WebApplication1" mapTo="WebApplication1.Models.EFUserDao, WebApplication1" />
  </types>
  </container>
 </containers>
 </unity>
 ......
</configuration>

運行站點,成功獲取用戶列表數(shù)據(jù)。

擴展

如果需求更改,要換用ADO.NET來操作數(shù)據(jù)庫,只要建一個SQLUserDao的類,繼承自IUserDao,然后將配置文件中的注冊類型修改即可

<type type="WebApplication1.Models.IUserDao, WebApplication1" mapTo="WebApplication1.Models.SQLUserDao, WebApplication1" />

筆者使用的是VS2017進行操作。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論