'Refactoring a method and separating out logic that needs to be reused
I'm tasked with refactoring an existing piece of code and separating it out into a separate class, so that the logic in separate class can be reused. I'm not clear on how to do it,
this is the method that needs to be broken down, I have simplified the code,
class TestClass
{
private int maxRetryAttempts = 5;
private int timetoLive = 30000;
private void ProcessMessage(string label, IDictionary<string, object> headers)
{
var message = LoadMyConfiguration.Instance.Get(label);
if (message != null)
{
maxRetryAttempts = message.RetryAttempts;
timetoLive = message.TTL;
}
object retryAttempt = 0;
object timestamp = null;
if (headers != null)
{
if (!headers.TryGetValue("RetryAttempts", out retryAttempt))
{
retryAttempt = 0;
headers.Add("RetryAttempts", retryAttempt);
}
if (!headers.TryGetValue("Timestamp", out timestamp))
{
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
headers.Add("Timestamp", timestamp);
}
}
long currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
try
{
if (((int)retryAttempt < maxRetryAttempts) &&
(((int)retryAttempt > 0) ? (currentTime - (long)timestamp) < timetoLive : true))
{
//BUSINESS LOGIC
//INCREMENT RETRY ATTEMPT
}
else
{
//BUSINESS LOGIC
}
}
catch (Exception ex)
{
throw;
}
}
}
basically there is a retry logic that needs to reused in other projects as well. And the logic to retrieve certain fields from the dictionary also needs to be reusable. The LoadMyConfiguration instance is already a separate class. But the rest of the stuff needs to go into separate class with just the business logic in the ProcessMessage method which calls the new reusable class to check whether to retry or expire.
My OOPS knowledge is not that good and this is what I have so far,
public class ReusableLogic
{
public IDictionary<string, object> MessageHeaders { get; private set; }
public bool IsExpired(long messageTimeStamp)
{
throw new NotImplementedException();
}
public void RetrieveHeaders(IDictionary<string, object> messageHeaders, out int retryAttempts, out long timeStamp)
{
object objRetryAttempts = 0;
object objTimeStamp = null;
object objDeliveryCount = 0;
if (messageHeaders != null)
{
if (!messageHeaders.TryGetValue(RetryAttemptsHeader, out objRetryAttempts))
{
objRetryAttempts = 0;
messageHeaders.Add(RetryAttemptsHeader, objRetryAttempts);
}
if (!messageHeaders.TryGetValue(TimestampHeader, out objTimeStamp))
{
objTimeStamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
messageHeaders.Add(TimestampHeader, objTimeStamp);
}
}
retryAttempts = Convert.ToInt32(objRetryAttempts);
timeStamp = Convert.ToInt64(objTimeStamp);
}
public bool RetryAttemptsExceeded(int currentRetryAttempts, int maxRetryAttempts)
{
throw new NotImplementedException();
}
public void Start()
{
throw new NotImplementedException();
}
}
I know it's not much, but I'm not sure how to proceed further, I just need few ideas or tips on how to proceed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
