了解如何注入对象依赖项 [英] Understanding how to inject object dependencies

查看:58
本文介绍了了解如何注入对象依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,该对象可以在方法中加载另一个对象的实例.

I have an object that loads an instance of another object in a method.

$survey   = new survey;

$question = new question($survey);

$question->load($question_id);

class question  {

public function __construct(&$survey)
{   
    $this->survey = $survey;
}   

public function load ($id)
{   
    // now a question is loaded
    // want to load the survey that this question is in
    $this->survey->load($this->get('survey_id')); // ** survey_id is a field in the questions table
    // now $this->survey object has all the info about the survey this question is in
}   

private function get($name)
{   
    // returns $name, if isset() from array that load() sets
}    }

但是,这让我很不高兴,因为似乎$ survey应该已经进入$ question已经是一个完整的对象了.但是,如果在进入对象之前不知道要加载哪个survey_id,该怎么办?我正在改造一个网站,这个问题最复杂的部分是乱扔.TIA-汉斯.

This is frying my brain, though, because it seems like $survey should come into $question already being a complete object. But, how do I do that, if I don't know what survey_id to load until I'm in the object? I am revamping a site and the most complex part is littered with this issue. TIA - Hans.

推荐答案

我刚刚注意到您提到您在现有代码中遇到此问题.我在下面所说的内容可能仍然适用于解决您的问题,但实际上可能并没有直接的建设性用途.:)不幸的是,我们需要查看更多代码来帮助您解决问题,因为到目前为止您向我们展示的内容很奇怪.

I just noticed you mentioned that you're encountering this in existing code. What I've said below may still apply to solving your problem, but it might not actually be of immediate constructive use. :) Unfortunately we'd need to see a lot more code to help you with your problem, as what you've shown us so far is bizarre.

您似乎已经从内到外构造了类.

You seem to have constructed your classes inside out.

调查通常被定义为问题集.

Surveys are normally defined as sets of questions.

因此,您的Survey类不应该包含并引用问题,而不是相反吗?

Therefore, shouldn't your Survey class contain and reference Questions, not the other way around?

您可以将他们的关系存储在链接表中,这将允许Surveys查找他们的问题,同时还允许Questions查找他们所在的Surveys.在每个类中创建访问器方法,以在链接表并返回对象的ID或对象本身.这种方法可以使对象之间的紧密度降低.

You can store their relationship in a link table, which will allow Surveys to look up their Questions, while also allowing Questions to look up which Surveys they're in. Create accessor methods in each class to look up the dependency in the link table and return either IDs for the objects, or objects themselves. This method makes the objects less closely tied together.

(将其扩展到用户回答问题的过程中,事情会变得更加有趣.如果这样做,我将为每个用户的响应(每个用户获得一个响应)创建一个类,其中包含答案.响应引用了调查,每个答案都引用一个问题.)

(Extending this to users answering questions, things get more interesting. If I was doing this, I'd create a class for each user's Response (each user gets one Response), which contains Answers. The Response references the Survey, the Answers each reference a Question.)

这可能不是依赖注入适用的情况.当要传递的对象的实际类别可能改变,或者当外力可以控制可以传递哪些对象时,DI最常使用.您是否曾经希望Surveys包含不是问题的内容?还是那些问题将永远存在于不是调查的内容中​​?还是与调查或问题相关的某种力量会操纵一个问题?如果您对这些回答否",那么DI可能不是您想要的.

This probably isn't a case where dependency injection is applicable. DI is most often used when the actual class of the object being passed may change, or when an outside force can control what object can be passed in. Do you ever expect that Surveys will contain things that aren't Questions? Or that Questions will ever exist inside something that isn't a Survey? Or that a force outside of either something related to Surveys or Questions will manipulate a Question? If you answered "no" to these, then DI is probably not what you want here.

这篇关于了解如何注入对象依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆