英文:
Dagger using Provides vs creating a custom class
问题
以下是翻译好的内容:
我对Dagger还不熟悉,在一些Dagger项目中,我看到了一种替代@Provides的方法,这让我思考这两种方法之间是否有任何实际区别。
通用代码:
class DependencyOne {
@Inject DependencyOne();
}
class DependencyTwo {
@Inject DependencyTwo();
}
方法1:
class MyClass {
Resource r;
@Inject MyClass(DependencyOne d1, DependencyTwo d2) {
r = Resource.builder().addDependencies(d1,d2).build();
}
Resource getResource() {
return r;
}
}
@Component interface MyComponent {
MyClass getMyClass();
}
// 使用以下方式获取资源
DaggerMyComponent.create().getMyClass().getResource();
方法2:
@Module class MyModule {
@Provides static Resource getResource(DependencyOne d1, DependencyTwo d2) {
return Resource.builder().addDependencies(d1,d2).build();
}
}
@Component(modules={MyModule.class})
interface MyComponent {
Resource getResource();
}
// 使用以下方式获取资源
DaggerMyComponent.create().getResource();
我知道我可能在过度思考,但这两种方法之间是否有任何实际区别,无论是性能还是其他方面?其中一种方法是否优于另一种方法?
英文:
I am new to Dagger and I came across an alternative to @Provides on some Dagger project and it got me thinking if there was any actual difference between the two methods.
Common Code:
class DependencyOne {
@Inject DependencyOne();
}
class DependencyTwo {
@Inject DependencyTwo();
}
Approach 1:
class MyClass {
Resource r;
@Inject MyClass(DependencyOne d1, DependencyTwo d2) {
r = Resource.builder().addDependencies(d1,d2).build();
}
Resource getResource() {
return r;
}
}
@Component interface MyComponent {
MyClass getMyClass();
}
// Get the resource using
DaggerMyComponent.create().getMyClass().getResource();
Approach 2:
@Module class MyModule {
@Provides static Resource getResource(DependencyOne d1, DependencyTwo d2) {
return Resource.builder().addDependencies(d1,d2).build();
}
}
@Component(modules={MyModule.class})
interface MyComponent {
Resource getResource();
}
// Get the resource using
DaggerMyComponent.create().getResource();
Now I know I am probably overthinking this a lot but is there any actual difference between the two approaches, performance or otherwise? Is one of them preferred over the other?
答案1
得分: 0
@Provides
方法和构造函数注入是 Dagger 2 的最佳实践。直接调用组件并像 Resource
这样逐个请求单独的依赖可能应该最小化。
假设你有一个想要能够使用 Resource
的类。你编写了 @Provides
方法来提供 Resource
。然后你可以编写这样一个类:
@Inject
class ResourceConsumer {
private final Resource resource;
@Inject
ResourceConsumer(Resource resource) {
this.resource = resource;
}
}
因为 Dagger 2 知道如何提供一个 Resource
,它也会知道如何提供一个 ResourceConsumer
。然后你可以在任何你想要的地方注入你的 ResourceConsumer
。
英文:
@Provides
methods and constructor injection is the best practice for Dagger 2. Calling the component directly and asking for individual dependencies like the Resource
is probably to be minimized.
Say you have a class that you want to be able to use the Resource
. You write the @Provides
method for the Resource
. Then you can write a class like this:
@Inject
class ResourceConsumer {
private final Resource resource;
@Inject
ResourceConsumer(Resource resource) {
this.resource = resource;
}
}
Because Dagger 2 knows how to provide a Resource
, it will know how to provide a ResourceConsumer
. Then you can inject your ResourceConsumer
wherever you want.
专注分享java语言的经验与见解,让所有开发者获益!
评论