我一直在阅读 MSDN 上有关 Unity(依赖注入、控制反转)的文章,但我认为我需要用简单的术语(或简单的示例)对其进行解释。我熟悉 MVPC 模式(我们在这里使用它),但我还不能真正掌握 Unity 的东西,我认为这是我们应用程序设计的下一步。
Unity只是一个IoC“容器”。搜索“StructureMap”并尝试一下。我认为,当 IoC 的东西对你来说是新的时,更容易理解。
基本上,如果您了解 IoC,那么您就会明白您所做的是反转创建对象时的控制。
没有 IoC:
public class MyClass
{
IMyService _myService;
public MyClass()
{
_myService = new SomeConcreteService();
}
}
使用 IoC 容器:
public class MyClass
{
IMyService _myService;
public MyClass(IMyService myService)
{
_myService = myService;
}
}
如果没有 IoC,依赖 IMyService 的类必须新建要使用的服务的具体版本。由于多种原因,这很糟糕(您已将您的类耦合到 IMyService 的特定具体版本,您无法轻松对其进行单元测试,您无法轻松更改它,等等)
使用 IoC 容器,您可以“配置”容器来为您解决这些依赖关系。因此,使用基于构造函数的注入方案,只需将 IMyService 依赖项的接口传递到构造函数中即可。当您使用容器创建 MyClass 时,您的容器将为您解决 IMyService 依赖关系。
使用 StructureMap,配置容器如下所示:
StructureMapConfiguration.ForRequestedType<MyClass>().TheDefaultIsConcreteType<MyClass>();
StructureMapConfiguration.ForRequestedType<IMyService>().TheDefaultIsConcreteType<SomeConcreteService>();
因此,您所做的就是告诉容器,“当有人请求 IMyService 时,给他们一份 SomeConcreteService 的副本。”并且您还指定了当有人请求 MyClass 时,他们会得到一个具体的 MyClass。
这就是 IoC 容器真正要做的全部事情。他们可以做更多的事情,但这就是它的主旨 - 他们为您解决依赖关系,因此您不必这样做(并且您不必在整个代码中使用“new”关键字)。
最后一步:当您创建 MyClass 时,您将执行以下操作:
var myClass = ObjectFactory.GetInstance<MyClass>();
我刚刚观看了 David Hayden 的 30 分钟 Unity 依赖注入 IoC 截屏视频,觉得这是一个很好的示例解释。以下是演出笔记的片段:
截屏视频展示了 Unity IoC 的几种常见用法,例如:
Unity 是一个像许多其他库一样的库,允许您获取所请求类型的实例,而无需自己创建它。所以给出了。
public interface ICalculator
{
void Add(int a, int b);
}
public class Calculator : ICalculator
{
public void Add(int a, int b)
{
return a + b;
}
}
您可以使用像 Unity 这样的库来注册当请求 ICalculator 类型时返回的计算器,即 IoC(控制反转)(此示例是理论上的,技术上不正确)。
IoCLlibrary.Register<ICalculator>.Return<Calculator>();
所以现在当您想要 ICalculator 的实例时,您只需...
Calculator calc = IoCLibrary.Resolve<ICalculator>();
IoC 库通常可以配置为保存单例或在每次解析类型时创建一个新实例。
现在假设您有一个依赖 ICalculator 的课程,您可以拥有..
public class BankingSystem
{
public BankingSystem(ICalculator calc)
{
_calc = calc;
}
private ICalculator _calc;
}
您可以设置库以在创建对象时将对象注入到构造函数中。
因此 DI 或依赖注入意味着注入另一个可能需要的任何对象。
Unity 是一个 IoC。 IoC 的要点是抽象类型本身之外的类型之间的依赖关系。这有几个优点。首先,它是集中完成的,这意味着当依赖项发生变化时您不必更改大量代码(单元测试可能就是这种情况)。
此外,如果使用配置数据而不是代码完成连接,您实际上可以在部署后重新连接依赖项,从而在不更改代码的情况下更改应用程序的行为。
MSDN 有一个使用 Unity 进行依赖注入的开发人员指南,可能会有用。
开发人员指南从依赖注入的基础知识开始,然后提供如何使用 Unity 进行依赖注入的示例。截至 2014 年 2 月,开发人员指南涵盖了 2013 年 4 月发布的 Unity 3.0。
我将介绍 ASP.NET Web API 2 中的大部分依赖注入示例
public interface IShape
{
string Name { get; set; }
}
public class NoShape : IShape
{
public string Name { get; set; } = "I have No Shape";
}
public class Circle : IShape
{
public string Name { get; set; } = "Circle";
}
public class Rectangle : IShape
{
public Rectangle(string name)
{
this.Name = name;
}
public string Name { get; set; } = "Rectangle";
}
在DIAutoV2Controller.cs中使用了自动注入机制
[RoutePrefix("api/v2/DIAutoExample")]
public class DIAutoV2Controller : ApiController
{
private string ConstructorInjected;
private string MethodInjected1;
private string MethodInjected2;
private string MethodInjected3;
[Dependency]
public IShape NoShape { get; set; }
[Dependency("Circle")]
public IShape ShapeCircle { get; set; }
[Dependency("Rectangle")]
public IShape ShapeRectangle { get; set; }
[Dependency("PiValueExample1")]
public double PiValue { get; set; }
[InjectionConstructor]
public DIAutoV2Controller([Dependency("Circle")]IShape shape1, [Dependency("Rectangle")]IShape shape2, IShape shape3)
{
this.ConstructorInjected = shape1.Name + " & " + shape2.Name + " & " + shape3.Name;
}
[NonAction]
[InjectionMethod]
public void Initialize()
{
this.MethodInjected1 = "Default Initialize done";
}
[NonAction]
[InjectionMethod]
public void Initialize2([Dependency("Circle")]IShape shape1)
{
this.MethodInjected2 = shape1.Name;
}
[NonAction]
[InjectionMethod]
public void Initialize3(IShape shape1)
{
this.MethodInjected3 = shape1.Name;
}
[HttpGet]
[Route("constructorinjection")]
public string constructorinjection()
{
return "Constructor Injected: " + this.ConstructorInjected;
}
[HttpGet]
[Route("GetNoShape")]
public string GetNoShape()
{
return "Property Injected: " + this.NoShape.Name;
}
[HttpGet]
[Route("GetShapeCircle")]
public string GetShapeCircle()
{
return "Property Injected: " + this.ShapeCircle.Name;
}
[HttpGet]
[Route("GetShapeRectangle")]
public string GetShapeRectangle()
{
return "Property Injected: " + this.ShapeRectangle.Name;
}
[HttpGet]
[Route("GetPiValue")]
public string GetPiValue()
{
return "Property Injected: " + this.PiValue;
}
[HttpGet]
[Route("MethodInjected1")]
public string InjectionMethod1()
{
return "Method Injected: " + this.MethodInjected1;
}
[HttpGet]
[Route("MethodInjected2")]
public string InjectionMethod2()
{
return "Method Injected: " + this.MethodInjected2;
}
[HttpGet]
[Route("MethodInjected3")]
public string InjectionMethod3()
{
return "Method Injected: " + this.MethodInjected3;
}
}
在 DIV2Controller.cs 中,所有内容都将从依赖配置解析器类注入
[RoutePrefix("api/v2/DIExample")]
public class DIV2Controller : ApiController
{
private string ConstructorInjected;
private string MethodInjected1;
private string MethodInjected2;
public string MyPropertyName { get; set; }
public double PiValue1 { get; set; }
public double PiValue2 { get; set; }
public IShape Shape { get; set; }
// MethodInjected
[NonAction]
public void Initialize()
{
this.MethodInjected1 = "Default Initialize done";
}
// MethodInjected
[NonAction]
public void Initialize2(string myproperty1, IShape shape1, string myproperty2, IShape shape2)
{
this.MethodInjected2 = myproperty1 + " & " + shape1.Name + " & " + myproperty2 + " & " + shape2.Name;
}
public DIV2Controller(string myproperty1, IShape shape1, string myproperty2, IShape shape2)
{
this.ConstructorInjected = myproperty1 + " & " + shape1.Name + " & " + myproperty2 + " & " + shape2.Name;
}
[HttpGet]
[Route("constructorinjection")]
public string constructorinjection()
{
return "Constructor Injected: " + this.ConstructorInjected;
}
[HttpGet]
[Route("PropertyInjected")]
public string InjectionProperty()
{
return "Property Injected: " + this.MyPropertyName;
}
[HttpGet]
[Route("GetPiValue1")]
public string GetPiValue1()
{
return "Property Injected: " + this.PiValue1;
}
[HttpGet]
[Route("GetPiValue2")]
public string GetPiValue2()
{
return "Property Injected: " + this.PiValue2;
}
[HttpGet]
[Route("GetShape")]
public string GetShape()
{
return "Property Injected: " + this.Shape.Name;
}
[HttpGet]
[Route("MethodInjected1")]
public string InjectionMethod1()
{
return "Method Injected: " + this.MethodInjected1;
}
[HttpGet]
[Route("MethodInjected2")]
public string InjectionMethod2()
{
return "Method Injected: " + this.MethodInjected2;
}
}
配置依赖关系解析器
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
RegisterInterfaces(container);
config.DependencyResolver = new UnityResolver(container);
// Other Web API configuration not shown.
}
private static void RegisterInterfaces(UnityContainer container)
{
var dbContext = new SchoolDbContext();
// Registration with constructor injection
container.RegisterType<IStudentRepository, StudentRepository>(new InjectionConstructor(dbContext));
container.RegisterType<ICourseRepository, CourseRepository>(new InjectionConstructor(dbContext));
// Set constant/default value of Pi = 3.141
container.RegisterInstance<double>("PiValueExample1", 3.141);
container.RegisterInstance<double>("PiValueExample2", 3.14);
// without a name
container.RegisterInstance<IShape>(new NoShape());
// with circle name
container.RegisterType<IShape, Circle>("Circle", new InjectionProperty("Name", "I am Circle"));
// with rectangle name
container.RegisterType<IShape, Rectangle>("Rectangle", new InjectionConstructor("I am Rectangle"));
// Complex type like Constructor, Property and method injection
container.RegisterType<DIV2Controller, DIV2Controller>(
new InjectionConstructor("Constructor Value1", container.Resolve<IShape>("Circle"), "Constructor Value2", container.Resolve<IShape>()),
new InjectionMethod("Initialize"),
new InjectionMethod("Initialize2", "Value1", container.Resolve<IShape>("Circle"), "Value2", container.Resolve<IShape>()),
new InjectionProperty("MyPropertyName", "Property Value"),
new InjectionProperty("PiValue1", container.Resolve<double>("PiValueExample1")),
new InjectionProperty("Shape", container.Resolve<IShape>("Rectangle")),
new InjectionProperty("PiValue2", container.Resolve<double>("PiValueExample2")));
}