Ho progettato il codice seguente per uno dei requisiti. Devo scrivere diversi test per il codice e avrei bisogno di feedback su dove posso migliorare il design.
Requisito:
- Leggi i dettagli del cliente da csv, calcola le sue spese medie su diversi articoli su base annua e scrivi di nuovo su csv.
- Leggi i dettagli del cliente da csv, calcola le sue spese totali su faimly e scrivi di nuovo a csv.
Poiché sia ExpenseCalculator utilizza la funzione CsvReaderWriter, è meglio creare la classe astratta che implementa la funzionalità csvReaderWriter e deriva dalla classe astratta o è meglio usare Interface?
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Expenses { get; set; }
// other details
}
public interface ICsvReaderWriter<T>
{
IEnumerable<T> ParseCsv(string filePath);
void WriteCsv(string filePath, IEnumerable<Customer> customers);
}
public class ReaderWriter : ICsvReaderWriter<Customer>
{
public IEnumerable<Customer> ParseCsv(string filePath)
{
throw new NotImplementedException();
}
public void WriteCsv(string filePath, IEnumerable<Customer> customers )
{
throw new NotImplementedException();
}
}
public interface IExpenseCalculator
{
void CalculateExpenses(string filePath);
}
public class ItemExpenseCalculator : IExpenseCalculator
{
private readonly ICsvReaderWriter<Customer> _csvReaderWriter;
public ItemExpenseCalculator(ICsvReaderWriter<Customer> csvReaderWriter)
{
this._csvReaderWriter = csvReaderWriter;
}
public void CalculateExpenses(string filePath)
{
var customers = _csvReaderWriter.ParseCsv(filePath);
// do manipulation
_csvReaderWriter.WriteCsv(filePath, customers);
}
}
public class FamilyExpenseCalculator: IExpenseCalculator
{
private readonly ICsvReaderWriter<Customer> _csvReaderWriter;
public FamilyExpenseCalculator(ICsvReaderWriter<Customer> csvReaderWriter)
{
this._csvReaderWriter = csvReaderWriter;
}
public void CalculateExpenses(string filePath)
{
var customers = _csvReaderWriter.ParseCsv(filePath);
// do manipulation
_csvReaderWriter.WriteCsv(filePath, customers);
}
}
public class Program
{
private void Main()
{
IExpenseCalculator itemExpenseCalculator = new ItemExpenseCalculator(new ReaderWriter());
itemExpenseCalculator.CalculateExpenses(@"D:\");
IExpenseCalculator familyExpenseCalculator = new FamilyExpenseCalculator(new ReaderWriter());
familyExpenseCalculator.CalculateExpenses(@"D:\");
}
}