Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should injected properties be disposed?

Tags:

c#

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?

like image 248
Carl Hörberg Avatar asked May 20 '09 21:05

Carl Hörberg


1 Answers

If you're referring to the dependency injection pattern, I think it depends on the knowledge in Object B about the implementation of Object A. The reason for doing dependency injection like this is usually because you do not know on beforehand how Object A will be implemented and whether it requires IDisposable. The only thing you know is the interface.

Adding this behavior would result in tighter coupling between the two classes, IMO.

like image 193
Roy Avatar answered Sep 21 '22 23:09

Roy