Let's say that initially, we have a complex data processing class designed to operate on a list of Dictionary<string, object>, applying a transformation that converts all string values within the Dictionary to uppercase. Here's the initial version:
using System;
using System.Collections.Generic;
class DataProcessor
{
public void ProcessData(List<Dictionary<string, object>> items)
{
List<Dictionary<string, object>> processedItems = new List<Dictionary<string, object>>();
foreach (var item in items)
{
Dictionary<string, object> processedItem = new Dictionary<string, object>();
foreach (var entry in item)
{
if (entry.Value is string)
{
processedItem[entry.Key] = ((string)entry.Value).ToUpper();
}
else
{
processedItem[entry.Key] = entry.Value;
}
}
processedItems.Add(processedItem);
}
for (int i = 0; i < Math.Min(3, processedItems.Count); i++)
{
Console.WriteLine("Processed Item: " + processedItems[i]);
}
}
}
We intend to expand this functionality, adding capabilities to filter the items based on a condition and to allow for custom transformations. The aim is to retain backward compatibility while introducing these enhancements. Here's the updated approach using method overloading:
using System;
using System.Collections.Generic;
class DataProcessor
{
public void ProcessData(List<Dictionary<string, object>> items)
{
ProcessData(items, item => true, null);
}
public void ProcessData(List<Dictionary<string, object>> items, Func<Dictionary<string, object>, Dictionary<string, object>> transform)
{
ProcessData(items, item => true, transform);
}
public void ProcessData(List<Dictionary<string, object>> items, Predicate<Dictionary<string, object>> condition, Func<Dictionary<string, object>, Dictionary<string, object>> transform)
{
List<Dictionary<string, object>> processedItems = new List<Dictionary<string, object>>();
foreach (var item in items)
{
if (condition(item)) // Apply condition to filter items
{
Dictionary<string, object> processedItem;
if (transform != null)
{
processedItem = transform(item); // Apply custom transformation if provided
}
else
{
// Default transformation: Convert string values to uppercase
processedItem = new Dictionary<string, object>();
foreach (var entry in item)
{
if (entry.Value is string)
{
processedItem[entry.Key] = ((string)entry.Value).ToUpper();
}
else
{
processedItem[entry.Key] = entry.Value;
}
}
}
processedItems.Add(processedItem);
}
}
for (int i = 0; i < Math.Min(3, processedItems.Count); i++)
{
Console.WriteLine("Processed Item: " + processedItems[i]);
}
}
}
// Usage examples:
class Program
{
static void Main(string[] args)
{
List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
Dictionary<string, object> item1 = new Dictionary<string, object>
{
{ "name", "apple" },
{ "quantity", 10 }
};
data.Add(item1);
Dictionary<string, object> item2 = new Dictionary<string, object>
{
{ "name", "orange" },
{ "quantity", 5 }
};
data.Add(item2);
DataProcessor processor = new DataProcessor();
// Default behavior - convert string values to uppercase
processor.ProcessData(data);
// Custom filter - select items with a quantity greater than 5
processor.ProcessData(data, item => (int)item["quantity"] > 5, null);
// Custom transformation - convert names to uppercase and multiply the quantity by 2
processor.ProcessData(data, item =>
{
Dictionary<string, object> transformed = new Dictionary<string, object>();
foreach (var entry in item)
{
if (entry.Key.Equals("name"))
{
transformed[entry.Key] = ((string)entry.Value).ToUpper();
}
else
{
transformed[entry.Key] = (int)entry.Value * 2;
}
}
return transformed;
});
}
}
In this evolved version, we've introduced method overloading with additional parameters: Predicate<Dictionary<string, object>> condition to filter the input list based on a given condition, and Func<Dictionary<string, object>, Dictionary<string, object>> transform for custom transformations of the filtered items. The default behavior processes all items, converting string values to uppercase, which ensures that the original functionality's behavior is maintained for existing code paths. This robust enhancement strategy facilitates adding new features to a function with significant complexity while preserving backward compatibility, showcasing an advanced application of evolving software capabilities responsively and responsibly.