Can someone explain dependency injection with a basic .NET example and provide a few links to .NET resources to extend on the subject?
This is not a duplicate of What is dependency injection? because I am asking about specific .NET examples and resources.
Here’s a common example. You need to log in your application. But, at design time, you’re not sure if the client wants to log to a database, files, or the event log.
So, you want to use DI to defer that choice to one that can be configured by the client.
This is some pseudocode (roughly based on Unity):
You create a logging interface:
then use this interface in your classes
inject those dependencies at runtime
and the instance is configured in app.config:
Now if you want to change the type of logger, you just go into the configuration and specify another type.