Dependency Injection for end-of-lifecycle clean up
The benefits of using an inversion of control container for simplifying the task of initializing an object graph have been well covered. But the benefits of using an IoC container for simplifying object cleanup are not as well publicized, and is a feature that I've grown to really like taking advantage of.
The other night, at an Alt.Net Meetup on dependency injection/inversion of control I found myself being only person in the room enthusiastic about this.
The core of the problem is that objects that require cleanup, (ie implement IDisposable) can infect object hierarchies - any class with a has-a relationship to the IDisposable class really needs implement IDisposable itself. If you're not careful, you can easily end up with tens of classes that have the nearly-all-ceremony Dispose() methods that do nothing but cleanup their children.
The cleanest solution for this problem is to move the member level containment to method level and wrap the use of the disposable in a using statement. But when that isn't a viable option the next best option is to move to dependency injection - if your class didn't create a disposable object, it can't be responsible for cleaning it up.
Of course, at some point the objects do need to be disposed of, in the simplest way to do that is to use a container, such as autofac, which supports both nested containers and object cleanup.