Dagger 2可重用组件

时间:2016-04-12 14:08:30

标签: android dependency-injection dagger-2

使用Dagger 2,我有一个调用Web服务的模块。我需要每次都能用不同的参数调用它(基本上是后端的切换,所以只有True或False才会改变)。我没有看到如何将这些参数传递给除构造函数之外的Dagger 2模块,或者将某些内容作为@Provides语句的一部分注入。我非常熟悉DI,因为我每天都在进行Spring开发,但是,我在Dagger 2中缺少这个方面。从某种意义上说,我想创建一个Spring @Service,或者@Component,用我可以调用的方法传入适当的参数.Dagger 2缺少什么?

模块是否是我想要创建的正确对象?

更新:添加了一些示例代码。

模块:

@Module
public class RecordedProgramWatchedStatusModule {

    private int chanId = -1;
    private DateTime startTime = null;
    private boolean watched;

    public RecordedProgramWatchedStatusModule() { }

    public RecordedProgramWatchedStatusModule( int chanId, DateTime startTime, boolean watched ) {
        this.chanId = chanId;
        this.startTime = startTime;
        this.watched = watched;
    }

    @Provides
    @PerActivity
    @Named( "updateRecordedProgramWatchedStatus" )
    UseCase provideGetUserDetailsUseCase( DvrRepository dvrRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread ) {
        return new PostUpdatedRecordedWatchedStatus( chanId, startTime, watched, dvrRepository, threadExecutor, postExecutionThread );
    }

}

可观察:

public class PostUpdatedRecordedWatchedStatus extends UseCase {

    private final int chanId;
    private final DateTime startTime;
    private final boolean watched;
    private final DvrRepository dvrRepository;

    public PostUpdatedRecordedWatchedStatus( final int chanId, final DateTime startTime, final boolean watched, final DvrRepository dvrRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread ) {
        super( threadExecutor, postExecutionThread );
        this.chanId = chanId;
        this.startTime = startTime;
        this.watched = watched;
        this.dvrRepository = dvrRepository;
    }

    @Override
    protected Observable buildUseCaseObservable() {
        return this.dvrRepository.updateRecordingWatchedStatus( this.chanId, this.startTime, watched );
    }
}

后端处理程序:

@SuppressWarnings( "Convert2MethodRef" )
@Override
public Observable<Boolean> updateRecordingWatchedStatus( final int chanId, final DateTime startTime, final boolean watched ) {
    Log.d( TAG, "updateRecordingWatchedStatus : enter" );
    final DvrDataStore dvrDataStore = this.dvrDataStoreFactory.createMasterBackendDataStore();

    return dvrDataStore.updateRecordingWatchedStatus( chanId, startTime, watched )
            .doOnError( throwable -> Log.e( TAG, "updateRecordingWatchedStatus : error", throwable ) )
            .doOnCompleted( () -> dvrDataStore.recordedProgramEntityList( true, -1, -1, null, null, null ) );
}

1 个答案:

答案 0 :(得分:0)

所以我看到了发生了什么。在这种情况下,我实现了一个名为DynamicUseCase的东西,您可以在其中将参数传递给您的UseCase,并在存储库中检索它们。 Check you my project

看看: https://github.com/leonardo2204/Flow1.0.0-alphaExample/blob/master/app/src/main/java/leonardo2204/com/br/flowtests/domain/interactor/DynamicUseCase.java

https://github.com/leonardo2204/Flow1.0.0-alphaExample/blob/master/app/src/main/java/leonardo2204/com/br/flowtests/domain/interactor/GetContacts.java

https://github.com/leonardo2204/Flow1.0.0-alphaExample/blob/master/app/src/main/java/leonardo2204/com/br/flowtests/presenter/FirstScreenPresenter.java#L86-L89

您只是从DynamicUseCase继承,传递Bundle中的变量并将其作为UseCase包参数传递,例如:

public class GetContacts extends DynamicUseCase {

    private final ContactsRepository contactsRepository;

    @Inject
    public GetContacts(ContactsRepository contactsRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) {
        super(threadExecutor, postExecutionThread);
        this.contactsRepository = contactsRepository;
    }

    @Override
    public Observable buildUseCaseObservable(Bundle bundle) {
        return this.contactsRepository.getContactsFromPhone(bundle.getBoolean("mustHaveNumber", false));
    }
}

Bundle bundle = new Bundle(1);
bundle.putBoolean("mustHaveNumber", musthavenumber);
this.getContacts.execute(new GetContactsSubscriber(), bundle);

它是那样的! 希望有所帮助!