Laravel 4:门面vs DI(何时使用) [英] Laravel 4: Facade vs DI (when to use)

查看:145
本文介绍了Laravel 4:门面vs DI(何时使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,立面被用作依赖注入的替代方法。如果我错了,请更正什么不清楚是什么时候应该使用一个或另一个。



每种方法的优点/缺点是什么?我应该如何确定何时使用一个或另一个?



最后,为什么不使用两者?我可以创建引用一个接口的立面。看来Sentry 2是这样写的。有没有最佳实践?

解决方案

FACADES



Facades 不是依赖注入的替代方法。



Laravel Facade是服务定位器模式的实现,创建一个干净漂亮的访问对象的方式:

  MyClass的:: doSomething的(); 

这是静态方法的PHP语法,但Laravel更改游戏并使其成为非静态在幕后,给您一个美丽,愉快和可测试的方式来编写您的应用程序。



依赖注入



依赖注入基本上是一种将参数传递给您的构造函数和方法,同时自动设置它们。

  class MyClass {

private $ property;

public function __construct(MyOtherClass $ property)
{
///这里可以使用Dependency Injection的魔法

$ this-> property = $ property

/// $ property已经是MyOtherClass的对象
}

}
pre>

更好的构造将是使用您的依赖注入构造函数中的接口:

 code> class MyClass {

private $ property;

public function __construct(MyInterface $ property)
{
///这里可以使用依赖注入的魔法

$ this-> property = $ property

/// $ property将接收一个实现MyInterface的具体类的对象
///这个类应该在其他地方在Laravel中定义,但这是一种也使
///您的应用程序易于维护,因为您可以交换您的界面的实现
///轻松
}

}

但请注意,在Laravel中,您可以以相同的方式注入类和接口。要注入界面,你只需要告诉它,这将是这样的:

  App :: bind('MyInterface',' MyOtherClass'); 

这将告诉Laravel,每次你的一个方法需要一个MyInterface的实例,它应该给它一个的MyOtherClass。



这里发生了什么,这个构造器有一个依赖: MyOtherClass ,这将是自动的由Laravel使用 IoC容器注入。因此,当您创建一个 MyClass 的实例时,Laravel会自动创建一个 MyOtherClass 的实例,并将其放在变量中 $ class



依赖注入只是开发人员创建的一个奇怪的行话,就像自动生成参数。



何时使用一个或另一个?



可以看到,他们是完全不同的事情,所以你不会需要在他们之间作出决定,但你必须决定在一个或多个应用程序的不同部分去哪里。



使用Facades 轻松地编写代码。例如:为应用程序模块创建软件包是一个很好的做法,因此,为这些软件包创建Facades也是使它们看起来像一个Laravel公共类并使用静态语法访问它们的一种方式。

$ b每次类需要使用数据或来自另一个类的处理时,
$ b

使用依赖注入。它将使您的代码可以测试,因为您将能够将这些依赖关系的模拟注入到您的课堂中,您还将执行单一责任原则(请查看 SOLID原则)。


My understanding is that a facade is used as an alternative to dependency injection. Please correct if I'm mistaken. What is not clear is when one should use one or the other.

What are the advantages/disadvantages of each approach? How should I determine when to use one or the other?

Lastly, why not use both? I can create a facade that references an interface. It seems Sentry 2 is written this way. Is there a best practice?

解决方案

FACADES

Facades are not an alternative to dependency injection.

Laravel Facade is an implementation of the Service Locator Pattern, creating a clean and beautiful way of accessing objects:

MyClass::doSomething();

This is the PHP syntax for a static methods, but Laravel changes the game and make them non-static behind the scenes, giving you a beautiful, enjoyable and testable way of writing your applications.

DEPENDENCY INJECTION

Dependency Injection is, basically, a way of passing parameters to your constructors and methods while automatically instatiating them.

class MyClass {

    private $property;

    public function __construct(MyOtherClass $property)
    {
        /// Here you can use the magic of Dependency Injection

        $this->property = $property

        /// $property already is an object of MyOtherClass
    }

}

A better construction of it would be using Interfaces on your Dependency Injected constructors:

class MyClass {

    private $property;

    public function __construct(MyInterface $property)
    {
        /// Here you can use the magic of Dependency Injection

        $this->property = $property

        /// $property will receive an object of a concrete class that implements MyInterface
        /// This class should be defined in Laravel elsewhere, but this is a way of also make 
        /// your application easy to maintain, because you can swap implementations of your interfaces
        /// easily
    }

}

But note that in Laravel you can inject classes and interfaces the same way. To inject interfaces you just have to tell it wich one will be this way:

App::bind('MyInterface', 'MyOtherClass');

This will tell Laravel that every time one of your methods needs an instance of MyInterface it should give it one of MyOtherClass.

What happens here is that this constuctor has a "dependency": MyOtherClass, which will be automatically injected by Laravel using the IoC container. So, when you create an instance of MyClass, Laravel automatically will create an instance of MyOtherClass and put it in the variable $class.

Dependency Injection is just an odd jargon developers created to do something as simple as "automatic generation of parameters".

WHEN TO USE ONE OR THE OTHER?

As you can see, they are completely different things, so you won't ever need to decide between them, but you will have to decide where go to with one or the other in different parts of your application.

Use Facades to ease the way you write your code. For example: it's a good practice to create packages for your application modules, so, to create Facades for those packages is also a way to make them seem like a Laravel public class and accessing them using the static syntax.

Use Dependency Injection every time your class needs to use data or processing from another class. It will make your code testable, because you will be able to "inject" a mock of those dependencies into your class and you will be also exercising the single responsibility principle (take a look at the SOLID principles).

这篇关于Laravel 4:门面vs DI(何时使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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