php - Dependency Injection / LoD -
I understand the basic principles of dependency injection, but how to handle early classes within the methods of other classes, Wants some advice; Does this dependency go against the injection pattern?
In addition, I would like to write the object (class) in DB (object form in Mongo) and do not want it to be framed with other dependencies.
To illustrate better what I'm saying here is an example:
We say that we have a user class that is injected with a server class -
class user {public $ user_id public $ user_name; Public function __ composition (server $ server) {$ this- & gt; Server = $ server; } Delete Public Function () {// ... Delete user code // Send mail to user mailer = new mailer ($ this-> server); $ Mailer- & gt; Sendmail (); }}
Two things about this
- This means that the user class is now bloated because it also has a server class
- Does it break demeter's law? In addition to passing the user class to the mailer class, the server class is not required.
I understand that in a way around it's injecting the removal of the mailer class Function, when called from the external controller, which means that the user class is not injected with the server class:
$ server = new server (); $ Mailer = new mailer ($ server); $ User = new user (); $ User- & gt; ($ Mailer) delete; Class user {public $ user_id public $ user_name; Public Function __construct () {// Other Code} Deleting Public Functions (Mailer $ Mailer) {// ... Delete User Code // Send Mail To Mailer-> Mailer-> Sendmail (); }}
But of course it means that you have to know every class needed for the methods in the class, if it becomes darker than some nesting levels, then surely this It would be difficult to keep an eye on.
What happens if user-> Delete is a private method? You will not be able to call it out to the controller to pass mailer object in the first place.
So my question is really the best way to go about this?
For me it is a smell of dependence on an object, just you can create a different object Are there.
Instead of taking a dependency on the object you want to build instead. So in your case just pass a mailer with the constructor of your user
, then you do not need to create a mailer and you do not have to worry about the server.
Here your user
has a dependency on the mailer (for mailing) and so this is something that should be injected.
You should make objects only new examples of stones Objects / Data Holders / DTO Anything that provides any functionality (i.e. services), there should be injections in those items which require the use of the functionality it occurs. I do not PHP, but I think your user class should look like this:
< Code> class user {public $ user_id public $ user_name; Public function __ composition (mailer $ mailer) {$ this- & gt; Mailer = $ Mailer; } Delete Public Function () {// ... Delete user code // Send mail to user $ this- & gt; Mailer- & gt; Sendmail (); }}
For injection through the constructor versus entering this method, it comes down to whether the user expectation for the provider of your user
I do not like it when it is appropriate or not a mailer when the user wants to delete, so I will pass it through the constructor.
Comments
Post a Comment