I have started refactoring a small application to use a small DI container instead of having
$registry::getstuff(); calls in my classes I inject them in a container.
This has raised 2 questions,
Q1 -> I extend Pimple DI class and create a container with dependencies specific to each object that will need DI. I then feed the object the whole shebang, and decrontruct it it in the constructor assigning the DI’s objects to the class properties of the object I’m building.
Should I be separating the object in the new object() call? I just found it easier like this but seeing I’m a one man team right now I just want to confirm I have proper methodology.
Q2 -> I find the $registry object I was passing around all over will be uneeded if I do this on a few of the main classes, is this a normal result of using DI, no more registry? I may have a singleton or two injected in the container but it looks as that is all I will need and even those could easily be eliminitated since the DI has a share() property that returns the same instance of the object, effectively removing the need for singletons. Is this the way to rid an app of needing registry/singletons, because if it is it’s darn easy like this.
Q2 :
If you were passing around all over your
$registryobject…. then your Registry was not really what is called a Registry (as Fowler described it).A Registry is more or less a global object (a “well-known”) with get/set methods.
In PHP, two common prototypes for the implementations of a Registry would be
As a singleton
With static methods all over the place
Passing your Registry all over the place makes it, well, just an object: a container with no greater purpose than providing references to other objects.
Your DI container (using Pimple as you suggested in your OP) is kind of a Registry itself: It IS well known and enables you to get components from anywhere.
So yes, we can say that your DI container will remove the requirement and necessity of a registry by performing the same functionality.
BUT (there’s always a but)
If you’re using your DI Container to replace your Registry, this is probably wrong.
eg:
Why is this bad? Because your code is not less dependent than before, it still depends on a component (either a registry or a container) which does not provides a clear goal (we may think of interface here)
The Registry-pattern be useful in some cases because it’s a simple and fairly inexpensive way to define components or data (e.g. global configuration).
A way to refactor the above example without relying on DI by removing the dependency would be:
Of course, this may not be possible if the client code isn’t aware of the connection, which is probably the reason why you probably ended using a Registry in the first place.
Now DI comes into play
Using DI makes our life better because it will handle the dependency and enables us to access the dependency in a ready-to-use state.
Somewhere in your code, you’ll configure your components:
Now you have everything you need to refactor your code:
The “client” code should be as isolated as possible. The client should be responsible for and inject its own dependencies (via
set-methods). The client should not be responsible for handling its dependencies’s dependencies.The DI container will configure GoodClientCode with the Service that has already been properly configured with its Connection
As for the Singleton aspect, yes, it will enable you to get rid of them.
Hope this helps