Dagger 2 - 使用组件和模块的正确方法是什么?

时间:2016-05-09 08:19:38

标签: java android dependency-injection inversion-of-control dagger-2

在使用Dagger 2进行一些游戏之后,我设法将inject我的依赖关系转换为其依赖对象,但我不确定它是否正确地执行此操作,并且我想要正确地执行此操作方式。

我这样做的方式,我有一个 component,其中包含多个 modules,我在顶级做了注入,依赖" seep" in。这是正确的方法吗?

应用组件:

@Singleton
@Component(modules = {AppModule.class, ServiceModule.class, CallAlarmModule.class})
public interface AppComponent {
    void inject(HealthCheckerApplication app);
    void inject(SettingsActivity activity);
    void inject(AlertCreator alertCreator);
    void inject(HealthService service);
    INotificationDataFactory provideFactory();
}

模块:

@Module
public class AppModule {
    HealthCheckerApplication app;
    AlertCreator alertCreator;

    public AppModule(HealthCheckerApplication app) {
        this.app = app;
        this.alertCreator = new AlertCreator(app);
    }

    @Provides
    @Singleton
    public SharedPreferences provideSharedPrefs() {
        return PreferenceManager.getDefaultSharedPreferences(app);
    }

    @Singleton
    @Provides
    public AlertCreator provideAlertCreator() {
        return alertCreator;
    }
}

@Module
public class ServiceModule {

    HealthService service;

    public ServiceModule(HealthService service) {
        this.service = service;
    }

    @Singleton
    @Provides
    public HealthService provideService() {
        return service;
    }

    @Provides
    public INotificationDataFactory provideFactory() {
        return new NotificationDataFactory();
    }

}

@Module
public class CallAlarmModule {

    IMinutesCounter.ITimerCallback timerCallBack;

    public CallAlarmModule(IMinutesCounter.ITimerCallback timerCallBack) {
        this.timerCallBack = timerCallBack;
    }

    @Singleton
    @Provides
    public IMinutesCounter.ITimerCallback provideCallAlarm() {
        return timerCallBack;
    }

    @Singleton
    @Provides
    public IMinutesCounter provideMinutesMonitor() {
        return new MinutesMonitor(timerCallBack);
    }
}

在代码中使用

状况服务:

public class HealthService extends Service {

    @Inject
    public NotificationCreator notificationCreator;

    @Inject
    public CallTooLongAlarm callTooLongAlarm;

    @Override
    public void onCreate() {
        super.onCreate();
        // creating all modules here (besides AppModule) and let dependencies "seep" in
        AppComponent appComponent = ((HealthCheckerApplication) getApplication()).getAppBuilder().serviceModule(new ServiceModule(this)).callAlarmModule(new CallAlarmModule(callTooLongAlarm)).build();
        appComponent.inject(this);

        notificationDataFactory = appComponent.provideFactory();
    }
}

应用

public class HealthCheckerApplication extends Application {
    DaggerAppComponent.Builder appBuilder;

    @Override
    public void onCreate() {
        super.onCreate();
        appBuilder = DaggerAppComponent.builder()
                .appModule(new AppModule(this));
    }

    public DaggerAppComponent.Builder getAppBuilder() {
        return appBuilder;
    }
}

CallTooLongAlarm:

public class CallTooLongAlarm implements MinutesMonitor.ITimerCallback {

    private HealthService healthServiceContext;

    @Inject
    public IMinutesCounter minutesMonitor;

    @Inject
    public CallTooLongAlarm(HealthService context) {
        this.healthServiceContext = context;
    }
}

Dagger 2 应该如何使用?请注意,我在多个地方访问AlertCreator,因此我需要在所有地方确保其相同的实例,因此请在AppModule中创建。

0 个答案:

没有答案