Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Aug 30 '12 at 12:35

This question came from our site for professional and enthusiast programmers.

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
up vote 1 down vote accepted

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.

share|improve this answer
This is what I was looking for. Thank you! – CSM Aug 30 '12 at 17:59
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.

share|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 posting your answer, you agree to the privacy policy and terms of service.

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