Dependency Injection is certainly one of the most important concepts when trying to write testable code. But while Java and C# have garbage collection, Delphi has not and normally, object disposal is managed using the ownership-principle (the one who creates the object destroys it). This is nicely supported by the try..finally construct
Obj := TObject.Create;
try
...
finally
Obj.Free;
end;
Now what if one uses dependency injection:
constructor TFileLister.Create(FileSystem: TFileSystem);
Who should now be responsible for destroying the FileSystem object? Does the ownership-principle still work here?
I know that interfaces are a solution to this problem (thanks to the fact that they are reference-counted). But what if there are no interfaces (say in some legacy code)? What other approaches or best practices are there to handle memory management when using dependency injection?