Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[sisu-users] mixing injection and non-injection

This may be more of a Guice, or even generic DI, issue, but I'll ask
in case there's a sisu-specific pattern.

While initializing a complex system, the code needs at some place to
create N items. Each item needs some facts which are dynamically
determined. It also needs some things which are available for
injection.

I could code the constructor to accept the dynamic resources and call
the injector to inject the rest.

Or I could set up a Provider over the objects, or call the injector to
make them, but code them so that they are not completely constructed
until I pass in the dynamic data.

Or, I can leave these items out of the DI system, and just code the
thing that creates them to have everything they need available via
injection and just pass it in via constructor.

The case in hand is initializing a sequence of worker threads, one for
each of a variable collection of tasks. There's an injected collection
of the overall application that names the tasks. For each task, the
code has to create a thread, and hand it the identity of its task.

     @Inject Collection<Task> tasks;
     @Inject Provider<TaskRunner> provider;


     for (Task task : tasks) {
       // I wish I could ...
      thisRunner =  provider.get(task); // instead of provider.get()
       // OR...
      thisRunner = provider.get(); // or injector.....
      thisRunnset.setTask(task);
      thisRunner.initialize(); // yuck.
      // OR ...
      thisRunner = new TaskRunner(task);
     injector.inject...(thisRunner);
     thisRunner.initialize(); // yuck
    }

Is there a better way to approach this.


Back to the top