这几天看了看 IOC DI 的东西, 顺手写个Hello World. 不知自己理解是否正确, 特来向各位朋友求证.
我想,肯定也有朋友不清楚这个东西, 顺便把我的理解写出来, 希望能帮到的不仅仅是自己,闲话少说:
DI 基本思想: 用一个单独的对象(装配器 Assembler) 来获得一个接口的合适的实现,并将其实例,赋给调用接口的类的一个字段. (可能没说清楚, 大家可以看 这里)我用的是接口注入的形式:MsgClass 类
namespace HelloWorld_DI_Interface
{
class MsgClass:IInjectPrinter
{
private IMsgPrinter _msgPrinter;
public void injectPrinter(IMsgPrinter printer)
{
this._msgPrinter = printer;
}
public void ShowMsg()
{
this._msgPrinter.printMsg();
}
}
}接口
namespace HelloWorld_DI_Interface
{
// 接口注入, 组件的注入,通过此接口进行.
// 此接口将一个 IInjectPrinter 实例 注入继承了该接口的对象
interface IInjectPrinter
{
void injectPrinter(IMsgPrinter printer);
} interface IInjectMsg
{
void injectMsg(string msg);
}
}接口实现类using System;namespace HelloWorld_DI_Interface
{
class MsgPrinterImpl:IMsgPrinter,IInjectMsg
{
private string _msg;
public void injectMsg(string msg)
{
this._msg = msg;
} public void printMsg()
{
Console.WriteLine(_msg);
}
}
}namespace HelloWorld_DI_Interface
{
interface IMsgPrinter
{
void printMsg();
}
}
装配器 Assembler
namespace HelloWorld_DI_Interface
{
public class Assembler
{
private MsgClass _msg;
private void configurePrinter()
{
MsgPrinterImpl printer = new MsgPrinterImpl();
printer.injectMsg("12345: Hello World!");
_msg = new MsgClass();
this._msg.injectPrinter(printer);
} public void Test()
{
configurePrinter();
this._msg.ShowMsg();
}
}
}
using System;namespace HelloWorld_DI_Interface
{
class Program
{
static void Main(string[] args)
{
Assembler assembler = new Assembler();
assembler.Test();
Console.ReadKey();
}
}
}
下面写下我的理解: -_-
我想,肯定也有朋友不清楚这个东西, 顺便把我的理解写出来, 希望能帮到的不仅仅是自己,闲话少说:
DI 基本思想: 用一个单独的对象(装配器 Assembler) 来获得一个接口的合适的实现,并将其实例,赋给调用接口的类的一个字段. (可能没说清楚, 大家可以看 这里)我用的是接口注入的形式:MsgClass 类
namespace HelloWorld_DI_Interface
{
class MsgClass:IInjectPrinter
{
private IMsgPrinter _msgPrinter;
public void injectPrinter(IMsgPrinter printer)
{
this._msgPrinter = printer;
}
public void ShowMsg()
{
this._msgPrinter.printMsg();
}
}
}接口
namespace HelloWorld_DI_Interface
{
// 接口注入, 组件的注入,通过此接口进行.
// 此接口将一个 IInjectPrinter 实例 注入继承了该接口的对象
interface IInjectPrinter
{
void injectPrinter(IMsgPrinter printer);
} interface IInjectMsg
{
void injectMsg(string msg);
}
}接口实现类using System;namespace HelloWorld_DI_Interface
{
class MsgPrinterImpl:IMsgPrinter,IInjectMsg
{
private string _msg;
public void injectMsg(string msg)
{
this._msg = msg;
} public void printMsg()
{
Console.WriteLine(_msg);
}
}
}namespace HelloWorld_DI_Interface
{
interface IMsgPrinter
{
void printMsg();
}
}
装配器 Assembler
namespace HelloWorld_DI_Interface
{
public class Assembler
{
private MsgClass _msg;
private void configurePrinter()
{
MsgPrinterImpl printer = new MsgPrinterImpl();
printer.injectMsg("12345: Hello World!");
_msg = new MsgClass();
this._msg.injectPrinter(printer);
} public void Test()
{
configurePrinter();
this._msg.ShowMsg();
}
}
}
using System;namespace HelloWorld_DI_Interface
{
class Program
{
static void Main(string[] args)
{
Assembler assembler = new Assembler();
assembler.Test();
Console.ReadKey();
}
}
}
下面写下我的理解: -_-
类 MsgClass 定义了一个public void ShowMsg()方法;
ShowMsg() 用来显示信息msg. 如果如往常一样, 给ShowMsg 传个参数, 然后用Console.WriteLine()打印出来, 没问题, 但主控权就在这个ShowMsg()方法这里了.假如,以后修改程序, 希望在打印信息前, 做些其它的操作, 比如, 加一段"----------------------"在信息前. 当然,这可以修改ShowMsg() 在打印信息前加上个Console.WriteLine("---------------------"). 这就需要修改ShowMsg(). 而DI的思想是, 不修改这个ShowMsg()ShowMsg()调用 接口IMsgPrinter 的printMsg()方法, 这个printMsg() 来处理打印的工作. 类MsgPrinterImpl 实现该接口.接口注入: 在接口中定义需要注入的信息,并通过接口完成注入. 首先,需要定义一个接口,组件的注入,将通过此接口进行.接口IInjectPrinter// 此接口将一个 IInjectPrinter 实例 注入继承了该接口的对象
interface IInjectPrinter
{
void injectPrinter(IMsgPrinter printer);
}
接口IInjectMsg// 此接口用于向 实现了 IInjectPrinter 接口的类的对象 (即:MsgPrinterImpl 的对象) 注入要打印的信息
interface IInjectMsg
{
void injectMsg(string msg);
}下面就是装配器 Assembler 了
public class Assembler
{
private MsgClass _msg;
private void configurePrinter()
{
MsgPrinterImpl printer = new MsgPrinterImpl();
printer.injectMsg("12345: Hello World!");
_msg = new MsgClass();
this._msg.injectPrinter(printer);
} public void Test()
{
configurePrinter();
this._msg.ShowMsg();
}
}
Assembler的工作:
1-- 将要打印的信息, 注入到MsgPrinterImpl 的对象 printer
2-- 将printer 赋给 MsgClass的字段 _msgTest() 代码块用于测试.
不是教学, 是请教来学.... 呵呵
我把我的想法说出来, 不对的地方, 请大家指正
因为不讲出来,总觉得自己是一知半解,而且不能确定是不是对的, 所以 希望各位不吝赐教哈!Thanks!
倘若以后, 打印的要求变了, 也就是说 要变 MsgPrinterImpl. 或者更换 MsgPrinterImpl 而改用其它实现了接口IMsgPrinter的类主程序不用改, 我只需要改变装配器 (Assembler) 这样, 主控权不在主程序, 而是在装配器. 对主程序是透明的.在编译阶段, 主程序不知道使用哪个实现类. 而是在运行时,注入了实现类. 注入的动作和MsgClass 无关, 只要是实现了IMsgPrinter的类对象就ok.