How broad is the definition of “dependency injection”?

What is the “essence” of dependency injection? Is it the idea of dynamically swapping out/in core logical/structural aspects of a program at runtime?

Traditionally, this is done in code via some DI container. For example, using Structure Map in C#:


However, I’ve written several programs which inject code via configuration, in XML. Something like:

<cacheServiceProvider use="My.Namespace.InMemoryCache, MyAssembly"/>

That config is iterated at startup, and the various classes are instantiated for use throughout the program.

Is it fair to say that this is a form of DI? “Dependency Injection by Configuration”? Or does DI require configuration in code and the use of some container system?

Are their other philosophical or conventional aspects required to say something is “dependency injected,” or am I completely misunderstanding this?


The essence is simply that given an object, you pass its dependencies to it rather than let it instantiate them itself.

How you achieve that, by containers, configuration, or any other means, is up to you.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *