Running FxCop on my code, I get this warning:
Microsoft.Maintainability : ‘FooBar.ctor is coupled with 99 different types from 9 different namespaces. Rewrite or refactor the method to decrease its class coupling, or consider moving the method to one of the other types it is tightly coupled with. A class coupling above 40 indicates poor maintainability, a class coupling between 40 and 30 indicates moderate maintainability, and a class coupling below 30 indicates good maintainability.
My class is a landing zone for all messages from the server. The server can send us messages of different EventArgs types:
public FooBar() { var messageHandlers = new Dictionary<Type, Action<EventArgs>>(); messageHandlers.Add(typeof(YouHaveBeenLoggedOutEventArgs), HandleSignOut); messageHandlers.Add(typeof(TestConnectionEventArgs), HandleConnectionTest); // ... etc for 90 other types }
The ‘HandleSignOut’ and ‘HandleConnectionTest’ methods have little code in them; they usually pass the work off to a function in another class.
How can I make this class better with lower coupling?
Have the classes that do the work register for events they’re interested in…an event broker pattern.