3

I'd like to know what the best practices are for Eclipse 4 dependency injection. After reading about this subject on the internet, I came up with the following strategy.

requirements

Share the data model of the application (e.g. company, employee, customer, ...) so that framework objects (view parts, handlers, listeners, ...) can access it with as little coupling as possible.

proposed strategy

  • I've used the lifeCycleURI plugin property to register a handler which is triggered at application startup. Such handler creates an "empty" top-level data model container object and place it into the EclipseContext. It is also discarded when application stops.

  • All Eclipse framework classes (view parts, handlers) use the classic DI to get such data model object injected.

  • Button listeners created with the class constructor can't have the data model object injected in them. So I thought they could be created with ContextInjectionFactory.make() to have injection performed. This would couple the class which creates the listener with CIF, but the great advantage is that injection works out of the box.

This is the best solution I've found yet to leverage E4 DI with as little coupling as possible. The weak spot is in my opinion this coupling with CIF. My question would be whether any strategy exist to remove even this coupling, or alternate solutions for the same requirements.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
MoZZoZoZZo
  • 233
  • 1
  • 10

1 Answers1

1

You can create a service class in your project, let's say ModelService.

Add @creatable and @singleton annotations to that class :

@creatable
@singleton
class ModelService{

}

And let DI do its job using following syntax in your parts/handlers/etc ..

@Inject ModelService modelService;

Then you can implement methods in your service like 'createBaseModel()', updateModel() and so on.

This creates a low coupling solution : you can also implement ModelService in a separate plugin and define it as a OSGi service. for that solution, you can read this Lars Vogel article.

Arcadien
  • 2,258
  • 16
  • 26