Who created the instance? This is usually the owner and should be responsible for deleting the instance.
Most likely, you have an “external” class that created C, and then passed it directly or indirectly to A and B. It is probably a natural candidate who is responsible for and must manage the C life cycle.
[Edit: in response to OP comment] Sounds like maybe you should take a look at the design here. Does this indicate the need for a refactor?
You have a class C that needs to be disposed of, which is used by both A and B; if you have a class that has a common responsibility for sorting C through A and B, and not for creating C from a DI container? Or C is really more singleton. Does it really need to be disposed of?
I assume that all I am saying is that it looks like it can point to a design that needs a little change; have a different look with a critical eye.
Rob levine
source share