In simple terms and/or in high-level pseudo-code, how does a DI container work and how is it used?
相关问题
- Wrapping Angular components which has Parent/Child
- How to I access the DbContext of EF core from anot
- NserviceBus property injection
- How can I get all singleton instances from a Guice
- Unity and delegate
相关文章
- TinyIoC: Register multiple interfaces on a single
- Restlet server resource with constructor parameter
- Injecting an IEnumerable into a constructor with a
- ASP.NET Core DbContext injection
- DI container, factory, or new for ephemeral object
- How does @Inject in Scala work
- When to use interfaces, and when to use higher ord
- @EntityListeners Injection + jUnit Testing
At its core a DI Container creates objects based on mappings between interfaces and concrete types.
This will allow you to request an abstract type from the container:
This requires that you have previously configured the container to map from IFoo to a concrete class that implements IFoo (for example Foo).
This in itself would not be particularly impressive, but DI Containers do more:
Once you start trying to manually manage composition and lifetimes you should start appreciating the services provided by a DI Container :)
Many DI Containers can do much more than the above, but those are the core services. Most containers offer options for configuring via either code or XML.
When it comes to proper usage of containers, Krzysztof Kozmic just published a good overview.
While the above is a massive understatement that's the easy way of thinking about them. Given the collection, if you ask for the same instance of an class - the DI container will decide whether to give you a cached version or a new one, or so on.
Their usage makes it easier and cleaner when it comes to wiring up dependencies. Imagine you have the following pseudo classes.
Constructing a house is a pain without a DI container, you would need to create an instance of a bedroom, followed by an instance of a kitchen. If those objects had dependencies too, you would need to wire them up. In turn, you can spend many lines of code just wiring up objects. Only then could you create a valid house. Using a DI/IOC (Inversion of Control) container you say you want a house object, the DI container will recursively create each of its dependencies and return you a house.
Without DI/IOC Container:
With DI/IOC Container:
At the end of the day they make code easy to follow, easier to write and shift the responsibility of wiring objects together away from the problem at hand.
You configure a DI container so it knows about your interfaces and types - how each interface maps to a type.
When you call
Resolve
on it, it looks at the mapping and returns the mapped object you have requested.Some DI containers use conventions over configuration, where for example, if you define an interface
ISomething
, it will look for a concreteSomething
type to instantiate and return.