About this (enter the dependency)
private readonly ICustomerService _customerService; public Billing(ICustomerService customerService) { _customerService = customerService; }
compared to this (create dependency)
private readonly ICustomerService _customerService; public Billing() { _customerService = new CustomerService(); }
The last sample, so to speak, is bad because ... it violates the DI ... of course, nothing is entered ... but what if the DI did not exist, which is so bad that the CustomerService is created manually from the Billing class? I see no practical advantage regarding the exchangeability of the service interface.
I ask for a practical example with the source code, whether it could be a unit test or show a practical solution why it is a much more loose connection.
Anyone who wants to show their DI muscles enough and why does he have a practical right to exist and use?
UPDATE
So, people do not read everything, I will write here my short experience:
DI as a sample has practical applications. To follow the DI without inserting all the services manually (a bad DI DI tool, so they say ...) use a DI infrastructure like LightCore / Unity, but be sure to use the right tool for the job. This is what I did not do ;-) Developing the mvvm / wpf application I have other requirements that the LightCore / Unity tool could not support, they were even a barrier. My decisions were to use MEFEDMVVM, which I am happy with. Now my services are automatically entered at runtime not at startup time .:-)
source share