Domanda

I have circural dependency problems in a GWT project set ("Main project", "Widgets project", "Service adapter project"). Main references widgets and service adapters. Widgets reference service adapters. And here comes the problem. I would like to use an EventBus practically everywhere, injected. Where should I put my Ginjector interface?

It has to be usable from every project and has to reference classes from every project too, to be able to inject classes from different projects. This is obviously uncompilable.

I thought of using a separate Ginjector for widgets and one for only the EventBus. If two separate Ginjectors use two separate GinModules both containing @Singleton EventBus bindings, the two getters will return the same EventBus instances or not?

Note: It's a Gin, not a Guice question.

È stato utile?

Soluzione

The most simple way I can think of is to create EventBus as singleton in separate injector (or make it static final field in some class), then use @Provides in other injectors to gain access to that specific EventBus instance.

Altri suggerimenti

I make one module for each logical section (one or more per project often), and then a single ginjector, made available from the entrypoint, referencing all of the modules that it needs. Everything (almost) past the entrypoint is then created by Gin, so it will be able to have fields injected.

Each new Ginjector that is GWT.create'd will have its own set of singletons, so it is important to only create a single root ginjector. It is possible to inject that injector to other parts of the codebase, but generally makes for more readable code if you dont do that.

If you need to pass instances of EventBus or anything else to objects not created by Gin, there are a few options. The first is to build your own Provider instance, and bind that in the module (or create methods in the module annotated with @Provides). The second is to create methods in the ginjector that take a single parameter and return void - Gin will be able to inject all of the fields and setters for that declared type. In the example below, only the fields and setters annotated with @Inject in MyWidget and its superclasses will be injected - it won't know to look for subclasses as well.

void injectMyWidget(MyWidget widget);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top