Does anyone have good examples of IoC containers (preferably in c#) and how and why to use them ? I have checked out the wiki page and Ayende's example, but I don't quite get the concept yet.
And when and where should I use an IoC container ?
If you want to see an IoC container under the hood, and also the point (Dependency Injection), there's a great podcast on DNR TV (Episode 126) that really goes into detail about how to create them, why you'd need them. It's a really wonderful podcast. Once you've watched this video, you'll then be able to look at Unity,Ninject, StructureMap, etc and be able to understand what they're doing
Are you trying to build a IoC container why not use one of the available ones like Spring.NET, StructureMap or Unity Application Block? Here is a list of open-source IoC projects
I normally use StructureMap - mostly because I'm familiar with the syntax. I've also heard good things about autofac and I'm looking forward to trying out Ninject when it hits v2.
You might want to take a look at this answer where I talk about a basic usage of an IoC container (I always think things are easier to understand with a simple example) - that might help you to understand things a little more. Basically, the IoC container helps you to build objects with all the dependencies satisfied, and allows you to change your dependencies with minimal configuration code.
I've used StructureMap quite a bit. The rest of your question is pretty loaded. I'll try to explain the concept in an example.
Suppose you created a website that will accept payments through PayPal. PayPal is now a dependency. But you don't want to code against a specific PayPal provider.
Instead, you would create and code against an interface like this:
All your PayPal code would reside in a class that implements the methods of your interface - PayPalPaymentProcessor, for example.
Now you have an object that you will actually use to process the payments. This could be a Controller (ASP.NET-MVC, ViewModel-WPF) or just a class as shown here:
class PaymentProcessor
{
private IPaymentProcessor _processor = null;
public PaymentProcessor(IPaymentProcessor processor)
{
_processor = processor;
}
public bool ProcessTransaction(Transaction trans)
{
_processor.ProcessPayment(trans.amount, ...);
}
}
This is where an IoC container comes in. Instead of you calling the constructor manually, you would let an IoC container inject the dependency:
All this mapping is separate from your implementation code and you could swap out these at a later point with little refactoring needed. There is a lot more to IoC containers, but that the basic concept. You can automate the injection of constructors to avoid the calls directly to ObjectFactory as well.
If you want to see an IoC container under the hood, and also the point (Dependency Injection), there's a great podcast on DNR TV (Episode 126) that really goes into detail about how to create them, why you'd need them. It's a really wonderful podcast. Once you've watched this video, you'll then be able to look at Unity,Ninject, StructureMap, etc and be able to understand what they're doing
Are you trying to build a IoC container why not use one of the available ones like Spring.NET, StructureMap or Unity Application Block? Here is a list of open-source IoC projects
Try reading Introduction to Unity Application Block and in ASP.NET StoreFront: Dependency Injection screencast you can see more about Dependency Injection concepts.
I normally use StructureMap - mostly because I'm familiar with the syntax. I've also heard good things about autofac and I'm looking forward to trying out Ninject when it hits v2.
You might want to take a look at this answer where I talk about a basic usage of an IoC container (I always think things are easier to understand with a simple example) - that might help you to understand things a little more. Basically, the IoC container helps you to build objects with all the dependencies satisfied, and allows you to change your dependencies with minimal configuration code.
I've used StructureMap quite a bit. The rest of your question is pretty loaded. I'll try to explain the concept in an example.
Suppose you created a website that will accept payments through PayPal. PayPal is now a dependency. But you don't want to code against a specific PayPal provider.
Instead, you would create and code against an interface like this:
All your PayPal code would reside in a class that implements the methods of your interface -
PayPalPaymentProcessor
, for example.Now you have an object that you will actually use to process the payments. This could be a Controller (ASP.NET-MVC, ViewModel-WPF) or just a class as shown here:
This is where an IoC container comes in. Instead of you calling the constructor manually, you would let an IoC container inject the dependency:
This piece of code tells StructureMap "Anytime you see a constructor that needs an
IPaymentProcessor
, return a newPayPalPaymentProcessor
".All this mapping is separate from your implementation code and you could swap out these at a later point with little refactoring needed. There is a lot more to IoC containers, but that the basic concept. You can automate the injection of constructors to avoid the calls directly to
ObjectFactory
as well.Hope this helps!
Check out Spring IoC (.net) a java/.net container. The documentation is quite good introduction.
In a brief: You can think about IoC as an architecture that encourage: objects composition and programming to an interface.
This gives you the following:
the ability to unit test your code easily (you can easily test your objects in isolation by mocking up all its dependencies).
An extremely advance configuration (because your program with IoC is just bunch of objects and an configuration that glues the objects together).
The ability to extend or modify byte compiled application (this is true for Java I'm not sure if it is true for .net).