Skip to content

Commit a96ca7a

Browse files
authored
feat(ExampleProject): add Order example from Milan's video (#20)
1 parent 1ac38b9 commit a96ca7a

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Moreover, you may find several Fluent API examples and their [usage](src/Example
5050

5151
- [Student](src/ExampleProject/Student.cs)
5252
- [Person](src/ExampleProject/Person.cs)
53+
- [Order](src/ExampleProject/Order.cs)
5354
- [HashCode](src/ExampleProject/HashCode.cs)
5455
- [Node](src/ExampleProject/Node.cs)
5556
- [DockerFile](src/ExampleProject/DockerFile.cs)

src/ExampleProject/Order.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Non-nullable member is uninitialized
2+
#pragma warning disable CS8618
3+
// ReSharper disable All
4+
5+
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
6+
// Implementation with forced steps.
7+
8+
using M31.FluentApi.Attributes;
9+
10+
namespace ExampleProject;
11+
12+
[FluentApi]
13+
public class Order
14+
{
15+
[FluentMember(0)]
16+
public int Number { get; private set; }
17+
18+
[FluentMember(1, "{Name}")]
19+
public DateTime CreatedOn { get; private set; }
20+
21+
[FluentLambda(2, "ShippedTo")]
22+
public Address ShippingAddress { get; private set; }
23+
}
24+
25+
[FluentApi]
26+
public class Address
27+
{
28+
[FluentMember(0, "{Name}")]
29+
public string Street { get; private set; }
30+
31+
[FluentMember(1, "{Name}")]
32+
public string City { get; private set; }
33+
34+
[FluentMember(2, "{Name}")]
35+
public string Zip { get; private set; }
36+
37+
[FluentMember(3, "{Name}")]
38+
[FluentDefault("Default{Name}")]
39+
public string State { get; private set; } = "N/A";
40+
41+
[FluentMember(4, "{Name}")]
42+
public string Country { get; private set; }
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Non-nullable member is uninitialized
2+
#pragma warning disable CS8618
3+
// ReSharper disable All
4+
5+
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
6+
// Implementation with arbitrary steps.
7+
8+
using M31.FluentApi.Attributes;
9+
10+
namespace ExampleProject;
11+
12+
[FluentApi]
13+
public class Order2
14+
{
15+
[FluentMember(0)]
16+
[FluentContinueWith(0)]
17+
public int Number { get; private set; }
18+
19+
[FluentMember(0, "{Name}")]
20+
[FluentContinueWith(0)]
21+
public DateTime CreatedOn { get; private set; }
22+
23+
[FluentLambda(0, "ShippedTo")]
24+
[FluentContinueWith(0)]
25+
public Address2 ShippingAddress { get; private set; }
26+
27+
[FluentMethod(0)]
28+
private void Build()
29+
{
30+
}
31+
}
32+
33+
[FluentApi]
34+
public class Address2
35+
{
36+
[FluentMember(0, "{Name}")]
37+
[FluentContinueWith(0)]
38+
public string Street { get; private set; }
39+
40+
[FluentMember(0, "{Name}")]
41+
[FluentContinueWith(0)]
42+
public string City { get; private set; }
43+
44+
[FluentMember(0, "{Name}")]
45+
[FluentContinueWith(0)]
46+
public string Zip { get; private set; }
47+
48+
[FluentMember(0, "{Name}")]
49+
[FluentContinueWith(0)]
50+
public string State { get; private set; }
51+
52+
[FluentMember(0, "{Name}")]
53+
[FluentContinueWith(0)]
54+
public string Country { get; private set; }
55+
56+
[FluentMethod(0)]
57+
private void Build()
58+
{
59+
State ??= "N/A";
60+
}
61+
}

src/ExampleProject/Program.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,41 @@
2424
Console.WriteLine(JsonSerializer.Serialize(person2));
2525
Console.WriteLine(JsonSerializer.Serialize(person3));
2626

27+
// Order (forced steps)
28+
//
29+
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
30+
//
31+
32+
Order order = CreateOrder
33+
.WithNumber(10)
34+
.CreatedOn(DateTime.UtcNow)
35+
.ShippedTo(a => a
36+
.Street("street")
37+
.City("city")
38+
.Zip("zip")
39+
.DefaultState()
40+
.Country("country"));
41+
42+
Console.WriteLine(JsonSerializer.Serialize(order));
43+
44+
// Order (arbitrary steps)
45+
//
46+
// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
47+
//
48+
49+
Order2 order2 = CreateOrder2
50+
.CreatedOn(DateTime.UtcNow)
51+
.ShippedTo(a => a
52+
.Country("country")
53+
.Street("street")
54+
.Zip("zip")
55+
.City("city")
56+
.Build())
57+
.WithNumber(10)
58+
.Build();
59+
60+
Console.WriteLine(JsonSerializer.Serialize(order2));
61+
2762
// HashCode
2863
//
2964

0 commit comments

Comments
 (0)