简体   繁体   English

[Dagger 1.x]:是否将ObjectGraph本身指定为类的依赖项?

[英][Dagger 1.x]: Specifying the ObjectGraph itself as a dependency for a class?

TL;DR; TL; DR;

Is it acceptable for a class to depend on the ObjectGraph itself? 类依赖于ObjectGraph本身是否可以接受?


I need this because I need to inject dependencies on some objects that I load at runtime - at a time that is disconnected from the point at which the ObjectGraph is initialized. 我需要这样做是因为我需要注入对在运行时加载的某些对象的依赖关系-在与ObjectGraph初始化时断开的时间。 Here is an example that illustrates how I use ServiceLoader framework to load concrete implementation classes of a Service at runtime, and then inject dependencies into the loaded implementation classes. 这是一个示例,说明了我如何使用ServiceLoader框架在运行时加载Service的具体实现类,然后将依赖项注入已加载的实现类中。

interface Plugin {
    void doSomething();
}

class AwesomePlugin implements plugin {
    @Inject DependencyOne dependencyOne; 
    @Inject DependencyTwo dependencyTwo;
    void doSomething(){
        // ...some implementation...
    }
}

class PluginEngine{
    public void start(){
        ServiceLoader<Plugin> pluginLoader = ServiceLoader.load(Plugin.class);
        for(Plugin plugin: pluginLoader){
            //TODO: Inject plugin dependencies here
        }
    }
}

Doing this would require the PluginEngine class to have access to the ObjectGraph instance: 为此,需要PluginEngine类有权访问ObjectGraph实例:

class PluginEngine{
    private final ObjectGraph objectGraph;

    public PluginEngine(ObjectGraph graph){
        this.objectGraph = graph;
    }

    public void start(){
        ServiceLoader<Plugin> pluginLoader = ServiceLoader.load(Plugin.class);
        for(Plugin plugin: pluginLoader){
            objectGraph.inject(plugin);
        }
    }
}

Is this a code smell? 这是代码气味吗? Is this pointing to some problem elsewhere in my code, or in the way my dependencies are set up? 这是在我的代码的其他地方,还是在我的依赖项设置方式上指出了一些问题?

While composing this question, I began to see the role of Dagger as a means of replacing arbitrary dependencies with a dependency on the ObjectGraph itself. 在撰写这个问题时,我开始看到Dagger的作用,它是用ObjectGraph本身的依赖关系替换任意依赖关系的一种方法。 On Android, you use a reference to the custom Application sub-class and use it to perform injection - which is basically just a means to get access to the ObjectGraph itself. 在Android上,您使用对自定义Application子类的引用,并使用它执行注入-基本上,这只是一种访问ObjectGraph本身的方法。 Is this reasoning flawed? 这种推理有缺陷吗?

To answer my own question, it looks like this is acceptable. 要回答我自己的问题,看来这是可以接受的。 The u2020 sample app does something roughly similar. u2020示例应用程序执行的操作大致相似。 In fact it makes some very clever use of getSystemService() to achieve this but that is Android specific. 实际上,它非常巧妙地使用getSystemService()来实现这一点,但这是Android特有的。 In particular, look at Injector.java and how it is used from within custom views like TrendingView 特别是,请查看Injector.java以及如何在诸如TrendingView之类的自定义视图中使用它

So, conceptually, one could do something like this - which basically abstracts the concrete ObjectGraph dependency behind an Injector interface. 因此,从概念上讲,人们可以做这样的事情-基本上将Injector接口后面的具体ObjectGraph依赖关系抽象出来。

class PluginEngine{
    private final Injector injector;

    public PluginEngine(Injector injector){
        this.injector = injector;
    }

    public void start(){
        ServiceLoader<Plugin> pluginLoader = ServiceLoader.load(Plugin.class);
        for(Plugin plugin: pluginLoader){
            injector.inject(plugin);
        }
    }
}

This can be refined/adjusted in various ways such that the injector dependency is provided via a constructor or obtained in other ways. 这可以通过各种方式进行完善/调整,以便通过构造函数提供喷射器依赖性或以其他方式获得喷射器依赖性。

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

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