Link copied to clipboard!
S — Single Responsibility
One class → one reason to change. Split concerns.
C#
class BadOrder
{
public decimal Subtotal(List items) => items.Sum();
public void Save(string path, List items) =>
File.WriteAllText(path, string.Join(',', items));
}
class PricingService
{
public decimal Subtotal(IEnumerable items) => items.Sum();
}
class OrderRepository
{
public void Save(string path, IEnumerable items) =>
File.WriteAllText(path, string.Join(',', items));
}
class Order
{
readonly PricingService _pricing = new();
readonly OrderRepository _repo = new();
public decimal Total(IEnumerable items) => _pricing.Subtotal(items);
public void Persist(string path, IEnumerable items) => _repo.Save(path, items);
}
// usage
var o = new Order();
var amount = o.Total(new[] { 10m, 20m });
o.Persist("o.txt", new[] { 10m, 20m });
next