0

I have an app which is distributed to five regions, US, EU, China, etc.

Requests are routed to the closest worker to the client, however some aspects of the requests must be processed in the specific regions according to some business logic, like if the client is for instance requesting info from the NYSE but are in Europe, most of the work could happen in Europe but some of the work has to happen in the US region.

When that happens I use the right QueueClient for the right region to send a message over there where that sensitive work is processed.

I have a routing client to determine when we need to send those messages to the right region, which works fine. However I find I need to manage multiple queue clients.

Looking for a design pattern that handles this need, because today I'm doing this:

public class MyService : IMyAppService
    {
        private readonly IMyAppQueueEventClient usQueueClient;
        private readonly IMyAppQueueEventClient euQueueClient;
        private readonly IMyAppQueueEventClient cnQueueClient;

        public MyAppService(IMyAppQueueEventClient USQueueClient, 
            IMyAppQueueEventClient EUqueueClient;
            IMyAppQueueEventClient CNqueueClient;
            )
        {
            
            this.usQueueClient = USQueueClient;
            this.euQueueClient = EUQueueClient;
            this.cnQueueClient = CNQueueClient;
            
        }

This feels really clunky. As I add new regions I end up having to update the constructer, which leads to follow on changes to update the tests, etc etc.

Surely there is a way to just read from appSettings and make an array of QueueClients and pass them in, and then just pick the right one.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48

1 Answers1

0

The way I solved this need was to use the Decorator pattern.

I took the base QueueClient and I added it and a string Region property type to an implementation like so:

public class RegionalQueueClient

   public string region;
   private QueueClient client;

I added a public Receipt SendMessage() method to this class, which calls the private QueueClient member to pass the message along.

Then I made another type called my SuperQueue which contains a List<RegionalQueueClient> member.

I load them all up using DependencyInjection and it works like a charm, and I'm able to find the right client for the right region using a Linq query as desired.

The records are read from AppSettings as an array, as seen in this answer.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48