It is. So is the service locator. And they both have been there before I joined the project and have been used by people.
If they weren't I would have removed them by now to make clear that this is not the supposed way of doing things.
Still my opinions and advice remain.
And no matter the place of the registration the result is the same for using the container as a global factory.
But without forcing dependencies on your implementation units if you are using separate units for registration.
If you have a unit like this:
unit SomeService;
interface
implementation
uses
Spring.Container,
SomeServiceIntf;
type
TSomeService = class(TInterfacedObject, ISomeService)
public
procedure DoSomething;
end;
procedure TSomeService.DoSomething;
begin
end;
initialization
GlobalContainer.RegisterType<ISomeService, TSomeService>;
end.
as has been suggested by people in the past you are facing two problems here:
The class is not accessible directly in any way which makes testing it directly impossible.
I kept it simple for this example but it might require some dependencies on the constructor which would be injected by the container.
So you would need a container only for testing purpose which is also not recommended.
Test code should not require a container at all
Even if the class would have been declared in the interface part this unit would require the Spring.Container unit.
This would force its usage into any project that might use SomeService.
It will cause problems especially when you are using different modules and not one monolithic application
as every module would need to know that one global instance which you can solve different ways which would not be required at all
if you would not have put the Spring.Container unit into that uses.