We should either integrate some existing dependency injection framework, or make our own.
An ad-hoc solution could be to have a ServiceManager that handles starting and stopping services, handling dependencies between services. Possible specs:
each Service has a list of dependencies (given in a typed object parameter dependencies in the constructor).
There is a ServiceManager that should be similar to what dependency injection frameworks call the "container".
The "start" or "stop" methods are not called directly, but via the ServiceManager.
For each service, the manager keeps a reversence count of how many times the service was "started".
If X is started and, each of its dependencies will be started first recursively). Then, the reference count of X is increased, and if it goes from 0 to 1, then X.startInternal is called.
When X is stopped, the opposite order happens: the reference count of X is decreased; if it goes to 0, X.stopInternal is called, then each dependency is stopped (recursively).
I thought initially to have separate "init" and "start", for example to register an event listener on a different service before actually starting (e.g. the blockchainMachine listening to newBlock), but I guess the constructor is a good place for that.
Now the idea is that the main service (e.g. the payment gateway) can become a service just like all the others, and it's the only thing that should be started "top-level", as it depends on the right services.
We should either integrate some existing dependency injection framework, or make our own.
An ad-hoc solution could be to have a ServiceManager that handles starting and stopping services, handling dependencies between services. Possible specs:
I thought initially to have separate "init" and "start", for example to register an event listener on a different service before actually starting (e.g. the blockchainMachine listening to newBlock), but I guess the constructor is a good place for that.
Now the idea is that the main service (e.g. the payment gateway) can become a service just like all the others, and it's the only thing that should be started "top-level", as it depends on the right services.