简体   繁体   English

匕首2单身vs真实单身

[英]Dagger 2 Singleton vs Real Singleton

I am trying to use dagger 2 in my project. 我正在尝试在项目中使用Dagger 2。 Dagger provides a nice way to create singletons and I have a few in my project but Dagger creates a new object graph with every new container for each @Singleton object so we have to create the component in the application level therefor we should provide the application to the class using the singleton and this is not looking really good :(. The question is: Should I keep my old singletons? or should I use dagger? Dagger提供了一种创建单例的好方法,我的项目中有一些,但是Dagger为每个@Singleton对象的每个新容器创建了一个新的对象图,因此我们必须在应用程序级别创建该组件,为此我们应该提供应用程序。类使用单例,这看起来并不好:(。问题是:我应该保留旧的单例吗?还是应该使用匕首?

Dagger is a much better way to manage singletons, in part because you don't need to worry about how to substitute those singletons during unit tests: Your singletons will be injected through means that you can control and override in unit tests (ie constructor arguments and accessible fields). Dagger是管理单例的更好的方法,部分是因为您不必担心如何在单元测试期间替换那些单例:单例将通过您可以在单元测试中进行控制和重写的方式注入(即构造函数参数)和可访问字段)。

Dagger creates a new object graph with every new container for each @Singleton object so we have to create the component in the application level Dagger为每个@Singleton对象的每个新容器创建一个新的对象图,因此我们必须在应用程序级别创建组件

If you want application-level singletons, you will want to keep the same component instance across your application's lifecycle, rather than creating a new one with every new "container". 如果需要应用程序级别的单例,您希望在应用程序的整个生命周期中保持相同的组件实例,而不是在每个新的“容器”中创建一个新的实例。 The component will contain and provide the singletons, so you shouldn't ever need more than one object graph active in your application. 该组件将包含并提供单例,因此您永远不需要在应用程序中激活多个对象图。 If some external creator (eg Android or a servlet engine) creates objects on its own outside of Dagger, this may mean you'll need to save your Dagger component in a singleton holder (perhaps a thread-safe public static field) the way you did for your old singletons; 如果某些外部创建者(例如Android或servlet引擎)在Dagger之外自己创建对象,则可能意味着您需要将Dagger组件保存为单例持有人(也许是线程安全的公共静态字段),方法如下:为您的旧单身人士做了; this should still be easier to understand and maintain, as you can provide as many singletons as you'd like through Dagger, and only worry about one externally-managed singleton for the component itself. 这应该仍然更易于理解和维护,因为您可以通过Dagger提供尽可能多的单例,而只担心组件本身有一个外部管理的单例。

As an alternative, you could keep your old singletons exactly as you have them, and write @Provides methods in a Module to retrieve those singleton instances when your Dagger-created objects request them. 或者,您可以将旧的单例保持原样,并在Module中编写@Provides方法,以在Dagger创建的对象请求这些单例实例时检索它们。 This would allow you to create a new object graph whenever and wherever you'd like, and your singletons will still behave as singletons. 这将使您可以随时随地创建一个新的对象图,并且您的单例仍然会像单例一样运行。 I'd caution against this, though, because at that point your singletons will be accessible two different ways in your application, and only the ones that are Dagger-created or Dagger-managed will be easily overridden in tests; 不过,我要特别提醒您,因为到那时,您的单例将可以在应用程序中以两种不同的方式访问,并且只有那些由Dagger创建或由Dagger管理的方式才能在测试中轻松覆盖; this can be confusing and hard to manage. 这会造成混乱并且难以管理。

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

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