-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path02_ControlAttributes.cs
249 lines (205 loc) · 7.92 KB
/
02_ControlAttributes.cs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Non-nullable member is uninitialized
#pragma warning disable CS8618
// ReSharper disable All
using System.Text.Json;
using M31.FluentApi.Attributes;
namespace PersonExample1
{
/* Control attributes allow you to jump between builder steps, skip steps, and finalize the object at desired
stages. This enables the creation of non-linear paths and calling different methods based on previously invoked
methods.
Consider the person model below. The address properties are nullable and will only be set if available. We want
to distinguish three different cases:
1. The address is unknown; no address properties are set.
2. The address is known; all address properties are set.
3. The person is a digital nomad; only the city property is set. */
[FluentApi]
public class Person
{
public string Name { get; private set; }
public string? HouseNumber { get; private set; }
public string? Street { get; private set; }
public string? City { get; private set; }
public bool IsDigitalNomad { get; private set; }
}
}
namespace PersonExample2
{
/* Let's create the builder methods 'WhoseAddressIsUnknown', 'WhoLivesAtAddress' and 'WhoIsADigitalNomad' to handle
the three different cases. To create a fork, all these methods are at builder step 1. After calling
'WhoseAddressIsUnknown', the fluent builder should stop, which can be conveniently achieved by adding the
attribute FluentBreak.
When 'WhoLivesAtAddress' is called, the builder continues with steps 2, 3, and 4, setting the three address
properties HouseNumber, Street, and City, respectively. I have indented the respective builder methods
for better readability.
So far, so good. However, for the generated API in this example, we also need to set the full address when
'WhoIsADigitalNomad' is called. To complete our implementation, we must jump to step 5 in this case, as
demonstrated in the next example. */
[FluentApi]
public class Person
{
[FluentMember(0)]
public string Name { get; private set; }
public string? HouseNumber { get; private set; }
public string? Street { get; private set; }
public string? City { get; private set; }
public bool IsDigitalNomad { get; private set; }
[FluentMethod(1)]
[FluentBreak]
private void WhoseAddressIsUnknown()
{
}
[FluentMethod(1)]
private void WhoLivesAtAddress()
{
}
[FluentMethod(2)]
private void WithHouseNumber(string houseNumber)
{
HouseNumber = houseNumber;
}
[FluentMethod(3)]
private void WithStreet(string street)
{
Street = street;
}
[FluentMethod(4)]
private void InCity(string city)
{
City = city;
}
[FluentMethod(1)]
private void WhoIsADigitalNomad()
{
IsDigitalNomad = true;
}
}
public static class Usage
{
public static void UseTheGeneratedFluentApi()
{
Person person1 = CreatePerson.WithName("Alice").WhoseAddressIsUnknown();
Person person2 = CreatePerson.WithName("Bob").WhoLivesAtAddress()
.WithHouseNumber("23").WithStreet("Market Street").InCity("San Francisco");
Person person3 = CreatePerson.WithName("Eve").WhoIsADigitalNomad()
.WithHouseNumber("82").WithStreet("Friedrichstraße").InCity("Berlin");
}
}
}
namespace PersonExample3
{
/* The attribute FluentContinueWith indicates a jump to the specified builder step. In this example, when
'WhoIsADigitalNomad' at step 1 is called, the fluent API continues with step 5, 'LivingInCity'. After step 5 the
builder stops, since there are no further steps.
With the introduction of step 5, the builder no longer stops after the 'InCity' method at step 4. To address
this, I've added the FluentBreak attribute to the 'InCity' method. */
[FluentApi]
public class Person
{
[FluentMember(0)]
public string Name { get; private set; }
public string? HouseNumber { get; private set; }
public string? Street { get; private set; }
public string? City { get; private set; }
public bool IsDigitalNomad { get; private set; }
[FluentMethod(1)]
[FluentBreak]
private void WhoseAddressIsUnknown()
{
}
[FluentMethod(1)]
private void WhoLivesAtAddress()
{
}
[FluentMethod(2)]
private void WithHouseNumber(string houseNumber)
{
HouseNumber = houseNumber;
}
[FluentMethod(3)]
private void WithStreet(string street)
{
Street = street;
}
[FluentMethod(4)]
[FluentBreak]
private void InCity(string city)
{
City = city;
}
[FluentMethod(1)]
[FluentContinueWith(5)]
private void WhoIsADigitalNomad()
{
IsDigitalNomad = true;
}
[FluentMethod(5)]
private void LivingInCity(string city)
{
City = city;
}
}
public static class Usage
{
public static void UseTheGeneratedFluentApi()
{
Person person1 = CreatePerson.WithName("Alice").WhoseAddressIsUnknown();
Person person2 = CreatePerson.WithName("Bob").WhoLivesAtAddress()
.WithHouseNumber("23").WithStreet("Market Street").InCity("San Francisco");
Person person3 = CreatePerson.WithName("Eve").WhoIsADigitalNomad().LivingInCity("Berlin");
}
}
}
namespace PersonExample4
{
/* The FluentReturn attribute, which can only be used on methods, instructs the builder to return the value produced
by the decorated method. Without FluentReturn, FluentMethods must return void and the builder always returns the
interface of the next step, or, in the final step, the built instance (e.g. a Person object).
FluentReturn allows the builder to respect the return value of the decorated method, enabling the return of
arbitrary types and values within the generated API. If a void method is decorated with FluentReturn, the builder
method will also return void. */
[FluentApi]
public class Person
{
[FluentMember(0)]
public string Name { get; private set; }
[FluentMethod(1)]
[FluentReturn]
public string ToJson()
{
return JsonSerializer.Serialize(this);
}
}
public static class Usage
{
public static void UseTheGeneratedFluentApi()
{
string serializedPerson = CreatePerson.WithName("Alice King").ToJson();
}
}
}
namespace PersonExample5
{
/* Lastly, this example introduces the FluentSkippable attribute, which allows builder methods to be optional. The
generated API offers the following behavior: After setting the first name, you can either set the last name to
complete the instance or set the middle name followed by the last name. */
[FluentApi]
public class Person
{
[FluentMember(0)]
public string FirstName { get; private set; }
[FluentMember(1)]
[FluentSkippable]
public string? MiddleName { get; private set; }
[FluentMember(2)]
public string LastName { get; private set; }
}
public static class Usage
{
public static void UseTheGeneratedFluentApi()
{
Person person2 = CreatePerson.WithFirstName("Bob").WithLastName("Bishop");
Person person1 = CreatePerson.WithFirstName("Alice").WithMiddleName("Sophia").WithLastName("King");
}
}
}