当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC如何使用Unity实现Ioc详解

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

2018年08月08日  | 移动技术网IT编程  | 我要评论

dvaj-033,台山市人才网,二维影像测量仪

为什么有这篇文章

最近在学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进行操作。

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网