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.
1
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.
2
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.
1