-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathResultQueryExamples.cs
More file actions
185 lines (150 loc) · 5.57 KB
/
ResultQueryExamples.cs
File metadata and controls
185 lines (150 loc) · 5.57 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CSharpFunctionalExtensions.Examples.LinqQuery
{
/// <summary>
/// Demonstrates using LINQ query syntax (sugar) as an alternative to
/// fluent method chaining to compose operations in a monadic style,
/// analogous to F# computation expressions or Haskell's do-notation.
/// </summary>
public class ResultQueryExamples
{
public async Task Demo()
{
// Using the LINQ query syntax:
var validCustomer =
from name in CustomerName.Create("jsmith")
from email in Email.Create("jsmith@example.com")
select new Customer(name, email);
// Equivalent to:
var validCustomer_ = CustomerName
.Create("jsmith")
.Bind(name =>
Email.Create("jsmith@example.com").Map(email => new Customer(name, email))
);
// Success(Customer(Name: jsmith, Email: jsmith@example.com))
Console.WriteLine(validCustomer);
var invalidCustomer =
from name in CustomerName.Create("jsmith")
from email in Email.Create("no email")
select new Customer(name, email);
// Failure(E-mail is invalid)
Console.WriteLine(invalidCustomer);
//------------------------------------------------------------------------------
// Also works with async methods.
var billing = await (
from customer in CustomerRepository.GetByIdAsync(1) // Task<Result<Customer>>
from billingInfo in PaymentGateway.ChargeCustomerAsync(customer, 1_000) // Task<Result<BillingInfo>>
select billingInfo
);
// Equivalent to:
var billing_ = await CustomerRepository
.GetByIdAsync(1)
.Bind(customer => PaymentGateway.ChargeCustomerAsync(customer, 1_000));
// Success(BillingInfo(Customer: jsmith@example.com, ChargeAmount: 1000))
Console.WriteLine(billing);
var failedBilling = await (
from customer in CustomerRepository.GetByIdAsync(1)
from billingInfo in PaymentGateway.ChargeCustomerAsync(customer, 5_000_000)
select billingInfo
);
// Failure(Insufficient balance.)
Console.WriteLine(failedBilling);
}
}
public class Email
{
private readonly string _value;
private Email(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static Result<Email> Create(string email)
{
if (string.IsNullOrWhiteSpace(email))
return Result.Failure<Email>("E-mail can't be empty");
if (email.Length > 100)
return Result.Failure<Email>("E-mail is too long");
if (!Regex.IsMatch(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
return Result.Failure<Email>("E-mail is invalid");
return Result.Success(new Email(email));
}
}
public class CustomerName
{
private readonly string _value;
private CustomerName(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static Result<CustomerName> Create(string name)
{
if (string.IsNullOrWhiteSpace(name))
return Result.Failure<CustomerName>("Name can't be empty");
if (name.Length > 50)
return Result.Failure<CustomerName>("Name is too long");
return Result.Success(new CustomerName(name));
}
}
public class Customer
{
public CustomerName Name { get; private set; }
public Email Email { get; private set; }
public Customer(CustomerName name, Email email)
{
if (name == null)
throw new ArgumentNullException("name");
if (email == null)
throw new ArgumentNullException("email");
Name = name;
Email = email;
}
public override string ToString()
{
return $"{nameof(Customer)}({nameof(Name)}: {Name}, {nameof(Email)}: {Email})";
}
}
public class CustomerRepository
{
public static async Task<Result<Customer>> GetByIdAsync(int id)
{
var customer =
from email in Email.Create("jsmith@example.com")
from name in CustomerName.Create("jsmith")
select new Customer(name, email);
return customer;
}
}
public class BillingInfo
{
public Customer Customer { get; set; }
public decimal ChargeAmount { get; set; }
public override string ToString()
{
return $"{nameof(BillingInfo)}({nameof(Customer)}: {Customer.Email}, {nameof(ChargeAmount)}: {ChargeAmount})";
}
}
public class PaymentGateway
{
public static async Task<Result<BillingInfo>> ChargeCustomerAsync(
Customer customer,
decimal chargeAmount
)
{
if (chargeAmount > 1_000_000)
return Result.Failure<BillingInfo>("Insufficient balance");
return Result.Success(
new BillingInfo { Customer = customer, ChargeAmount = chargeAmount }
);
}
}
}