-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
163 lines (127 loc) · 4.88 KB
/
Program.cs
File metadata and controls
163 lines (127 loc) · 4.88 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
using System.Text.Json;
using ExampleProject;
#pragma warning disable CS8321 // Local function is declared but never used
// Student
//
Student student1 = CreateStudent.Named("Alice", "King").OfAge(22).WhoStartsUniversity()
.LivingIn("New York").WhoIsHappy().WhoseFriendsAre("Bob", "Carol", "Eve");
Student student2 = CreateStudent.Named("Bob", "Bishop").BornOn(new DateOnly(2002, 8, 3)).InSemester(2)
.LivingInBoston().WithUnknownMood().WhoseFriendIs("Alice");
Console.WriteLine(JsonSerializer.Serialize(student1));
Console.WriteLine(JsonSerializer.Serialize(student2));
// DocumentedStudent (generated code includes `summary`, `param` and `returns` XML comments)
//
DocumentedStudent documentedStudent1 = CreateDocumentedStudent.Named("Alice", "King").OfAge(22).WhoStartsUniversity()
.LivingIn("New York").WhoIsHappy().WhoseFriendsAre("Bob", "Carol", "Eve");
DocumentedStudent documentedStudent2 = CreateDocumentedStudent.Named("Bob", "Bishop").BornOn(new DateOnly(2002, 8, 3)).InSemester(2)
.LivingInBoston().WithUnknownMood().WhoseFriendIs("Alice");
Console.WriteLine(JsonSerializer.Serialize(documentedStudent1));
Console.WriteLine(JsonSerializer.Serialize(documentedStudent2));
// ExchangeStudent (inherited from Student)
//
ExchangeStudent exchangeStudent = CreateExchangeStudent.Named("Bob", "Bishop").BornOn(new DateOnly(2002, 8, 3))
.InSemester(2).LivingInBoston().WithUnknownMood().WhoseFriendIs("Alice").WithHomeCountry("United States");
Console.WriteLine(JsonSerializer.Serialize(exchangeStudent));
// Person
//
Person person1 = CreatePerson.WithFirstName("Alice").WithMiddleName("Sophia").WithLastName("King").WhoLivesAtAddress()
.WithHouseNumber("23").WithStreet("Market Street").InCity("San Francisco");
Person person2 = CreatePerson.WithFirstName("Bob").WithLastName("Bishop").WhoseAddressIsUnknown();
Person person3 = CreatePerson.WithFirstName("Eve").WithLastName("Knight").WhoIsADigitalNomad().LivingInCity("Berlin");
Console.WriteLine(JsonSerializer.Serialize(person1));
Console.WriteLine(JsonSerializer.Serialize(person2));
Console.WriteLine(JsonSerializer.Serialize(person3));
// Order (forced steps)
//
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
//
Order order = CreateOrder
.WithNumber(10)
.CreatedOn(DateTime.UtcNow)
.ShippedTo(a => a
.Street("street")
.City("city")
.Zip("zip")
.DefaultState()
.Country("country"));
Console.WriteLine(JsonSerializer.Serialize(order));
// Order (arbitrary steps)
//
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
//
Order2 order2 = CreateOrder2
.CreatedOn(DateTime.UtcNow)
.ShippedTo(a => a
.Country("country")
.Street("street")
.Zip("zip")
.City("city")
.Build())
.WithNumber(10)
.Build();
Console.WriteLine(JsonSerializer.Serialize(order2));
// HashCode
//
int hashCode = CreateHashCode
.Add(42).Add(3.14).AddSequence(new[] { 1, 2, 3 }).Add("Hello world").Value();
Console.WriteLine(hashCode);
// Docker file
//
// Example from https://mitesh1612.github.io/blog/2021/08/11/how-to-design-fluent-api.
//
string dockerFile = CreateDockerFile
.FromImage("node:12")
.CopyFiles("package*.json", "./")
.RunCommand("npm install")
.WithEnvironmentVariable("PORT", "8080")
.ExposePort(8080)
.WithCommand("npm start")
.ToString();
Console.WriteLine(dockerFile);
// Employee
//
// Example from https://stackoverflow.com/questions/59021513/using-fluent-interface-with-builder-pattern.
//
Employee employee = CreateEmployee
.WithName("My Name")
.WithPhone(
p => p.WithNumber("222-222-2222").WithUsage("CELL"))
.WithJobs(
j => j.WithCompanyName("First Company").WithSalary(100),
j => j.WithCompanyName("Second Company").WithSalary(200));
Console.WriteLine(JsonSerializer.Serialize(employee));
// HttpRequest
//
// Example from https://github.com/dotnet/csharplang/discussions/7325.
//
HttpRequestMessage message = CreateHttpRequest
.WithMethod(HttpMethod.Post)
.WithUrl("https://example.com")
.WithHeaders(("Accept", "application/json"), ("Authorization", "Bearer x"))
.WithJsonContent(
new { Name = "X", Quantity = 10 },
opt => opt.PropertyNameCaseInsensitive = true)
.GetMessage();
Console.WriteLine(JsonSerializer.Serialize(message));
// await RunAsyncExample();
static async Task RunAsyncExample()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await CreateHttpRequest
.WithMethod(HttpMethod.Get)
.WithUrl("https://www.m31coding.com")
.WithZeroHeaders()
.WithoutContent()
.SendAsync(client);
Console.WriteLine(response.StatusCode);
}
// Node
//
Node<int> tree = CreateTree<int>.Root(8)
.Left(3, n => n
.Left(1)
.Right(6))
.Right(10, n => n
.LeftNull()
.Right(14));
Console.WriteLine(JsonSerializer.Serialize(tree));