I have a class ChatManager
, which has a ChatServer
and a ChatClient
(WCF) class inside of them.
I want my controller which instantiates the ChatManager
to be able to subscribe to the UserConnected
, UserDisconnected
and MessageReceived
events that are on the ChatClient
.
What is the most elegant and logical way to do this? Is it silly for me to define the events in the ChatClient
like I have, and then redefine the events in the ChatManager
solely to pass the events up to the Controller without it having to deal with or know about the ChatClient
? The ChatManager
would subscribe to the events of the ChatClient
, and then fire off its own events which the ChatController
would be listening to.
I know WPF has the concept of bubbling up of events, but I don't know if that is for this type of scenario, since nothing is part of a user interface.
I'd start by questioning whether both ChatManager
and ChatController
can both justify their own existence. Generally whenever you find yourself creating a "Manager" class, it really isn't necessary, especially if part of what it is doing consists of merely relaying messages.
Controller classes can struggle against SRP since their "responsibility" is quite broad. In cases where you want to delegate responsibility for certain behaviour then leave the responsibility for the ChatClient
with the controller, and initialize a subordinate controller with the ChatClient
(through a contract interface) so that it can interact with the client as needed. Just make sure that when you start registering for events that you de-register those events before discarding subordinates or the client, otherwise you'll be looking at managed memory leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With