简体   繁体   English

如果某个类正在从数据库中读取并解释结果,那么该如何将其松散地耦合到应用程序?

[英]How can a class be loosely coupled to an application if it is reading from a database and interpreting the results?

As a PHP dev, most of my projects are database-driven. 作为一个PHP开发人员,我的大多数项目都是数据库驱动的。 I'm a fond lover of OOP & while new to the object world, I attempt to follow best practices to improve on my abilities. 我是OOP的挚爱者,虽然刚接触对象世界,但我尝试遵循最佳实践来提高自己的能力。

My burning question 我的燃眉之急

If I have an object that requires information from my database, how is that object loosely coupled because it is heavily reliant on the returning results of the database? 如果我有一个需要从数据库获取信息的对象,那么该对象是如何松散耦合的,因为它严重依赖于数据库的返回结果?

Code example: 代码示例:

<?php

class Test {

protected $repository;

public function __construct(Repository $repository)
{
    $this->repository = $repository;
}

public function process()
{
    $results = $this->repository->where('id', 1)->get();

    ( ! empty($results) ) ?: ''; // do something bad.

    foreach ($results as $result) {

        // This is my problem, my class now knows it needs an array key of name to be available.
        // Is this bad? 
        echo $result['name'];

    }
}

} }

Here, my class is expecting an array key of name. 在这里,我的班级期望使用名称的数组键。 This just feels wrong to me, because now my class is tightly coupled to the result set... 我觉得这很不对劲,因为现在我的课已经与结果集紧密联系在一起了...

Layers 图层

I have been doing a lot of reading on application layers, such as the domain layer, service layer, transaction scripts, application layer etc. 我已经在应用程序层上进行了大量阅读,例如域层,服务层,事务脚本,应用程序层等。

Even equipped with this knowledge I cannot see a solution to my problem. 即使具备了这些知识,我也看不到解决问题的方法。

To Finalise 完成

It would be great to hear your thoughts on whether I'm driving myself mad for no reason. 很高兴听到您对我是否无缘无故地发疯的想法。

It would also be great to see some code examples or further reading materials, books etc links. 看到一些代码示例或进一步的阅读材料,书籍等链接也将很不错。

I think you may be going too extreme with this. 我认为您可能对此太过极端了。 I wouldn't spend too much time trying to figure out how the repo can not know that you have a key called name. 我不会花太多时间试图弄清存储库如何不知道您有一个名为name的密钥。 Someone will have to depend on that somewhere in your code and I don't think your repo class is a bad place to put that. 有人将不得不依赖于您代码中的某个地方,我认为您的repo类不是放那个的坏地方。

In the end, your goal should just be to save you and the maintainers of this codebase time when implementing new features, business rules or fixing bugs. 最后,您的目标应该只是在实施新功能,业务规则或修复错误时节省您和该代码库的维护人员时间。 And in my experience you can do this by isolating the parts of your application that you can foresee being complex or changing a lot. 根据我的经验,您可以通过隔离应用程序的各个部分(可以预见其复杂性或大量更改)来实现此目的。

So I wouldn't worry about the repo knowing the name of a key in the result set unless that key needed to be changed a lot, or if retrieving data from the result set was going to be a complex process. 因此,除非该键需要进行大量更改,或者从结果集中检索数据将是一个复杂的过程,否则我不必担心仓库会知道结果集中的键名。

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

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