简体   繁体   English

Dagger可以用来在内容提供商上执行注入吗?

[英]can Dagger be used to perform injection on a Content Provider?

I have recently been integrating Dagger into a project that uses ContentProviders. 我最近一直在将Dagger集成到一个使用ContentProviders的项目中。 I create a single ObjectGraph instance in my custom Application object, and basically in each managed component: 我在自定义Application对象中创建了一个ObjectGraph实例,基本上在每个托管组件中:

  • Activity, 活动,
  • Fragment, 分段,
  • Service 服务

... Then, I call getApplication(), downcast to my custom Application object, and force the injection through some custom implementation in my Application class. ...然后,我调用getApplication(),向下转换到我的自定义Application对象,并通过我的Application类中的一些自定义实现强制注入。 This seems to be the prescribed method of performing injection based on the samples I've seen posted by the guys at Square. 这似乎是根据我在Square看到的人发布的样本进行注射的规定方法。

This pattern doesn't hold for ContentProvider instances though as their lifecycle isn't as predictably tied to the lifecycle of the Application object, ie ContentProviders can be, and as I'm observing frequently are , created before the Application object is created (for reasons I have yet to comprehend). 这种模式不适用于ContentProvider实例,因为它们的生命周期与Application对象的生命周期没有预期的关联,即ContentProviders可以,并且正如我经常观察的那样 ,是在创建Application对象之前创建的(对于我尚未理解的原因)。

so... does anyone have a nice way of injecting ContentProviders using Dagger? 所以...有没有人有一个使用Dagger注入ContentProviders的好方法? I've so far made it by having an isInjected() call at the beginning of each of my ContentProvider's interface methods (insert, query, update, delete)... basically a hacky form of lazy initialization. 到目前为止,我已经通过在每个ContentProvider的接口方法(插入,查询,更新,删除)的开头调用isInjected()来实现它...基本上是一种延迟初始化的hacky形式。 But this seems far from ideal. 但这似乎远非理想。 Is there a more prescribed approach to injecting ContentProviders? 是否有更规定的方法来注入ContentProviders?

The Application subclass is just a convention since it's usually the first object created. Application子类只是一个约定,因为它通常是第一个创建的对象。 Our apps do not have content providers which is why we use them. 我们的应用程序没有内容提供商,这就是我们使用它们的原因。 There's nothing that says you can't put it somewhere else. 没有什么可以说你不能把它放在其他地方。

You can just use the traditional singleton pattern for instantiating and holding a reference to the ObjectGraph . 您可以使用传统的单例模式来实例化并保持对ObjectGraph的引用。

public final class Dagger {
  static ObjectGraph og;

  static ObjectGraph og() {
    if (og == null) {
      og = ObjectGraph.create(..);
    }
    return og;
  }
}

The first person to access will initialize the instance which will be used for the lifetime of the process. 第一个访问者将初始化将用于进程生命周期的实例。

If your content provider is in a different process than your main application this solution will still work. 如果您的内容提供商与主应用程序处于不同的进程,则此解决方案仍然有效。 Or you could simply create the graph when your content provider is created since it will be the only consumer. 或者您可以在创建内容提供商时简单地创建图表,因为它将是唯一的消费者。 Normal multi-process rules still apply, of course, so no instances will be shared with the other processes. 当然,正常的多进程规则仍然适用,因此不会与其他进程共享任何实例。

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

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