《模式——工程化实现及扩展》(设计模式C# 版)《重新研读C#语言》——“自我检验" 参考答案:依赖注入部分
转自:《模式——工程化实现及扩展》(设计模式C# 版)
http://www.cnblogs.com/callwangxiang/
1、 目标类型定义如下,它依赖于IFirst和ISecond两个接口,已知实现了IFirst接口的类型有First1、First2和First3,实现了ISecond类型有Second1和Second2。请使用设值注入法完成注入,同时对注入的结果进行验证。
interface IFirst {}
interface ISecond{}
class Target
{
public IFirst First { get; set; }
public ISecond Second { get; set; }
}
class First1 : IFirst{}
class First2 : IFirst { }
class First3 : IFirst { }
class Second1 : ISecond{}
class Second2 : ISecond{}
interface ISecond{}
class Target
{
public IFirst First { get; set; }
public ISecond Second { get; set; }
}
class First1 : IFirst{}
class First2 : IFirst { }
class First3 : IFirst { }
class Second1 : ISecond{}
class Second2 : ISecond{}
参考答案
[TestMethod]
public void TestSetterInjection()
{
var target = new Target()
{
First = new First1(),
Second = new Second2()
};
Assert.IsNotNull(target);
Assert.IsInstanceOfType(target.First, typeof(First1));
Assert.IsInstanceOfType(target.Second, typeof(Second2));
}
public void TestSetterInjection()
{
var target = new Target()
{
First = new First1(),
Second = new Second2()
};
Assert.IsNotNull(target);
Assert.IsInstanceOfType(target.First, typeof(First1));
Assert.IsInstanceOfType(target.Second, typeof(Second2));
}
贸易电子化,技术全球化