这时,我很讨厌在我的模块中像下面一样编写类似的构造函数。
public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    /* ... */
    \Foo\Bar\Model\Baz $baz,
    /* ... */
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
) {
    $this->registry = $registry;
    /* ... */
    $this->baz = $baz;
    /* ... */
    /* some awesome stuff */
}在许多很多情况下,我在模块中都需要相同类的实例。
所以我在问自己,是否可以使用一个或两个提供必要类的中央帮助程序类而不是在每个构造函数中都定义它们,这是一种可接受的方法。
这意味着这样的模式:
助手类
namespace Foo\Bar\Helper
class Main
{
    protected $baz;
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        /* ... */
        \Foo\Bar\Model\Baz $baz,
        /* ... */
    ) {
        $this->registry = $registry;
        /* ... */
        $this->baz = $baz;
        /* ... */
        /* some awesome stuff */
    }
    public function getBazInstance()
    {
        return $this->baz;
    }
}较短的构造函数
public function __construct(
    \Foo\Bar\Helper\Main $mainHelper,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
) {
    $this->mainHelper = $mainHelper;
    /* some awesome stuff */
}在这一点上,我不确定将来是否需要应对这种结构所带来的巨大不利影响。这是减少DI定义数量的可接受方法吗?