-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_CombinedPatterns.cs
More file actions
219 lines (183 loc) · 7.62 KB
/
Copy path06_CombinedPatterns.cs
File metadata and controls
219 lines (183 loc) · 7.62 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using Corsinvest.Fx.Functional;
namespace Corsinvest.Fx.Examples;
/// <summary>
/// Example 06: Combined Patterns
///
/// Demonstrates the power of combining multiple patterns:
/// - Option + ResultOf + Pipe together
/// - Real-world user registration flow
/// - Complex error handling with type-safe errors
/// - Data transformation pipelines with validation
/// </summary>
public static class CombinedPatterns
{
public static void Run()
{
Console.WriteLine("\n═══ Example 06: Combined Patterns (Option + ResultOf + Pipe) ═══\n");
// Scenario: User registration with validation, database lookup, and email sending
var testUsers = new[]
{
("alice@example.com", "Alice", "25"),
(string.Empty, "Bob", "30"), // Missing email
("charlie@example.com", "Charlie", "invalid"), // Invalid age
("bob@example.com", "Bob", "28") // Duplicate email
};
foreach (var (email, name, ageStr) in testUsers)
{
Console.WriteLine($"🔄 Registering: {name} ({email}, age={ageStr})");
var result = RegisterUser(email, name, ageStr);
result.Match(
ok => Console.WriteLine($" ✅ Success: User {ok.Value.Name} registered with ID {ok.Value.Id}"),
fail => Console.WriteLine($" ❌ Failed: {fail.ErrorValue}")
);
Console.WriteLine();
}
// Scenario 2: Configuration loading with defaults
Console.WriteLine("🔧 Configuration Loading with Defaults:\n");
var config = LoadConfigWithDefaults();
Console.WriteLine($" Database: {config.Database}");
Console.WriteLine($" Port: {config.Port}");
Console.WriteLine($" Timeout: {config.TimeoutSeconds}s");
Console.WriteLine($" MaxRetries: {config.MaxRetries}");
}
// Main registration pipeline combining all patterns
private static ResultOf<RegisteredUser, RegistrationError> RegisterUser(string email, string name, string ageStr)
{
// Step 1: Parse and validate input (ResultOf + Pipe)
var validationResult = ValidateEmail(email)
.Bind(_ => ValidateName(name))
.Bind(_ => ParseAge(ageStr))
.Map(age => new ValidatedInput(email, name, age));
// Step 2: If validation fails, short-circuit
if (validationResult.IsFail)
{
return ResultOf.Fail<RegisteredUser, RegistrationError>(RegistrationError.ValidationFailed);
}
var input = validationResult.GetValueOrThrow();
// Step 3: Check if email already exists (Option -> ResultOf)
var existingUserCheck = FindUserByEmail(input.Email)
.Match(
some => ResultOf.Fail<Unit, RegistrationError>(RegistrationError.EmailAlreadyExists),
none => ResultOf.Ok<Unit, RegistrationError>(Unit.Value)
);
if (existingUserCheck.IsFail)
{
return ResultOf.Fail<RegisteredUser, RegistrationError>(RegistrationError.EmailAlreadyExists);
}
// Step 4: Create user (Pipe for transformation)
var userId = Random.Shared.Next(1000, 9999);
var user = new User(userId, input.Name, input.Email, input.Age)
.Pipe(u => SaveUser(u)) // Side effect: save to "database"
.Pipe(u => new RegisteredUser(u.Id, u.Name, u.Email, DateTime.UtcNow));
// Step 5: Send welcome email (Option for nullable result)
SendWelcomeEmail(user.Email)
.Match(
some => Console.WriteLine($" → Welcome email sent to {some.Value}"),
none => Console.WriteLine($" → Warning: Could not send welcome email")
);
return ResultOf.Ok<RegisteredUser, RegistrationError>(user);
}
// Validation functions returning ResultOf
private static ResultOf<string, ValidationError> ValidateEmail(string email)
{
if (string.IsNullOrWhiteSpace(email))
{
return ResultOf.Fail<string, ValidationError>(ValidationError.Required);
}
if (!email.Contains("@"))
{
return ResultOf.Fail<string, ValidationError>(ValidationError.Invalid);
}
return ResultOf.Ok<string, ValidationError>(email);
}
private static ResultOf<string, ValidationError> ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return ResultOf.Fail<string, ValidationError>(ValidationError.Required);
}
return ResultOf.Ok<string, ValidationError>(name);
}
private static ResultOf<int, ValidationError> ParseAge(string ageStr)
{
if (!int.TryParse(ageStr, out var age))
{
return ResultOf.Fail<int, ValidationError>(ValidationError.Invalid);
}
if (age < 0 || age > 150)
{
return ResultOf.Fail<int, ValidationError>(ValidationError.Invalid);
}
return ResultOf.Ok<int, ValidationError>(age);
}
// Database simulation using Option
private static Option<User> FindUserByEmail(string email)
{
// Simulate existing users
var existingUsers = new Dictionary<string, User>
{
["bob@example.com"] = new User(1, "Bob", "bob@example.com", 30)
};
return existingUsers.TryGetValue(email, out var user)
? Option.Some(user)
: Option.None<User>();
}
private static User SaveUser(User user)
{
Console.WriteLine($" → User saved to database: {user.Name}");
return user;
}
// Email service returning Option
private static Option<string> SendWelcomeEmail(string email)
{
// Simulate email sending (could fail)
if (email.Contains("invalid"))
{
return Option.None<string>();
}
return Option.Some(email);
}
// Configuration loading with Option + Pipe for defaults
private static AppConfig LoadConfigWithDefaults()
{
var rawConfig = new Dictionary<string, string>
{
["database"] = "localhost",
["port"] = "5432"
// timeout and maxRetries are missing
};
return new AppConfig(
Database: GetConfigValue(rawConfig, "database").GetValueOr("localhost"),
Port: GetConfigValue(rawConfig, "port")
.Bind(ParseInt)
.GetValueOr(5432),
TimeoutSeconds: GetConfigValue(rawConfig, "timeout")
.Bind(ParseInt)
.GetValueOr(30), // Default timeout
MaxRetries: GetConfigValue(rawConfig, "maxRetries")
.Bind(ParseInt)
.GetValueOr(3) // Default retries
);
}
private static Option<string> GetConfigValue(Dictionary<string, string> config, string key)
=> config.TryGetValue(key, out var value)
? Option.Some(value)
: Option.None<string>();
private static Option<int> ParseInt(string input)
=> int.TryParse(input, out var result)
? Option.Some(result)
: Option.None<int>();
// Domain models
private record ValidatedInput(string Email, string Name, int Age);
private record User(int Id, string Name, string Email, int Age);
private record RegisteredUser(int Id, string Name, string Email, DateTime RegisteredAt);
private record AppConfig(string Database, int Port, int TimeoutSeconds, int MaxRetries);
// Error types
private enum ValidationError { Required, Invalid }
private enum RegistrationError
{
ValidationFailed,
EmailAlreadyExists,
DatabaseError
}
}