0

I want to create job manager, which jobs could have injectable members.

jobManager.queueJob(new Job1());

After job is queued i want to call object graph and inject jobs fields before starting it.

What is the proper way to access object graph from JobManager class?

Currently I'm injecting android application class that has getter for ObjectGraph.

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

2 Answers2

0

Dagger 1 cannot inject the injector. Dagger 2 will have strongly typed injectors (that you provide as an interface) and these will be injectable, effectively permitting injecting the injector.

Christian Gruber
  • 4,691
  • 1
  • 28
  • 28
  • Does my approach with accessing graph through application object is bad practice? – Alexey Zakharov May 09 '14 at 20:06
  • Injecting the injector (guice terminology) is, in general, bad practice. Specifically, calling graph.get() arbitrarily within your app is what's inadvisable - it creates dependencies that are brittle and hard to test. Dagger 2.0 will allow you to have dependencies on strong types, your custom defined types, so you are more flexibly able to do this sort of thing without having an "injector" or "graph management object" making your code hard to reason-through, and hard to test. – Christian Gruber May 10 '14 at 13:17
0

If you use the JobManager from https://github.com/yigit/android-priority-jobqueue then you can inject your jobs with Dagger 1 by setting a Configuration object on the JobManger:

public JobManager createJobManager(MyApp myApp) {
    Configuration config = new Configuration.Builder(app)
            .injector(new DependencyInjector() {
                @Override
                public void inject(Job job) {
                    myApp.getObjectGraph().inject(job);
                }
            })
            .build();

    return new JobManager(app, config);
}

Now any jobs you add to the JobManager will be injected. This assumes you have an Application subclass which exposes a getObjectGraph() method.

eliasbagley
  • 1,237
  • 9
  • 17