Reputation: 2172
I've heard people talk about child containers in IoC, when I google it I end back here at the questions that got me wondering what they were in the first place.
What is an IoC child container and how are they used?
Upvotes: 3
Views: 2285
Reputation: 31867
There's no single definition for the term - what is implemented as "child containers" in one container often has totally different logical functionality in another.
In Autofac and in some similar containers, a "child container" (since v2, just a "lifetime scope") is a way to create a related set of components with a lifetime shorter than the "parent" container, and thus with dependencies resolved only from child-to-parent and never the other way around.
Other containers have different implementations, some allowing dependency resolution to go both ways (at least in my understanding.) The particular container you're working with is an important extra piece of info in getting this question answered more authoritatively.
Hope this helps!
Upvotes: 9
Reputation: 9676
IoC - or Inversion of Control - is a way to let your classes depend on services and other classes without actually knowing how to instansiate them and only knowing how to use them. Consider the following example:
public class MyService
{
public void DoSomething()
{
Logger l = new Logger(); // let's assume this class exists, and it logs stuff
l.Info("Some info logmessage");
}
}
and here we'll call that service:
public class MyProgram {
public MyProgram()
{
MyService myService = new MyService();
myService.DoSomething();
}
}
Now, with dependency injection or IoC, we could to invert the responsibility of logging. In all simplicity, this means that MyService
shouldn't really know how to instanciate the logger, but it should know how to use it.
So let's say the Logger
class implements an interface called ILog
which contains only the method void Info(string s)
We could then refactor our MyService
class to this:
public class MyService
{
ILog _logger;
public MyService(ILog logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.Info("Some info logmessage");
}
}
This would then mean we would have to refactor MyProgram
as well:
public class MyProgram {
public MyProgram()
{
ILog logger = new Logger(); // instanciate it here instead.
MyService myService = new MyService(logger);
myService.DoSomething();
}
}
This is basically Inversion of Control, but when we start talking about Dependency Injection, this can all be automated. Let's say for instance we want to use Ninject for injection. I'm not gonna write out a code example here, but the basis is that Ninject will essensially be setup to know that when some class requests the ILog
interface , we should return the Logger
class.
This enables us to easily change what kind of implementation we want to use for different interfaces - also increases the testability.
I'll recommend this short free video at tekpub that walks you through an example of IoC and Dependency injection using Ninject.
http://www.tekpub.com/view/concepts/1
Hope this helps!
Upvotes: 2