我什么时候应该使用Factory< T>代替提供者< T> [英] When should I use Factory<T> instead of Provider<T>
问题描述
Dagger文档显示使用 Provider< Filter>
来获取过滤器
实例,这看起来很有意义。
The Dagger documentation shows using a Provider<Filter>
to get Filter
instances, which appears to make perfect sense.
我正在写一个 ListAdapter
,它实例化了Views,我希望Dagger能够注入。我很想在我的 ListAdapter
中注入 Provider< ViewType>
,然后调用 mViewProvider .get()
实例化视图。
I am writing a ListAdapter
which instantiates Views, which I'd like Dagger to inject. I'm tempted to inject a Provider<ViewType>
into my ListAdapter
, and call mViewProvider.get()
to instantiate views.
然而,Dagger文档说:
However, the Dagger documentation says:
注入提供商< T>
可能会产生令人困惑的代码,并且可能是错误结构化或错误结构化对象的设计气味在你的图表中。通常你会想要使用 Factory< T>
或 Lazy< T>
或重新组织生命周期和您的代码结构只能注入 T
Injecting
Provider<T>
has the possibility of creating confusing code, and may be a design smell of mis-scoped or mis-structured objects in your graph. Often you will want to use aFactory<T>
or aLazy<T>
or re-organize the lifetimes and structure of your code to be able to just inject aT
我可以看看我如何使用工厂,方式与使用辅助注射时的方式相似。
I can see how I could use a Factory, in a similar manner to that required when using assisted injection.
但是使用我自己的工厂< T>
已经结束使用Dagger的提供商< T>
,因为我必须自己写这个?
But what advantage would using my own Factory<T>
have over using Dagger's Provider<T>
, given I'd have to write the former myself?
推荐答案
提供商< T>
在系统中具有非常具体的含义。它是Graph管理对象的委托构造函数。 提供商< T>
有特定的保证/行为,我建议不要注入提供商< T>
,除非你支持某些需要它的遗留情况。
Provider<T>
has a very concrete meaning in the system. It is a delegated constructor of a Graph-managed object. Provider<T>
has specific guarantees/behaviour and I generally recommend against injecting Provider<T>
unless you are supporting some legacy situation that requires it.
工厂< T>
就是一个例子--FooFactory更准确,因为意图这是你不使用手工工厂,而是使用像 AutoFactory
这样的东西( http://github.com/google/auto )生成创建对象的工厂。然后你不必编写自己的文件,但是在编写这些文档时还没有构建 AutoFactory
。
Factory<T>
is an example - FooFactory is more accurate, because the intent of this is that you not use hand-rolled factories, but instead use something like AutoFactory
(http://github.com/google/auto) to generate factories that create objects. Then you don't have to write your own, but AutoFactory
was not yet built when these docs were written.
最终,原因主要是代码清晰度和长期维护。使用dagger的实例管理作为事实上的实例工厂是可能的,但是有限,因为它只能用于注入依赖关系的实例。如果不添加其他范围或图层,则无法支持调用堆栈依赖关系。在Guice中,这一事实经常导致人们使用额外的范围来通过使用自定义范围来玩游戏并将对象图和分层复杂化以获得一些免费代码,从而将调用堆栈依赖关系转换为对象实例(提供)依赖关系。
Ultimately the reason is primarily code-clarity and long-term maintenance. Using dagger's instance management as a de-facto Factory of instances is possible, but limited, as it can only work with instances that have injected dependencies. Call-stack dependencies cannot be supported without adding another scope, or graph layer. That fact has, in Guice, often caused people to use extra scopes to shoe-horn call-stack dependencies into object instance (provision) dependencies by playing games with custom scopes and complexifying their object graph and layering just to get some free code.
这是为了解决这个问题(在guice中)Assisted-Injection和(在Dagger中)AutoFactory的创建 - 所以你可以做更没有语义清晰的事情取决于框架内部,但会自动为您完成。
It was to solve this that (in guice) Assisted-Injection and (in Dagger) AutoFactory was created - so you could do the more semantically clearer thing that didn't depend on framework internals, but get it automatically done for you.
不使用提供商< T>
是一个意见。如果您愿意, 可以自由地这样做,但不推荐练习。相反,我们建议使用像 AutoFactory
这样的解决方案来获得更好的命名类型,在系统中具有更清晰的含义,可以支持更灵活地处理调用堆栈状态。
Not using Provider<T>
is an opinion. You are free to do so if you wish, but it is not recommended practice. Instead we recommend a solution like AutoFactory
to get better named types, with clearer meaning in your system, that can support more flexible handling of call-stack state.
这篇关于我什么时候应该使用Factory< T>代替提供者< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!