简体   繁体   中英

Right pattern and dependency injection

I have a problem with deciding the right way or writing a module. I THINK I know the theory behind the dependency injection and what are it's advantages but perhaps I am mixing something or do not have a very clear image.

A rather simplified example would be :

I have a model for my object and here I inject a validator class that handles the various validations :

// index.php
// ......
$model = new Model();
$model->setValidator(new Validator());
$model->getValidator()->exampleValidateCall();
// ......

// validator.php
class Validator implements ValidatorInterface {

   //.....

   public function exampleValidateCall() {
      // code goes here
   }
}

My problem here is that I need for instance access to the settings entity witch defines the behaviour of the model. Because the settings define the model, I do not think that I should pass theese settings inside the validator.

One option would be that the validator will extend the model, but I think that would be bad practice ( because the whole dependency injection concept goes kaboom... or not ? ). I could do something like :

$model->setValidator(new Validator($model->getSettings()));

but this looks even more idiotic from my point of view.

One better solution, again from my point of view, would be to pass a new object to the validator constructor

$model->setValidator(new Validator(new Settings()));

because in reality settings do not have dependency with the model, but that seems to complicate a little too much. And also the settings entity is constructed inside the model because it defies some of the behaviours.

What would be the best practice in writing these objects / dependencies ?

This would not fit in a comment, so I'm posting as an answer.

 $model->setValidator(new Validator($model->getSettings())); 

but this looks even more idiotic from my point of view.

This is not idiotic whatsoever. It is a completely valid construction and even respectful to the Law of Demeter .

The main question here is whether it makes sense to store your settings within your model or it should be a separete a different object as you pointed:

 $model->setValidator(new Validator(new Settings())); 

If you're building a generic validator with which you can parametrize business rules, I think it's valid to have theese settings lying in your model.

Otherwise, If this validation is entity-specific, I think this would be better as a different structure.

Rule of thumb for DI is that if the Validator requires a Settings to operate then it should be passed into the constructor. If it can operate without the setting then you can use add the dependency post instantiation. Another option in your case might be to pass it into the method that is will be used in.

$model->setValidator(new Validator());
$model->validator($thingToValidate, $model->getValidationSettings());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM