正确的Dagger2实施,包括翻新和回调功能

时间:2018-06-21 17:40:07

标签: android dependency-injection dagger-2 dagger

我正在重构项目,我想从依赖注入开始,并让Dagger在整个项目中正常工作。下面有一个我到目前为止所做的示例,它可以正常工作,但是我想查看其是否正确实现,以及是否有一种方法可以在组件的BindsInstance中具有可选字段。

我的示例遵循以下路径:

片段-> ServiceUtil->服务

“我的片段”创建ServiceUtil类的实例,并将“回调”属性之一设置为片段中存在的函数。然后,我的ServiceUtil类使用Retrofit和我的Service接口的实例进行API调用并返回回调。

问题:

  1. 我在下面进行的设置是否合理,并且在结构上是否正确?
  2. 我是否应该创建两个组件,一个在Fragment与ServiceImpl之间,一个在ServiceImpl与Service之间?
  3. 我有一些带有多个回调的ServiceImpl类,因为ServiceImpl处理服务类中的多个已定义服务。例如,我有一个ForgotPassword服务,需要3个API。因此,我在ForgotPasswordService接口中定义了3个API,因此在其实现类中定义了3个回调。有什么办法可以使回调在组件中为可选?某些屏幕可能仅使用3个api调用中的1个。

片段:

public class Fragment1 extends BaseFragment
{
    @Inject
    ForgotPasswordServiceImpl forgotPasswordServiceImpl;

    public static Fragment1 newInstance()
    {
        return new Fragment1();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment1, container, false);

        ComponentA daggerComponentA = DaggerComponentA.builder().apiCallback(apiCallback).baseUrl(Urls.basePreauthUrl).build();
        daggerComponentA.inject(this);

    }

    private Callback<String> apiCallback = new CustomCallback<String>()
    {
        @Override
        public void onResponseReceived(Call<String> call, Response<String> response)
        {
            int statusCode = response.code();
            if (statusCode == HttpCodes.OK && response.body() != null  && response.body().getStatus().equals(SUCCESS_INDICATOR) && !response.body().getParticipantID().isEmpty())
            {
                //Update some stuff
            }else{
                //Update some stuff
            }
        }

        @Override
        public void onSessionExpired(Call<String> call, Response<String> response)
        {
            //Do something different
        }

        @Override
        public void onFailure(@NonNull Call<String> call, @NonNull Throwable t)
        {
            //Do Something
        }
    };

}

ServiceImpl类

public class MyServiceImpl
{

    MyServiceImpl myService;
    Callback<String> apiCallback;


    public MyServiceImpl(MyServiceImpl myService, Callback<String> apiCallback){
        this.myService = myService;
        this.apiCallback = apiCallback;
    }

    public void callApi(String test){

        Call<String> api = myService.findUser();
        api.enqueue(apiCallback);
    }
}

服务等级

public interface MyService
{
    @POST(Urls.exampleUrl)
    Call<String> findUser();
}

组件类

@Component(modules = {TestModule.class})
public interface TestComponent
{
    @Component.Builder
    interface Builder {

        @BindsInstance Builder baseUrl(@Named("baseUrl") String baseUrl);
        @BindsInstance Builder apiCallback(@Named("apiCallback") Callback<String> apiCallback);
        ForgotPasswordComponent build();
    }
    void inject(Fragment1 fragment1);
}

TestModule类

@Module
public class TestModule extends PreAuthBaseModule
{
    @Provides
    public MyService myService(Retrofit retrofit){
        return retrofit.create(MyService.class);
    }

    @Provides
    public Callback<String> apiCallback(@Named("apiCallback") Callback<String> apiCallback){
        return apiCallback;
    }

    @Provides
    public MyServiceImpl myServiceImpl(MyService myService, Callback<String> apiCallback){
        return new MyServiceImpl(myService, apiCallback);
    }
}

PreAuthBaseModule类

@Module(includes = {PreAuthOkHttpClientModule.class, JacksonConverterFactoryModule.class})
public class PreAuthBaseModule
{
    @Provides
    public Retrofit retrofit(@Named("baseUrl") String baseUrl, OkHttpClient okHttpClient, JacksonConverterFactory factory){
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(factory)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();
    }
}

0 个答案:

没有答案