原文:http://www.asp.net/web-api/overview/advanced/dependency-injection

1 什么是依赖注入(Dependency Injection)

依赖,简单来说就是一个对象需要的任何其他对象。具体解释请Google或百度。在我们使用Web api2  开发应用程序时,通常都会遇到Controller  访问库 Repository 问题。

例如:我们定义一个Domain 对象

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}

接下来用Entity Framework 来实现一个简单的仓储ProductRepository

public class ProductsContext : DbContext
{
public ProductsContext()
: base("name=ProductsContext")
{
}
public DbSet<Product> Products { get; set; }
} public class ProductRepository : IDisposable
{
private ProductsContext db = new ProductsContext(); public IEnumerable<Product> GetAll()
{
return db.Products;
}
public Product GetByID(int id)
{
return db.Products.FirstOrDefault(p => p.Id == id);
}
public void Add(Product product)
{
db.Products.Add(product);
db.SaveChanges();
} protected void Dispose(bool disposing)
{
if (disposing)
{
if (db != null)
{
db.Dispose();
db = null;
}
}
} public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

然后,我们创建一个简单ProductController  API 控制器,实现两个get 请求

public class ProductsController : ApiController
{
// This line of code is a problem! 这行代码有问题

ProductRepository _repository = new ProductRepository();

    public IEnumerable<Product> Get()
{
return _repository.GetAll();
} public IHttpActionResult Get(int id)
{
var product = _repository.GetByID(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}

看这个类的红色行代码,ProductsContoller 依赖了 ProductRepository ,ProductsContoller  在代码中创建了 ProductRepository 的实例,

用这种方式硬编码,是一个糟糕的注意(bad idea),因为:

1 ) 如果你想用不同实现来 替换ProductRepository  ,必须修改 ProductsContoller  类 。(违反了开闭原则)

2 ) 如果ProductRepository   还有其他依赖,你需要在 ProductsContoller   类中实现其他的依赖项,一个大的项目通常都有很多controller ,这会造成大量的实现依赖分散在你的整个项目中。

3) 这很难进行单元测试,因为ProductRepository    已经硬编码在 ProductsContoller   中。对于单元测试,我们应该使用ProductRepository  的mock 或Sub (桩)实例,但这种写法无法进行单元测试。

我们要解决这个问题,只需把ProductRepository   注入到ProductsContoller    类中。 首先重构ProductRepository    实现一个 IProductRespository 接口

public interface IProductRepository
{
IEnumerable<Product> GetAll();
Product GetById(int id);
void Add(Product product);
} public class ProductRepository : IProductRepository
{
// 省略接口的实现
}

然后修改controller ,提供一个构造函数传递 IProductRepository 参数

public class ProductsController : ApiController
{
private IProductRepository _repository; public ProductsController(IProductRepository repository)
{
_repository = repository;
}
// Other controller methods not shown.
}

这个例子使用了构造函数进行注入,你也可以使用属性或者方法进行注入。(个人建议在一个项目中,统一使用一种注入方式,例如都使用构造函数进行注入)。

现在有一个问题,因为我的程序并不能直接创建一个带参数的Controller。 Web API 程序收到一个路由请求时,创建一个controller ,但是它不知道如何创建一个带IProductRepository  参数的 controller。

到这儿,就该依赖注入登场了!

2   The Web API Dependency Resolver (Web API 依赖注入)

Web API 定义了一个IDependencyResolver 接口来 解析依赖,定义如下:

public interface IDependencyResolver : IDependencyScope, IDisposable
{
IDependencyScope BeginScope();
} public interface IDependencyScope : IDisposable
{
object GetService(Type serviceType);
IEnumerable<object> GetServices(Type serviceType);
}

IDependencyScope 接口有两个方法

1) GetService 创建一个类型实例

2) GetServices  创建一个指定类型对象的集合。

IDependencyResolver  继承 IDependencyScope ,并且添加了一个BeginScope 方法,BeginScope 用来管理对象的生命周期。

当Web API 创建一个controller 实例时,它首先调用IDependencyResolver.GetService 方法,再传入controller 的参数类型。 我们可以使用这个可扩展的钩子 来创建Controller 的实例,解决依赖关系。

如果GetService 返回null,WebAPI 会在controller类 中寻找无参的构造函数。

Dependency Resolution with the Unity Container (使用Unity容器来解决依赖)

虽然我们可以从头写完一个IDependencyResolver 的实现,但这个接口的真正设计目的,是充当Web api 和 现存Container 的桥梁。

Container 是一个软件组件,负责管理依赖。用Container 注册 类型,就可以使用Container 来创建类型的实例。容器会自动识别依赖关系,并且很多容器还可以控制对象的生命周期。

本教程,我们使用微软的 Unity 容器来解决依赖。

在包管理控制台中,输入如下命令,安装unity 。

Install-Package Unity

接着再实现一个封装了unity 容器的 IDependencyResolver  接口实现类 。

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies; public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container; public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
} public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
} public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
} public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
} public void Dispose()
{
container.Dispose();
}
}

注意:

1) 如果GetService 不能解析一个类型,那么必须返回 null

2) 如果GetServices 不能解析一个类型,那么必须返回一个空的集合实例。

不能解析类型时,按上面两种情况处理,不能抛出任何异常。

Configuring the Dependency Resolver  配置依赖解析器

最后一步,在全局 HttpConfiguration 对象的DependencyResolver 属性上设置 依赖解析器

public static void Register(HttpConfiguration config)
{
//1 创建容器
var container = new UnityContainer();
// 2 注册组件
container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
//3 配置依赖解析用 unity容器

config.DependencyResolver = new UnityResolver(container);

    // Other Web API configuration not shown.
}

以上代码是写在 Global.asax 文件中的应用程序启动Application_Start() 方法中,

 public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}

5  Dependenecy Scope and Controller Lifetime  依赖范围和控制器的生命周期

每个request都会创建一个controller ,为了管理对象的生命周期,IDependencyResolver 使用一个 Scope 的概念。

dependency resolver 依附 到HttpConfiguration 对象上,有一个全局范围(global scope)。当Web api 创建一个 controller 时, 它调用 BeginScope, 这个方法返回一个IDependencyScope 代表一个子范围

Web api 在这个子范围中 调用 GetService 方法创建Controller ,当请求完成时,Web api 在这个子范围内调用Dispose 方法,用Dispose 这个方法来释放 Controller 的依赖。

你需要实现BeginScope 取决于你使用的容器,对于Unity ,BeginScope 的实现如下:

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}

Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)的更多相关文章

  1. 循序渐进学.Net Core Web Api开发系列【11】:依赖注入

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  2. ASP.NET MVC Web API 学习笔记---Web API概述及程序示例

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  3. [Web API] 如何让 Web API 统一回传格式以及例外处理[转]

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...

  4. [Web API] 如何让 Web API 统一回传格式以及例外处理

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...

  5. Web API 2 入门——Web API 2(C#)入门(谷歌翻译)

    ASP.NET Web API 2(C#)入门 在这篇文章中 本教程中使用的软件版本 创建一个Web API项目 添加模型 添加控制器 使用Javascript和jQuery调用Web API 运行应 ...

  6. 如何在 ASP.Net Web Forms 中使用依赖注入

    依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...

  7. 开发H5程序或者小程序的时候,后端Web API项目在IISExpress调试中使用IP地址,便于开发调试

    在我们开发开发H5程序或者小程序的时候,有时候需要基于内置浏览器或者微信开发者工具进行测试,这个时候可以采用默认的localhost进行访问后端接口,一般来说没什么问题,如果我们需要通过USB基座方式 ...

  8. ASP.NET Core中的依赖注入(1):控制反转(IoC)

    ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...

  9. ASP.NET Core 中的依赖注入 [共7篇]

    一.控制反转(IoC) ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了 ...

随机推荐

  1. JS实现多少小时前,多少天前...

    最近需要实现题目的功能,因为我的时间戳是PHP生成的,所以转换JS时间戳需要乘1000,废话不多说,看下面的代码把! 大家可以判断一下传进来的值是否为数值型,还有判断是否比当前的时间戳大!可以根据结果 ...

  2. SpringBoot 之 打war包

    1.修改打包方式为 war <packaging>war</packaging> 2. 修改tomcat 依赖 <dependency> <groupId&g ...

  3. SVM之Python实现

    SVM Python实现 Python实现SVM的理论知识 SVM原始最优化问题: \[ min_{w,b,\xi}{1\over{2}}{||w||}^2 + C\sum_{i=1}^m\xi^{( ...

  4. c# 快速修改图片颜色

    public static void ChangeColour(this Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byt ...

  5. Array、ArrayList和List三者的区别

    数组 class TestArraysClass { static void Main() { // Declare a single-dimensional array ]; // Declare ...

  6. libtar 和 libz 的使用

    libtar 和 libz 的使用 用c代码生成 tar.gz 文件  实现的功能和 tar -zcf 命令一样 大概流程为 1.先用libtar相关函数对某个目录生成tar文件 2.然后对tar文件 ...

  7. myeclipse更改类或者是配置文件不用重启tomcat的方法

    一.修改java代码(如action)无需重启与部署方法 方法1:在WebRoot下的META-INF文件夹中新建一个名为context.xml文件,里面添加如下内容(要区分大小写): <Con ...

  8. java中的集合和视图

    一.集合的概念 何为集合,集合就是相当于一个对象的容器.集合是类似数组的一个作用.既然有了数组,为何还要有集合呢,由于数组对象一旦创建,其大小便不可以更改,我们只能往数组中存放创建时数量的对象.而集合 ...

  9. OLEDB 简单数据查找定位和错误处理

    在数据库查询中,我们主要使用的SQL语句,但是之前也说过,SQL语句需要经历解释执行的步骤,这样就会拖慢程序的运行速度,针对一些具体的简单查询,比如根据用户ID从用户表中查询用户具体信息,像这样的简单 ...

  10. echarts环形图点击旋转并高亮

    通过计算某个扇形区域的值占整个圆的百分比来得到这个扇形的角度,从而根据startAngle这个属性来设定图形的开始渲染的角度,使点击某个扇形时圆环旋转使之始终对准某个点. 期间考虑到某扇形区域太小点击 ...