1

For a middleware solution I expose an API, which processes data and sends the necessary information to different parties.

In the current design we create a new client, which acts sort of like an HttpClient per request (because it holds internal state which we keep track of). Previously the clients were tightly coupled within our services, like so:

    public void Process(SomeData data)
    {
        ExampleClient client = new ExampleClient();

        // Process the data

        client.SendACertainRequest(processedData);
    }

After refactoring existing code to allow for easier testing I came up with the solution below, under the assumption that I'm using a factory pattern and as such name it [arbitraryName]Factory. Because the implementation for each of the actual clients created by the factory differs quite a lot, I decided upon using a different Factory for each client, as to not allow the different services to create a client which they should not be able to use. Would these still be called Factories or would it violate any naming rules as it specifies what exact class (albeit interface) is created upon calling the factory?

    public class SampleService
    {
        private readonly IExampleClientFactory _clientFactory;

        public SampleService(IExampleClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }

        public void Process(SomeData data)
        {
            IExampleClient client = _clientFactory.CreateExampleClient();
            
            // Process the data
           
            client.SendACertainRequest(processedData);
        }
    }


Another service would look like:

    public class OtherService
    {
        private readonly IOtherClientFactory _clientFactory;

        public OtherService(IOtherClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }

        public void Process(SomeData data)
        {
            IOtherClient client = _clientFactory.CreateOtherClient();

            // Process the data

            client.DoSomeAction(processedData);
        }
    }

I'm open to improvements on the design and naming.

7
  • This would still be called a factory (think of "factory" as a generic name for an custom-made constructor that can be of varying levels of complexity). This is in some ways similar to the Factory Method pattern, in that it lets the class have places in its logic where it can request creation of a polymorphic object of unknown concrete type. 1/2 Feb 22 at 18:23
  • The difference is that Factory Method is geared towards subclassing (factory functions are virtual methods on the class itself), wheres here the Factory is injected as a Strategy (it's just that it's a "creation strategy"). I wouldn't change the name, though - "factory" communicates the intent/role better. 2/2 Feb 22 at 18:23
  • What benefits does this implementation provide? Feb 22 at 20:54
  • Is HttpClient a part of the implementation of those other classes? If so I would recommend reading this and being careful to avoid creating HttpClient instances directly: stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore Feb 23 at 7:58
  • @RobertHarvey This implementation allows for the mocking of the client in unit tests and adheres to SOLID whereas the previous solution did not.
    – M. Mayhem
    Feb 23 at 9:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.