/
7. ResultTypeWithBehaviour.linq
75 lines (62 loc) · 1.63 KB
/
7. ResultTypeWithBehaviour.linq
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<Query Kind="Program" />
//Problems:
// A lot of ceremony upfront, and code to consume, can get messy and make the code hard to read.
// All the behaviour related to a result is in the result now and reslts can be tested.
void Main()
{
var inputs = new[] { "Food", "Foo", null, };
foreach (var result in inputs.Select(ComplexOperation))
{
result.Process();
}
}
IProcessor processor = new Processor();
Result ComplexOperation(String input)
{
try
{
if (input.Length % 2 == 0)
return new Success(processor, input.Length);
else
return new Failure(processor, "Length is odd.");
}
catch (Exception ex)
{
return new Error(processor, ex);
}
}
class Success : Result
{
public Int32 EvenLength { get; }
public Success(IProcessor p, Int32 value) : base(p) { EvenLength = value; }
public override void Process() => Processor.WriteMessage($"Even length: {EvenLength}.");
}
class Failure : Result
{
public String FailureMessage { get; }
public Failure(IProcessor p, String message) : base(p) { FailureMessage = message; }
public override void Process() => Processor.WriteMessage(FailureMessage);
}
class Error : Result
{
public Exception Exception { get; }
public Error(IProcessor p, Exception ex) : base(p) { Exception = ex; }
public override void Process() => Processor.WriteMessage(Exception);
}
abstract class Result
{
public Result(IProcessor processor)
{
Processor = processor;
}
protected IProcessor Processor { get;}
public abstract void Process();
}
interface IProcessor
{
void WriteMessage(Object message);
}
class Processor : IProcessor
{
public void WriteMessage(Object message) => Console.WriteLine(message);
}