I just started using dagger 2 and had not used any other dependency injection infrastructures before. Now I am fixated on cyclic dependence, and I do not know how to solve it correctly. Consider an example in a server application that uses a Reactor template with Java NIO:
I have a Handler object attached to a selection key, which is executed when new information arrives on the network:
class Handler implements Runnable { Server server; Client client; public void run {
The Client class contains some state of the connected client. All connected clients are managed in the Server class.
class Client { Handler h; public send(String response) { h.send(response); } }
When a new entry arrives, Handler creates Command objects, executes them on the server, and the server will ultimately respond to the client.
So what I'm doing right now is creating a Client object manually in Handler , passing the this link to be able to send a response:
client = new Client(this);
So my question is: is there something wrong with the design? Is it possible to separate Client and Handler ? Or should I just live with it and not use dependency injection everywhere ?
I appreciate your suggestions
source share