@Injects after @Produces?

I am trying to learn DI through dagger 2 and apply it to our product. Application-level items annotated with help @Singletonare fairly simple (e.g. SharedPreferences). There are several dependencies in the thinking of our architecture that are asynchronous in nature, which, as I suggested, have a scope @ForSession.

  • Our token / account identification obtained from Android AccountManager. May be synchronous in case of an existing, valid session. It may be asynchronous if no existing session AccountManagershould show the full input stream.
  • Once we have valid information about the session and session tokens:
    • Provide Endpointdependencies to run so our network layer knows where to find the API.
    • Get our "user" information from the network API.
    • Retrieve additional supporting information from the network API (or local cache).
    • pull localized source strings from the network API (or local cache).
    • Get a component that relies on a binding Service. Provide this component asynchronously only when the binding is Servicecomplete.

The presentation layer must be closed when receiving a collection of these elements. Besides a kind of “boot” display, it can do nothing but this.

, @ProducerModule @Produces. , @Produces ListenableFuture<> , , SettableFuture<> . , set() , .

antsy .

, - , , .

" " , @Inject T. ?

, , . ?

@ProducerModule
public class SessionModule {
@Produces
@ForSession
static ListenableFuture<User> produceSignedInUser(SessionManager sessionManager) {
    return sessionManager.getSignedInUserFuture();
}

@Produces
@ForSession
static ListenableFuture<BoundService> produceBoundService(SessionManager sessionManager) {
    return sessionManager.getBoundServiceFuture();
}

@Produces
@ForSession
static CompositeSessionInfo produceComposite(User user, BoundService service) {
    return new CompositeSessionInfo(user, service);
}
}

:

@ForSession
@ProductionComponent(modules = SessionModule.class)
public interface SessionComponent {
    ListenableFuture<CompositeSessionInfo> getCompsiteSessionInfoFuture();
}

- , - :

SessionComponent component = Dagger_SessionComponent.builder()
    .executor(executor)
    .build();

Futures.addCallback(component.getCompsiteSessionInfoFuture(), 
   new FutureCallback<CompositeSessionInfo> {
       public void onSuccess(CompositeSessionInfo result) {
           releaseTheHounds(result);
       }
       public void onFailure(Throwable t) {
           reportError(t);
       }
});

? : @Produces static? ? (EDIT: static , , , Module).

EDIT:

, . , , , @Inject @Produce d , "" , . , , , .

, @Producer , @Provides, , @Inject ed.

EDIT:

, , . @Inject. , , .

+4
1

, , . , , - , - .

. , , @Module, @Produced SessionProductionComponent, , SessionProvisionComponent. @Component @Provide @Inject.

@Produces
@ForSession
public SessionProvisionModule produceSessionProvisionModule(Application app, SomeAsyncDependency someAsyncDependency, AnotherAsyncDependency anotherAsyncDependency) {
    SessionProvisionModule module = new SessionProvisionModule(someAsyncDependency, anotherAsyncDependency);
    ((App) app).createSessionProvisionComponent(module);
    return module;
}

MainActivity, , :

    App app = (App) getApplication();
    sessionProductionComponent = app.getSessionProductionComponent();
    if (app.getSessionProductionComponent() == null) {
        sessionProductionComponent = app.createSessionProductionComponent(new SessionProductionModule());
    }

    Futures.addCallback(sessionProductionComponent.getSessionProvisionModuleFuture(),
            new FutureCallback<SessionProvisionModule>() {
                @Override
                public void onSuccess(SessionProvisionModule result) {
                    app.getSessionProvisionComponent().inject(MainActivity.this);
                }

                @Override
                public void onFailure(Throwable t) {
                    // handle failure
                }
            });

Future , inject() MainActivity, @Injected , . , @Inject @Produce.

, , , DI. , , , , Future, SessionProvisionComponent @Produced.

.

+1

All Articles