I have to refactor a complex C# app (many dialogs, mixed logic and so on).

There is a part managing the communication with special hardware equipments (sending commands and receive data via asynchronous c# callbacks). The code is "spaghetti" with mixed UI/Logic/Communication/etc and my task is to split the layers in a DDD sense.

So, to which layer belongs a callback driver routine?

The callbacks are creating "bubbles" in the system, up to the UI layer and because of this I cannot enforce the essential principle that any element of a layer depends only on other elements in the same layer or on elements of the layers "beneath" it.

  • Hi, I noticed that you've prefixed your question title with 'C#'. We generally discourage that practice here - our tag system works far better :-) – Alexander R Aug 30 '12 at 12:20

You might want to look at the event aggregator pattern. On one side, you will have the code that interfaces with the hardware. When a message comes in from the hardware, you should post it to the event aggregator (or maybe convert it into an internal event type and post it). Any other component that is interested in that information can subscribe to it and receive it when it comes through. This pattern decouples the code that interfaces with the hardware from the rest of your system.

| improve this answer | |
  • This is what I was looking for. I need to have a unique "component" integrated in the system (but decoupled) which is doing the communication and manages many hardware equipments. Thank you! – CSM Aug 30 '12 at 18:06
  • Glad that I could help! – Michael Brown Aug 30 '12 at 21:29

You're not as inexperienced with special hardware that sends asynchronous events as you seem to think you are. That describes a mouse or keyboard perfectly. Just use the same patterns. In the driver layer, have some sort of registerListener method and create an interface for other classes to implement for receiving events.

| improve this answer | |
  • Yes, sure. I´m sorry if my question is not that clear. I´m looking for a way to split the responsibilities in a complex application. – CSM Aug 30 '12 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.