当前位置 : 主页 > 网络编程 > net编程 >

ASP.NET MVC如何使用Unity实现Ioc详解

来源:互联网 收集:自由互联 发布时间:2021-07-08
为什么有这篇文章 最近在学ASP.NET MVC项目中使用Ioc,选用了Unity作为依赖注入的容器组件,在网上找了相关的文章简单实现了依赖注入,但想用文件配置的方式进行容器注入的注册,发

为什么有这篇文章

最近在学ASP.NET MVC项目中使用Ioc,选用了Unity作为依赖注入的容器组件,在网上找了相关的文章简单实现了依赖注入,但想用文件配置的方式进行容器注入的注册,发现相关的文章实现的方式不适用,因为网上的文章大多是使用Unity 4.0.1的版本,而目前最新的Unity版本是5.8.6,使用配置进行容器注入的代码已然不同。

Ioc和Unity

IOC(Inversion of Control),即“控制反转”,是一种设计思想。有了IoC后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。

Unity是微软Patterns & Practices 部门开发的一个轻量级的依赖注入容器。

代码准备

新建一个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从数据库中读取数据...
  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();
 }
}

以上代码主要实现从数据库中获取用户列表数据到控制器中。

使用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的容器,然后注册所需要的对象,最后调用。

按上面的方式,每次使用GetAllUsers()前都需要声明下,这里应该封装下。Unity在ASP.NET MVC中的使用已经将代码封装好了。

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代码(使用构造函数注入)

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>

运行站点,成功获取用户列表数据。

扩展

如果需求更改,要换用ADO.NET来操作数据库,只要建一个SQLUserDao的类,继承自IUserDao,然后将配置文件中的注册类型修改即可

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

笔者使用的是VS2017进行操作。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对易盾网络的支持。

网友评论