简体   繁体   English

正确的模式和依赖注入

[英]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 ? ). 一种选择是验证器将扩展模型,但是我认为这是不好的做法(因为整个依赖项注入概念都是kaboom ...还是不是?)。 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 . 这是一个完全有效的结构,甚至符合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. DI的经验法则是,如果Validator需要操作Settings ,则应将其传递给构造函数。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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