DI and IOC examples in simple C#
Possible Duplicate:
Inversion of Control < Dependency Injection
Could anyone please help me to understand DI and IOC with simple C# example please? As I understand IOC is the inversion of control flow (which means nothing to me) and DI means injecting interfaces via properties or constructors. Not sure how these two are related.
Thank you.
I'll add an explanation that I've been told is a helpful one.
As you correctly point out, dependency injection is the act of a class demanding its collaborators via constructor or setter injection. You could think of this as a coding technique. Inversion of control, on the other hand, is more of a design technique -- a philosophy, if you will.
In procedural programming (and procedural-style programming in OO languages), you generally have some kind of "command and control" philosophy. If you're creating a house object that consists of three floor objects, which consist of some number of room objects each, you create a house object whose constructor instantiates three floor objects, each of whose constructor, in turn, instantiates room objects.
When you invert control, you're inverting command and control paradigm. With IoC, when I go to instantiate a house, it doesn't take care of all of its details for me. Instead, it says, via its constructor (and dependency injection), "you can't instantiate me without some floors". So, you go to instantiate the floors and they say "nope, not without some rooms". Now, in this "inverted" style, our house doesn't control the creation of the rooms - it leaves that up to someone else and it expresses instead, only what it will do with the rooms it's given. This is desirable because, by flipping command and control on its head, you have better seams for reasoning about and testing the code -- it's easier to deconstruct things into components.
So, to summarize, inversion of control is the philosophy and dependency injection is the means by which it's achieved.
Let's say you have a class that needs a service:
public class A
{
IEmailSender _emailSender
public A(IEmailSender emailSender)
{
_emailSender = emailSender;
}
private void SendEmail()
{
_emailSender.Send();
}
}
Class A now is said to has a dependency on IEMailSender, meaning it needs an object that implements IEMailSender to be able to perform it's function.
A dependency injection framework like ninject or autofac would be responsible for creating an object that implements IEMailSender and injecting it into the constructor of A , provided that you told them how to create such object.
Ex using ninject:
Bind<IEmailSender>().To<EmailSender>();
this is telling ninject, whenever a class needs an IEMailSender, give them EmailSender (assuming ofcourse that EmailSender implements the IEMailSender interface)
This is as simple as I can think of, hope this helps.
I highly recommend you to read Dependency Injection in .NET book, it in details describes all aspects with comprehensive code examples. For the inversion of control you should refer to another book by Robert Martin: Agile Principles, Patterns, and Practices in C#. It in details describes SOLID principles with examples and understandable explanations. These two books really helped me to understand these principles.
链接地址: http://www.djcxy.com/p/82232.html上一篇: 控制反转(依赖反转)系统中的事件是以哪种方式进行的?
下一篇: 简单C#中的DI和IOC示例