简体   繁体   English

匕首 2 组件中的匕首 2 模块

[英]dagger 2 module in a dagger 2 component

I'm new to dagger, I decided to use one of my many adapters as training for learning dagger 2, this adapter takes a few objects in its constructor which I've annotated with @Inject我是 dagger 的新手,我决定使用我的许多适配器之一作为学习 dagger 2 的训练,这个适配器在它的构造函数中接受了一些对象,我已经用 @Inject 进行了注释

@Inject
public CardAdapter(
        ItemTouchListener onItemTouchListener,
        @Named(layoutIdentifierName) String layoutIdentifier,
        TypeFactory typeFactory
        RequestManager glide,
) {
    this.onItemTouchListener = onItemTouchListener;
    this.layoutIdentifier = layoutIdentifier;
    this.typeFactory = typeFactory;
    this.elements = new ArrayList<>();
    this.glide = glide;
}

Glide is the one I'm having trouble with I'll come to it last. Glide 是我遇到问题的一个,我会在最后解决它。

First is OnItemTouchListener, It's an interface implemented in my fragment, I've created a module for this.首先是 OnItemTouchListener,它是在我的片段中实现的接口,我为此创建了一个模块。 It's an abstract class with an abstract method annotated with @Binds and included in my components (adapters) modules这是一个抽象的 class 抽象方法用@Binds 注释并包含在我的组件(适配器)模块中

COMPONENT零件

@Component(modules = ItemTouchListenerModule.class)

MODULE模块

@Module
public abstract class ItemTouchListenerModule {

@Binds
abstract ItemTouchListener provideCardHolderFragmentItemTouchListener(CardHolderFragment cardHolderFragment);

} }

Next is the layoutIdentifier its just a string but it changes at runtime so I've made my component have its own builder and given it a method so I can set this at runtime,接下来是 layoutIdentifier 它只是一个字符串,但它在运行时会发生变化,所以我让我的组件有自己的构建器并给它一个方法,这样我就可以在运行时设置它,

@BindsInstance
Builder layoutIdentifier(@Named(layoutIdentifierName) String layoutIdentifier);

Next is the TypeFactory, this is an interface, This is another module with an @Binds annotation接下来是TypeFactory,这是一个接口,这是另一个带有@Binds注解的模块

@Module
public abstract class TypeFactoryModule {
     @Binds
     abstract TypeFactory bindTypeFactory(TypeFactoryForList typeFactoryForList);
}

And last, (the one I'm having issues with,) is glide, I'm actually looking to provide the RequestManager, I created a module for it like this最后,(我遇到问题的那个)是滑翔,我实际上正在寻找提供RequestManager,我为它创建了一个这样的模块

@Module
public class GlideModule {

    public static final String glideContextName = "glide context";
    private Context context;

    public GlideModule(@Named(glideContextName) Context context) {
        this.context = context;
    }

    @Provides
    RequestManager provideGlide(){
         return Glide.with(context);
    }
}

but if I include this in my components modules and add a method in the custom builder and build it, it gives me an error但是如果我将它包含在我的组件模块中并在自定义构建器中添加一个方法并构建它,它会给我一个错误

 error: @Component.Builder is missing setters for required modules or components: [com.sealstudios.simpleaac.dagger.GlideModule]
interface Builder {

here is my component, any ideas?这是我的组件,有什么想法吗? also an explanation would be nice i dont think i get everything that happens here, many thanks还有一个解释会很好我不认为我得到了这里发生的一切,非常感谢

@Component(modules = {TypeFactoryModule.class, ItemTouchListenerModule.class, 
GlideModule.class})
public interface CardAdapterComponent {

String layoutIdentifierName = "layout identifier";
CardAdapter getCardAdapter();

void inject(CardHolderFragment cardHolderFragment);

@Component.Builder
interface Builder {

    @BindsInstance
    Builder layoutIdentifier(@Named(layoutIdentifierName) String layoutIdentifier);

    @BindsInstance
    Builder itemTouchListenerModule(CardHolderFragment cardHolderFragment);

    @BindsInstance
    Builder glideModule(Context context);

    CardAdapterComponent build();
}

} }

Actually the problem is with the GlideModule constructor argument.实际上问题出在GlideModule构造函数参数上。 Dagger has no idea about how to inject a module's constructor! Dagger 不知道如何注入模块的构造函数! Either you can explicitly add a component builder method receiving the explicit GlideModule object or you can just change GlideModule as below,您可以显式添加接收显式GlideModule object 的组件构建器方法,也可以只更改GlideModule如下,

@Module
public class GlideModule {

    public static final String glideContextName = "glide context";

    @Provides
    RequestManager provideGlide(Context context){
         return Glide.with(context);
    }
}

Now you can simply pass the context in the component's builder method.现在您可以简单地在组件的 builder 方法中传递上下文。 Dagger will recognize the Context object type and appropriately pass to the provideGlide method and it should work as expected. Dagger 将识别Context object 类型并适当地传递给provideGlide方法,它应该可以按预期工作。

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

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