What does the binding
mean in the context of the DI pattern?
I am going through the IoC
tutorial over here. And I come across the following excerpt:
Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.
I can not understand what binding is referred to here?
I guess that the binding referred to here is about setting the client class field. But in such a case we are not moving such a binding with the DI. Because, there are three ways to use the DI: constructor, property (setter) and method. And all of those ways do not move the binding (the assignment of the client class field) outside of the class, all of them happen in the scope of the client class. So, I am confused here.
UPDATE After a few answers were provided to the question I come to a conclusion that there are two possible definitions of what a binding is.
1 Binding means mapping an interface which is used inside the dependent classes to an actual object type.
2 Binding means passing an actual type as an argument for an interface parameter into the dependent classes.
What is the correct definition 1 or 2? Or does the definition of the binding depend on the context where the binding is mentioned?
What binding means?
It means that our dependency classes will automatically be resolved by the IoC container.
We have IRepository interface
and the Repository class
.
If we bind those two together. Every time we request the IRepository
, our container automatically provides us with the Repository
class.
This makes it very easy to alter the implementation of the Repository class. Since we are not immediately dependent on it. We never say new Repository class. We only provide the interface and everything else is taken care of by the container. You could for example say the IRepository is bound to the DatabaseRepository class. With only changing your container bindings and nothing else.
This all happens due to the binding in the IoC container. This provides a lot of flexibility inside your application.
Also what we can do with the binding command in our container can provide lifetimes for our objects usually those three(Singleton, PerInstance, Scoped).