如何使用Dagger 2等IOC容器注入抽象 [英] How do I inject an abstraction using an IOC container like Dagger 2

查看:79
本文介绍了如何使用Dagger 2等IOC容器注入抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是拥有一个抽象,并将抽象作为参数传递。这样一来,如果有一天比翻新更好的事情出现了,而我想更换它,那将很容易做到。虽然我不知道该怎么做。这是我要执行的操作的基本概述:

What I would like to do is to have an abstraction, and pass the abstraction as parameter. So that if one day something better than retrofit comes along and I want to switch it, it will be easy to do so. Although I have no idea on how do it. Here is a basic overview of what I want to do:

public interface ApiClient{
    Object getClient()
}

public class RetrofitClient implements ApiClient{
    private static Retrofit retrofit = null;

    @Override
    public Retrofit getClient(){
        if(retrofit == null){
            OkHttpClient tokenInterceptor = new OkHttpClient.Builder()
                  .addInterceptor(new NetworkInterceptor())
                  .build();

            retrofit = new Retrofit.Builder()
                  .baseUrl("www.hello.com")
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
        }
    }
}

然后我想通过传递ApiClient作为参数来使用它:

And then I would like to consume it by passing ApiClient as parameter:

public class MyClass{
    private ApiClient apiClient;

    public MyClass(ApiClient apiClient){
        this.apiClient =  apiClient;
    }

    public void getSomeData(){
        MyClient client = this.client.create(MyClient.class);
    }
}

我需要某种方式注入 RetrofitClient MyClass 中。
,请向IOC容器提出任何其他替代方案,以及我将如何使用它来实现这一目标,谢谢。

I would need some way of injecting RetrofitClient into MyClass. Please suggest an IOC container any other alternative and how I would use it to achieve this, thanks.

此链接提供了更详细的说明:使用C#和Castle Windsor完成 https://github.com/ castleproject / Windsor / blob / master / docs / basic-tutorial.md

This link provides a more in depth description on it is done using C# and Castle Windsor https://github.com/castleproject/Windsor/blob/master/docs/basic-tutorial.md

推荐答案

在Java中,如C# ,抽象由接口或抽象类表示。

In Java, as in C#, abstractions are represented by interfaces or abstract classes.

您提出的建议可能称为在接口中包装外部库以允许按配置交换。为此,您必须检查要交换出来的具体方法所揭示的方法,并创建一个与之匹配的新接口。

What you are proposing might be called 'wrapping an external library in an interface to allow swapping by configuration'. To do that, you would have to inspect the methods exposed by the concretion that you want to be able to swap out and create a new interface that matches those.

,如果外部库具有这样的方法:

For example, if the external library has methods like this:

public class ExternalLibrary {
    void foo() {
        //foo implementation
    }

    void bar() {
        //bar implementation
    }
}

您将创建一个包装器接口:

You would create a wrapper interface:

public interface FooBarable {
    void foo();
    void bar();
}

下一步是使 ExternalLibrary的使用者在您的应用中取决于 FooBarable

The next step is to make the consumers of ExternalLibrary in your app depend on FooBarable:

public class MyActivity extends Activity {

    FooBar fooBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerFooBarComponent
            .builder()
            .fooBarModule(new ExternalLibraryModule())
            .build()
            .inject(this);
    }
}

如果需要交换例如,ExternalLibrary 可以用于测试模拟,您可以按照 Dagger 2用户指南

If you need to swap ExternalLibrary for, say, a test mock, you can use a test component as per the instructions for testing in the Dagger 2 User Guide:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerTestFooBarComponent
            .builder()
            .mockFooBarModule(new MockFooBarableModule())
            .build()
            .inject(this);
    }

了解DI框架/ IOC容器Dagger 2如何与所有这些一起工作,则必须查看用户指南 Google Dagger 2 Android应用蓝图,因为在一个答案的上下文中有太多需要解释的东西。

To see how the DI framework/IOC container Dagger 2 works with all of this, you would have to look at the user guide and the Google Dagger 2 Android app blueprint as there is too much to explain within the context of one answer.

这篇关于如何使用Dagger 2等IOC容器注入抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆