How do you reconcile IDisposable and IoC?
Posted
by Mr. Putty
on Stack Overflow
See other posts from Stack Overflow
or by Mr. Putty
Published on 2009-06-12T16:48:43Z
Indexed on
2010/03/12
21:07 UTC
Read the original article
Hit count: 403
I'm finally wrapping my head around IoC and DI in C#, and am struggling with some of the edges. I'm using the Unity container, but I think this question applies more broadly.
Using an IoC container to dispense instances that implement IDisposable freaks me out! How are you supposed to know if you should Dispose()? The instance might have been created just for you (and therefor you should Dispose() it), or it could be an instance whose lifetime is managed elsewhere (and therefor you'd better not). Nothing in the code tells you, and in fact this could change based on configuration!!! This seems deadly to me.
Can any IoC experts out there describe good ways to handle this ambiguity?
© Stack Overflow or respective owner