So I read the Dependency Injection in .NET book not long ago. Trying to use DIC now. However, I have something I don’t quite understand. Two examples:
1. if I have some old code or a 3rd party dll that uses a singleton static method like this:
var objTool = ThirdPartyTool.GetInstance();
objTool.DoStuff();
...
2. if I have some code in a method creating a temp object:
var tempOrder = new Order();
tempOrder.Total = strArray.[0];
tempOrder.ItemId = strArray.[1];
tempOrder.ShipAddress = strArray[2];
if(Customer.HasConfirmedOrder) { Customer.Order = tempOrder; }
...
In both cases, before I enter the DIC world, I know those objects will naturally be GC collected when they are out of scope, or if there is a Dispose() to call, I am responsiable to call it somewhere after I use the object.
How does DIC treat them? (I am running Ninject on a .NET 4 MVC 3 project, but I guess other languages/projects and DICs have pretty much the same structure)
If an object is not managed by the DI framework, as the example you provided, it is your responsibility to manage its lifetime and if necessary call Dispose on it (especially if this object implements IDisposable)
So to answer your question:
It doesn’t treat them at all. The DI framework doesn’t even know of their existence.
The DI framework is responsible for managing objects that you explicitly registered with it.