Dependency Injection in ASP.NET Web API 2 Using Unity
What is Dependency Injection?
A dependency is any object that another object requires. For example, it's common to define a repository that handles data access. Let's illustrate with an example. First, we'll define a domain model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Here is a simple repository class that stores items in a database, using Entity Framework.
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);
}
}
Now let's define a Web API controller that supports GET requests for Product entities. (I'm leaving out POST and other methods for simplicity.) Here is a first attempt:
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);
}
}
Notice that the controller class depends on ProductRepository, and we are letting the controller create the ProductRepository instance. However, it's a bad idea to hard code the dependency in this way, for several reasons.
- If you want to replace
ProductRepositorywith a different implementation, you also need to modify the controller class. - If the
ProductRepositoryhas dependencies, you must configure these inside the controller. For a large project with multiple controllers, your configuration code becomes scattered across your project. - It is hard to unit test, because the controller is hard-coded to query the database. For a unit test, you should use a mock or stub repository, which is not possible with the currect design.
We can address these problems by injecting the repository into the controller. First, refactor the ProductRepository class into an interface:
public interface IProductRepository
{
IEnumerable<Product> GetAll();
Product GetById(int id);
void Add(Product product);
} public class ProductRepository : IProductRepository
{
// Implementation not shown.
}
Then provide the IProductRepository as a constructor parameter:
public class ProductsController : ApiController
{
private IProductRepository _repository; public ProductsController(IProductRepository repository)
{
_repository = repository;
} // Other controller methods not shown.
}
This example uses constructor injection. You can also use setter injection, where you set the dependency through a setter method or property.
But now there is a problem, because your application doesn't create the controller directly. Web API creates the controller when it routes the request, and Web API doesn't know anything about IProductRepository. This is where the Web API dependency resolver comes in.
The Web API Dependency Resolver
Web API defines the IDependencyResolver interface for resolving dependencies. Here is the definition of the interface:
public interface IDependencyResolver : IDependencyScope, IDisposable
{
IDependencyScope BeginScope();
} public interface IDependencyScope : IDisposable
{
object GetService(Type serviceType);
IEnumerable<object> GetServices(Type serviceType);
}
The IDependencyScope interface has two methods:
- GetService creates one instance of a type.
- GetServices creates a collection of objects of a specified type.
The IDependencyResolver method inherits IDependencyScope and adds the BeginScope method. I'll talk about scopes later in this tutorial.
When Web API creates a controller instance, it first calls IDependencyResolver.GetService, passing in the controller type. You can use this extensibility hook to create the controller, resolving any dependencies. If GetService returns null, Web API looks for a parameterless constructor on the controller class.
Dependency Resolution with the Unity Container
Although you could write a complete IDependencyResolver implementation from scratch, the interface is really designed to act as bridge between Web API and existing IoC containers.
An IoC container is a software component that is responsible for managing dependencies. You register types with the container, and then use the container to create objects. The container automatically figures out the dependency relations. Many IoC containers also allow you to control things like object lifetime and scope.
Note
"IoC" stands for "inversion of control", which is a general pattern where a framework calls into application code. An IoC container constructs your objects for you, which "inverts" the usual flow of control.
For this tutorial, we'll use Unity from Microsoft Patterns & Practices. (Other popular libraries include Castle Windsor, Spring.Net, Autofac, Ninject, andStructureMap.) You can use NuGet Package Manager to install Unity. From the Tools menu in Visual Studio, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:
Install-Package Unity
Here is an implementation of IDependencyResolver that wraps a Unity container.
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()
{
Dispose(true);
} protected virtual void Dispose(bool disposing)
{
container.Dispose();
}
}
Note
If the GetService method cannot resolve a type, it should return null. If the GetServices method cannot resolve a type, it should return an empty collection object. Don't throw exceptions for unknown types.
Configuring the Dependency Resolver
Set the dependency resolver on the DependencyResolver property of the global HttpConfiguration object.
The following code registers the IProductRepository interface with Unity and then creates a UnityResolver.
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container); // Other Web API configuration not shown.
}
Dependency Scope and Controller Lifetime
Controllers are created per request. To manage object lifetimes, IDependencyResolver uses the concept of a scope.
The dependency resolver attached to the HttpConfiguration object has global scope. When Web API creates a controller, it calls BeginScope. This method returns an IDependencyScope that represents a child scope.
Web API then calls GetService on the child scope to create the controller. When request is complete, Web API calls Dispose on the child scope. Use the Dispose method to dispose of the controller's dependencies.
How you implement BeginScope depends on the IoC container. For Unity, scope corresponds to a child container:
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}
Most IoC containers have similar equivalents.
Dependency Injection in ASP.NET Web API 2 Using Unity的更多相关文章
- Dependency Injection in ASP.NET Web API 2
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...
- Asp.Net Web API 2第十一课——在Web API中使用Dependency Resolver
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文主要来介绍在Asp.N ...
- ASP.NET Web API的Controller是如何被创建的?
Web API调用请求的目标是定义在某个HttpController类型中的某个Action方法,所以消息处理管道最终需要激活目标HttpController对象.调用请求的URI会携带目标HttpC ...
- 目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建
目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建 通过上面的介绍我们知道利用HttpControllerSelector可以根据 ...
- Asp.net web api 知多少
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
- ASP.NET Web API 框架研究 IoC容器 DependencyResolver
一.概念 1.IoC(Inversion of Control),控制反转 即将依赖对象的创建和维护交给一个外部容器来负责,而不是应用本身.如,在类型A中需要使用类型B的实例,而B的实例的创建不是由A ...
- ASP.NET Web API 中的返回数据格式以及依赖注入
本篇涉及ASP.NET Web API中的返回数据合适和依赖注入. 获取数据 public IEnumerable<Food> Get() { var results = reop.Get ...
- ASP.NET Web API - 使用 Castle Windsor 依赖注入
示例代码 项目启动时,创建依赖注入容器 定义一静态容器 IWindsorContainer private static IWindsorContainer _container; 在 Applica ...
随机推荐
- Ubuntu下安装virtualbox失败解决方案
安装失败的截图: 因此使用常规方法:对依赖的两个包进行获取安装,依旧失败: 因此解决方法为在官网上下载相对应版本的virtualbox软件:下载地址为:https://www.virtualbox.o ...
- 理解 js的 async/await
async 和await 在干什么? async 用于声明一个function是异步的 await用于等待一个异步方法执行完成(其实我理解的是等待的是一个表达式,就是一个结果), 其中 await ...
- 动态改变UITabBarController的菜单文字
有时候项目可能涉及到使用多种语言,如简体.繁体.为了适应这种情况我用到了Localizable.strings,然后在不同的语言版本文件内定义相应的内容(这就不说了,可以参考:http://www.c ...
- Global.asax 详解
在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...
- ConfigUtil读取配置文件
package utils; import java.util.ResourceBundle; public class ConfigUtil { private static ResourceBun ...
- 原型设计 Axure8.1 软件注册码
用户名:Koshy 注册码: wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6xY
- hdu 5185 dp(完全背包)
BC # 32 1004 题意:要求 n 个数和为 n ,而且后一个数等于前一个数或者等于前一个数加 1 ,问有多少种组合. 其实是一道很水的完全背包,但是没有了 dp 的分类我几乎没有往这边细想,又 ...
- mysql 常用linux命令
★ 数据导出命令 D:\Program Files\MySQL\MySQL Server 5.6.39\bin 导出: mysqldump -u root -p cela_sub > D:/d ...
- 记录一次服务器CPU 100%的解决过程
昨天客户反馈业务系统很慢,而且偶尔报错. 查看nginx日志: [root@s2 nginx]# tail log/error.log 2017/03/14 12:54:46 [error] 1704 ...
- LG2023 [AHOI2009]维护序列
题意 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数 ...