DEV Community

Rafal Pienkowski
Rafal Pienkowski

Posted on

Chain as a Service (CaaS)

Design patterns are very important in daily work of a software developer. At least I think so. I assume that many of you know chain of responsibility design pattern.

If you don't know this design pattern I advise you to read one of those articles before you read further this article:

or you can find article or book about this design pattern on your own.

So now everyone knows chain of responsibility (CoR) design pattern so we can back to main aim of this article. As we know CoR contains series of handlers which are doing their job on incoming requests. After handler's job is done request is passed to its successor. That scenario can be illustrated with an image of a chain:


So let go back to article's title. Chain as a Service (CaaS). I'm pretty sure that every developer needed at least once to create in career a service. I've a question to you: Omitting service purpose do you consider to create a service with CoR design pattern? If not I'll show you something. Let's take our chain from previous image and let's connect first chain link with last one. We'll recive something like that:


Isn't that simple? With that image in back of my head I've created some example project on github. It's written in .Net Core 2.0. Of course you can download it and play with it on your own.

I've implemented 3 handlers:

I've created a ServiceFactory class which implements IHandlerFactory interface and produces my simple service. My service consist of:


and output from application looks like:

Hello world!
Current date and time: 16.11.2017 17:18:12
Hello world!
Current date and time: 16.11.2017 17:18:14
Hello world!
Current date and time: 16.11.2017 17:18:16
Enter fullscreen mode Exit fullscreen mode

I hope this example will encourage you to more frequent use chain of responsibility design pattern. I would be honored if you will build a service in a way I presented.

I hope you enjoy this article. Your feedback will be extremely valuable to me.


Top comments (2)

meanin profile image
Paweł Ruciński

Have you ever implemented real life example of it? Can you share it with us?

If not, can you propose any? Because I can't imagine usage of it in real case scenario.

rafalpienkowski profile image
Rafal Pienkowski

I'm aware that example in on my Github isn't a rewarding as a real life scenario :)

Here you have a more real life scenario: A service which process messages from a queue. It doesn't metter what kind of queue we will use and what should be done by the service. Below an image which will ilustrate that example:


Yellow chain link is responsible for getting messages from a queue. It'll start population of a message in our service. It is the most important chain link in our chain as a service (CaaS) solution. Without it you have "just" Chain of Responsibility (CoR) wich will proccess one message from queue and will end it's work. Our yellow chain link ensures that your chain, as long as there will be a message in a queue, will have a job to do.

By the way. We should remember that our chain is as strong as the most weak element in it.