Probably a Unity beginner's question: when using Unity, would you still need to implement Dispose methods on the objects you have injected? Or is even this not needed (so, done automatically by Unity)? This is in the context of a web application.
Implementing IDisposable
has nothing to do with Unity. You should implement the interface when your type is using unmanaged resources likes Files, that cannot be garbage collected by the CLR.
Unity can manage the lifetime of your types and instances. For this case Unity provides diffrent types of LifeTimeManager to control the lifetime of your instances.
Unity does only respect the IDisposable
interface when you register them using the ContainerControlledLifetimeManager
or the HierarchicalLifetimeManager
. That meens when you dispose the Unity-Container it will also call Dispose
on all instances implementing the IDisposable
interface registered by the named LifetimeManager above.
When you register types that implement the IDisposable
interface using the TransientLifetimeManager
(you get a new instances each type you call Resolve on the container), it is up to you to call Dispose
on the instance.