One of the main advantages of using DI/IOC is to reduce coupling between different classes.
When you use the approach you have to configure hierarchy of objects, which express the dependencies you took out of their code. This can turn into a lot of code. Even if you use the code part of a DI framework, it can greatly reduce the amount of configuration by letting you configure in a more expressive way:
- Whenever an ILogger is needed, use a
FileLogger.
- You can go even further with
conventions, whenever an
I[Name]Controller is requested
replace it with [Name]Controller.
- You can have it manage singleton
objects for you. So your code is
the same as with any other
dependency, but the DI makes sure
you get the same instance all the
time
A couple structuremap configurations I use:
ForRequestedType<ILogger>().TheDefaultIsConcreteType<NLogLogger>();
//the following injects any property that has a type
//that implements IController. (overcomes a regular asp.net limitation with DI)
SetAllProperties(
p => p.TypeMatches(t => t.IsConcreteAndAssignableTo(typeof(IController)))
);