I'm using Proto.Actor and I have an actor which acts as a proxy and forwards the messages to the appropriate exchange just because each exchange has different subscription methods, different parameters are passed, etc. I made a factory method which looks like below. What I don't like is the fact that there is a repeated logic. What would you recommend to me in order to follow up the DRY principle (Do not repeat yourself)?
using Proto;
namespace Api.Actors.Exchanges;
internal sealed class ExchangeFactory
{
private readonly IContext _context;
private readonly IServiceProvider _serviceProvider;
private readonly int _retryAttempts;
internal ExchangeFactory(IContext context, IServiceProvider serviceProvider, int retryAttempts)
{
_context = context;
_serviceProvider = serviceProvider;
_retryAttempts = retryAttempts;
}
internal PID CreateExchange(string exchange, string accountId)
{
switch (exchange)
{
case "ftx":
var exchangeProps = Props.FromProducer(() =>
// ActivatorUtilities is used for partial dependency injection.
ActivatorUtilities.CreateInstance<FtxSubscription>(_serviceProvider, accountId)
)
.WithChildSupervisorStrategy(
new OneForOneStrategy((_, _) => SupervisorDirective.Restart, _retryAttempts, null)
);
return _context.SpawnNamed(exchangeProps, $"{exchange}:{accountId}");
case "kraken":
var exchangeProps2 = Props.FromProducer(() =>
// ActivatorUtilities is used for partial dependency injection.
ActivatorUtilities.CreateInstance<KrakenSubscription>(_serviceProvider, accountId)
)
.WithChildSupervisorStrategy(
new OneForOneStrategy((_, _) => SupervisorDirective.Restart, _retryAttempts, null)
);
return _context.SpawnNamed(exchangeProps2, $"{exchange}:{accountId}");
default:
throw new NotSupportedException();
}
}
}