Repository navigation
Expand file tree
/
Copy pathNullObject.cs
More file actions
47 lines (39 loc) · 1.66 KB
/
Copy pathNullObject.cs
File metadata and controls
47 lines (39 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
namespace CSharpExercises.Patterns;
public interface IAuditLog
{
void Record(string action);
}
public sealed class MemoryAuditLog : IAuditLog
{
public List<string> Entries { get; } = [];
public void Record(string action) => Entries.Add(action);
}
/// <summary>
/// Null object: instead of passing null and checking "if (log != null)" everywhere, pass an object that implements the
/// interface and does nothing. Code stays free of null checks and can't forget one. A singleton instance is enough
/// because it has no state. The same idea gives "guest" users, empty discount strategies and no-op loggers.
/// </summary>
public sealed class NullAuditLog : IAuditLog
{
public static readonly NullAuditLog Instance = new();
private NullAuditLog() { }
public void Record(string action) { }
}
public sealed record Shopper(string Name, decimal Credit);
/// <summary>A null shopper with safe defaults, so "no shopper found" doesn't need special cases downstream.</summary>
public static class Shoppers
{
public static readonly Shopper Guest = new("Guest", 0);
public static Shopper FindOrGuest(IReadOnlyDictionary<string, Shopper> customers, string? id) =>
id is not null && customers.TryGetValue(id, out var customer) ? customer : Guest;
}
public sealed class TransferService(IAuditLog? audit = null)
{
private readonly IAuditLog _audit = audit ?? NullAuditLog.Instance; // no null checks below this line
public decimal Transfer(decimal balance, decimal amount)
{
if (amount > balance) throw new InvalidOperationException("Insufficient funds.");
_audit.Record($"transfer {amount}");
return balance - amount;
}
}