dongqiabei7682 2014-12-14 16:01
浏览 30

细长框架:Currying vs Dependency Injection

In some frameworks like Angular, you can inject services and controllers into one other like this

App.controller('exampleController', function($scope, ajaxService){

    ajaxService.getData().then(function(data) {

        //do something with the data

    });

});

This is called Dependency Injection according to Angular docs

You can do something like this in Slim Framwork too, like this

$app->get('/example', function() use ($app, $db) {

    $data = $db->getData();

    //do something with the data

}

This is called Currying according to Slim docs.

As far as I see these are the exact same thing? Why are they called by different names?

  • 写回答

1条回答 默认 最新

  • dsds33222 2014-12-14 17:06
    关注

    Couple of interesting reading here: What is currying What is dependency injection

    No matter the programming language or framework, we could say "Dependency Injection" (DI) is something like delegation at OOP Class definition (see example) and Currying is something totally different, let's say function parameter simplification.

    DI allows you keep your classes simple and de-coupled. If you are familiar with Laravel, ServiceProvider are a good example. In a nutshell your class has a property which will be set to a instance of another Class (normally implementing an interface) so that you can delegate some functionality. Typical example is:

    interface LogInterface {
       public function write();
    }
    
    class LogToFile implements LogInterface {
       public function write()
       {
           //Do something
       }
    }
    
    class LogToFileDB implements LogInterface {
       public function write()
       {
           //Do something
       }
    }
    
    class MyDIClass {
       private $log;
    
       public function __construct(LogInterface $log){
           $this->log = $log;
       }
    
      public function writeLog(){
           $this->log->write();
       }
    }
    
    //You can do
    $myobj = new MyDIClass(new LogToFile);
    //or you can do 
    //$myobj = new MyDIClass(new LogToDB);
    
    //this will work, no worries about "write()" you are delegating through DI
    $myobj->writeLog();
    

    I've just written this code above (probably not functional) to illustrate DI. Using the interface you ensure the method "write()" will be implemented in any class implementing LogInterface. Then your class forces to get a LogInterface object when objects are instantiated. Forget how "write()" works, it is not your business.

    In regards Currying, technique is diferent you just simplify the params in a function. When using Slim Framework the guys says "you can curry your routes by using $app". I've used Slim a lot and my understanding in that way is, ok, rathaer than pass several variables to your route closures just enrich your $app variable and pass 1 single var: $app

    $app = new \Slim\Slim();
    //Rather than 
    //$app->get('/foo', function () use ($app, $log, $store, ...) {
    
    $app->get('/foo', function () use ($app) {
        $app->render('foo.php'); // <-- SUCCESS
        $app->log->write();
        $app->db->store();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog