I have a requirement to fetch data for different types of Users (Customers, Publishers etc) for three different timelines (retro, current, and past), all have different URLs. There is a scheduler which will run at the different interval (for example 2 minute for retro data, 5 minutes for current data and 10 minutes for Future Dated Data). I have attached an image for better understanding.
interface IDataPublisher {
void GetUrl();
void Publish();
}
interface ITimeLineGrabber {
TimeSpan GetTimeSpanForNextRun();
}
abstract class BasePublisher : IDataPublisher, ITimeLineGrabber, BackgroundService
{
private readonly ILogger<BasePublisher> _logger;
public BasePublisher (ILogger<BasePublisher> logger)
{
_logger = logger;
}
public abstract string GetUserType();
public abstract string GetDataType();
public abstract TimeSpan GetTimeSpanForNextRun();
public abstract string GetUrl();
public override async Task StartAsync(CancellationToken cancellationToken)
{
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
await base.StopAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var userType = GetUserType();
var dataType = GetDataType();
TimeSpan timeSpan = GetTimeSpanForNextRun();
while (!stoppingToken.IsCancellationRequested)
{
try
{
// Do Something with userType and dataType
await Task.Delay(timeSpan , stoppingToken);
}
catch (Exception ex)
{
_logger.LogError($"Error - {ex.Message}", DateTimeOffset.Now);
}
}
}
public sealed class CustomerRetroPublisher: BasePublisher
{
public override string GetUserType()
{
return "Customer";
}
public override string GetDataType()
{
return "Retro";
}
public override Timespan GetTimeSpanForNextRun()
{
return Timespan.FromMinutes(2); // read from config file..
}
public override string GetUrl()
{
return "CustomerRetroUrl";
}
}
but this will end up with 12 different classes and loads of code will be duplicated/ over complicated. I have a feeling there must be a simple approach to all this. Just wondering what could be the best approach for my requirement.
IDataPublisher
defines a methodPublish()
, but I cannot see this method in your implementation of the interface. Is there something missing? \$\endgroup\$