I have 2 third-party providers, both do a similar function(this could be logging, messaging, etc), and both have events that the client must subscribe to.

I want to know what design patterns or methods I need so that I could swap out one with the other without changing any code. My main concern is how to abstract the events. One provider will have certain events that needs wiring up to handlers with certain signatures, and the other will have its own.

Should I have some kind of dictionary of delegates that is exposed via a field on the interface?

I use C#.

  • Would I need to use an EventAggregator?
    – user197619
    May 10 '17 at 8:20
  • Wouldn't you need to abstract away the different calls to the methods that handle the logging and messanging?
    – Zalomon
    May 10 '17 at 8:42
  • Yes. My question relates to where there is more than one provider for a particular concern. If we talk about Messaging, for instance, I would like to swap one Messaging provider with another, i.e. RabbitMq for Kafka. My main concern is the event handling. RabbitMq has certain events that need subscribing to, and Kafka has its own events. So would an EventAggregator help somehow in this scenario?
    – user197619
    May 10 '17 at 8:54

Write your own Wrapper (also called Adapter) for that purpose, one class for each item to be wrapped. The rest of your program is built against the interface of your Wrapper class.

  • Would I use an EventAggregator for the event handling? How would the events be held between both providers if I were to swap one with the other?
    – user197619
    May 10 '17 at 9:06

Create a wrapper interface with two implementation, one for RabbitMQ and another one for Kafka. This would include both the events a different methods. This way you can swap messaging services and use them indistinctively. if you add a factory class you could instanctiate the one you wish for every case or even make it a configuration matter; thus saving you the need for recompilation.

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.