Short best practice question: If an object A is injected into another object B, should then object B implement IDisposable and dispose A when B is disposed?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would generally say no; the nature of dependency injection means that the injected object does not know much about the lifecycle of what it was injected with; to some extent, this is the definition of injection. As such, I do not think the injected object should dispose whatever it was injected with; the injecting code should take the responsibility for knowing the complete lifecycle of all the objects it is injecting, and should be able to properly dispose of them when all operations on them are complete, and not before.