How services work

From RifidiWiki

Jump to: navigation, search

Why services

The first version of Designer (and also the rest of Rifidi) was littered with a lot of Singletons.
As everybody knows by now:

  • Singletons are bad.
  • They are hard to test.
  • Impossible (almost) to mock

So I replaced them with a little service registry where you register a concrete class with an interface this class implements.
Once again I had to discover that somebody else had already had that idea some time ago.
OSGi is an SOA (bright me, reading about OSGi all the time without actually realizing what it means).
So I am using now the OSGi ServiceRegistry to deal with services.
You, as an aspiring rifidi developer, won't need to worry about that.
To make it easier to use the services I wrote a ServiceLocater that does all the work for you.
The only thing to remember is to use the @Inject annotation and do the following call in the constructor.

ServiceRegister.getInstance().service(this)

@Inject

This annotation is a marker for the ServiceLocator in the ServiceRegistry. It annotates a setter method that takes the desired service interface as it's only parameter.

@Inject
public void setEventsService(EventsService eventsService) {
	this.eventsService = eventsService;
}

After the call to service(this) in the constructor the locator will check the class for the annotation and try to resolve it.
If the service is not available atb the time of the call it will be resolved as soon as the service becomes available.
NOTE: All OSGi services are supported, als declarative services. The only problem is that due to the way they get initialized they are unsutable for a GUI application.

Personal tools