简体   繁体   English

在Laravel中,使用App :: make('')而不是构造函数注入的任何缺点?

[英]In Laravel, any downside to using App::make('') rather than constructor injection?

Normally I would just inject dependancies via the constructor, but it gets to be very verbose when the parent class has dependancies and have to pass them through all the children. 通常我会通过构造函数注入依赖项,但是当父类具有依赖项并且必须将它们传递给所有子项时,它会变得非常冗长。

The alternative is to use $this->dependancy = App::make('Dependancy') in the parent class alone. 另一种方法是单独在父类中使用$this->dependancy = App::make('Dependancy') Then both the parent and child constructors can be empty. 然后父和子构造函数都可以为空。 Is there any downside to doing it this way? 这样做是否有任何不利之处?

There is a downside to your approach, doing what you propose will make your application less testable. 您的方法有一个缺点,做您的建议会使您的应用程序不易测试。

What I mean is that if you try to write a unit test for the parent class you will no longer be testing that parent class in isolation. 我的意思是,如果您尝试为父类编写单元测试,您将不再孤立地测试该父类。 Your test now also depends on the results from the dependency declared inside the parent class. 您的测试现在还取决于父类中声明的依赖项的结果。

If you pass this dependency in through constructor injection (or any type of injection) you have control over the dependency and can mock/stub the output from it and better test your parent class in isolation. 如果通过构造函数注入(或任何类型的注入)传递此依赖项,则可以控制依赖项,并可以模拟/存储它的输出,并更好地单独测试父类。

First of all there is no downside to use App::make('...') or app('...') but if you use type hinting in the constructor then you don't need to inject the dependencies manually. 首先,使用App::make('...')app('...')没有任何缺点,但如果在构造函数中使用类型提示,则不需要手动注入依赖项。 For example, if you have something like this: 例如,如果你有这样的东西:

class SomeController extends BaseController {

    protected $otherClass = null;

    public function __construct(SomeOtherClass $otherClass)
    {
        $this->otherClass = $otherClass;
    }
}

Now, if you use SomeController class then Laravel will automatically inject the SomeOtherClass into the SomeController class and if your SomeOtherClass has it's own dependencies then Laravel will inject those as well if they are type hinted. 现在,如果你使用SomeController类,那么Laravel会自动将SomeOtherClass注入到SomeController类中,如果你的SomeOtherClass有自己的依赖关系,那么如果它们是类型提示, Laravel也会注入它们。 So, you may use Dependency Injection in constructor over App::make(...)/app(...) and it would be better if you use Interface to type hint instead of concrete class . 因此,您可以在构造函数中使用Dependency Injection而不是App::make(...)/app(...) ,如果您使用Interface来键入提示而不是concrete class ,那会更好。 It's been said, program over interface not implementation (concrete class). 有人说,程序超过interface而不是implementation (具体类)。

Generally speaking, there is one thing in dependency injection technique and it is that, while you compose a class with other objects, then it may look complex. 一般来说,依赖注入技术中有一点是,当你用其他对象组成一个class时,它可能看起来很复杂。 Actually, dependency injection is a way to compose a class by mixing other objects at runtime through constructor method (composition over inheriting) but sometimes it may look complex to find out the object's relationship to each other if there are so many dependencies. 实际上,依赖注入是一种通过constructor方法(继承的组合)在runtime混合其他对象来组成类的一种方法,但是如果存在如此多的依赖关系,有时可能看起来很复杂以找出对象之间的关系。 Ultimately it's a good design pattern which gives us a way to decouple the objects from each other and you need to chose wisely when to use it. 最终,这是一个很好的设计模式,它为我们提供了一种将物体彼此分离的方法,您需要明智地选择何时使用它。

Update: Actually, in Laravel when you type hint a dependency in the constructor method, the framework automatically injects the dependencies by reading the type of it's dependent object and behind the scene, Laravel uses the App::make(...) method. 更新:实际上,在Laravel当您在构造函数方法中键入提示dependency Laravel时, framework通过读取依赖对象的type并在场景后面自动注入依赖项, Laravel使用App::make(...)方法。 So, if you don't use dependency injection then you just need to manually use the App::make(...) , that's all. 所以,如果你不使用依赖注入,那么你只需要手动使用App::make(...) ,就是这样。 In both cases, the App::make() is being used, either automatically by the framework or by the developer. 在这两种情况下, App::make()都是由框架或开发人员自动使用的。 So, use App::make() without any hesitation, it's safe and a better approach to decouple the dependencies from each other. 所以,毫不犹豫地使用App::make() ,它是安全的,并且是一种更好的方法来将依赖关系相互分离。

In other words, if you type hint the dependencies in the constructor method then Laravel will use App::make(...) to automatically inject them but if you don't type hint then You need to manually inject them and in this case you'll use App::make() instead of framework, that's it. 换句话说,如果你在构造函数方法中键入提示依赖项,那么Laravel将使用App::make(...)自动注入它们,但是如果你不输入提示那么你需要手动注入它们,在这种情况下你将使用App::make()而不是框架,就是这样。 For automatic resolving of dependencies, you need to type hint your dependencies but if you manually inject your dependencies then you don't need to type hint them and without type hint, Laravel can't inject any dependencies automatically and it makes sense. 要自动解析依赖项,您需要键入提示您的依赖项,但如果您手动注入依赖项,那么您不需要键入提示它们而没有类型提示, Laravel不能自动注入任何依赖项,这是有道理的。 Btw, I'm not thinking about testing tho. 顺便说一句,我不是在考虑testing

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

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