溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET MVC使用Unity實(shí)現(xiàn)Ioc的方法

發(fā)布時(shí)間:2021-02-07 15:02:52 來(lái)源:億速云 閱讀:324 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)ASP.NET MVC使用Unity實(shí)現(xiàn)Ioc的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

為什么有這篇文章

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

Ioc和Unity

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

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

代碼準(zhǔn)備

新建一個(gè)MVC項(xiàng)目,使用默認(rèn)命名WebApplication1。在Model中新建下面3個(gè)類:

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ù)庫(kù)中讀取數(shù)據(jù)...
  return list;
 }
}

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

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

  //do something...

  return View();
 }
}

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

使用Unity

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

ASP.NET MVC使用Unity實(shí)現(xiàn)Ioc的方法

HomeController中代碼改動(dòng)

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();
 }
}

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

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

ASP.NET MVC使用Unity

使用Nuget安裝Unity.MVC。

 ASP.NET MVC使用Unity實(shí)現(xiàn)Ioc的方法

安裝完成后會(huì)在~/App_Start/目錄下自動(dòng)生成UnityMvcActivator.cs和UnityConfig.cs文件。

打開(kāi)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();
 }
}

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

使用配置文件

修改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>

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

擴(kuò)展

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

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

感謝各位的閱讀!關(guān)于“ASP.NET MVC使用Unity實(shí)現(xiàn)Ioc的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI