C
C#3w ago
Mango

✅ Client creation design dilema

I created a design dilema in my head. I have created a library, which utilizes the tcp client, that has a client with private constructors. One parameterless and one that takes in an IFrameReader and an NetworkStream (the IFrameReader is a custom Pipeline I made that adapts to any type of stream you give it). If you do a parameter-less construction, you have to connect to your device with a host and port that you pass into a ConnectAsync(string host, int port) method. If you construct the client with the IFrameReader and NetworkStream, you do not use ConnectAsync as the data it receives from the IFrameReader you pass in is up to you to interface properly with. I have built this into a factory pattern:
MyClientFactory.CreateDefaultClient(); // Returns MyClient()
MyClientFactory.CreateWithReader(IFrameReader reader, NetworkStream? stream); // Returns MyClient() built with that 2nd ctor
MyClientFactory.CreateDefaultClient(); // Returns MyClient()
MyClientFactory.CreateWithReader(IFrameReader reader, NetworkStream? stream); // Returns MyClient() built with that 2nd ctor
I'm wondering if I should instead have it designed like this:
new MyClient()
.WithFrameReader(reader)
.WithStream(stream);
new MyClient()
.WithFrameReader(reader)
.WithStream(stream);
or something like an optional options param in the ctor
new MyClient(new MyClientOptions
{
FrameReader = someFrameReader;
NetStream = someNetworkStream
});
new MyClient(new MyClientOptions
{
FrameReader = someFrameReader;
NetStream = someNetworkStream
});
or if it even matters at all and I should just stick with my factory pattern.
1 Reply
Mango
MangoOP3w ago
The thing I dislike most about the fluent .With() design is it makes the client mutable, which its really not supposed to be. So perhaps I strike that as an option So I'm really torn between factory pattern and options pattern. But with options pattern you could inject options straight into it with DI like services.Configure<MyClientOptions>() 🤔 Im gonna use the options pattern

Did you find this page helpful?