Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Moreover, you may find several Fluent API examples and their [usage](src/Example

- [Student](src/ExampleProject/Student.cs)
- [Person](src/ExampleProject/Person.cs)
- [Order](src/ExampleProject/Order.cs)
- [HashCode](src/ExampleProject/HashCode.cs)
- [Node](src/ExampleProject/Node.cs)
- [DockerFile](src/ExampleProject/DockerFile.cs)
Expand Down
43 changes: 43 additions & 0 deletions src/ExampleProject/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Non-nullable member is uninitialized
#pragma warning disable CS8618
// ReSharper disable All

// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
// Implementation with forced steps.

using M31.FluentApi.Attributes;

namespace ExampleProject;

[FluentApi]
public class Order
{
[FluentMember(0)]
public int Number { get; private set; }

[FluentMember(1, "{Name}")]
public DateTime CreatedOn { get; private set; }

[FluentLambda(2, "ShippedTo")]
public Address ShippingAddress { get; private set; }
}

[FluentApi]
public class Address
{
[FluentMember(0, "{Name}")]
public string Street { get; private set; }

[FluentMember(1, "{Name}")]
public string City { get; private set; }

[FluentMember(2, "{Name}")]
public string Zip { get; private set; }

[FluentMember(3, "{Name}")]
[FluentDefault("Default{Name}")]
public string State { get; private set; } = "N/A";

[FluentMember(4, "{Name}")]
public string Country { get; private set; }
}
61 changes: 61 additions & 0 deletions src/ExampleProject/OrderArbitrarySteps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Non-nullable member is uninitialized
#pragma warning disable CS8618
// ReSharper disable All

// Example from https://youtu.be/qCIr30WxJQw?si=FRALafrpA1zWACA8.
// Implementation with arbitrary steps.

using M31.FluentApi.Attributes;

namespace ExampleProject;

[FluentApi]
public class Order2
{
[FluentMember(0)]
[FluentContinueWith(0)]
public int Number { get; private set; }

[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public DateTime CreatedOn { get; private set; }

[FluentLambda(0, "ShippedTo")]
[FluentContinueWith(0)]
public Address2 ShippingAddress { get; private set; }

[FluentMethod(0)]
private void Build()
{
}
}

[FluentApi]
public class Address2
{
[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public string Street { get; private set; }

[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public string City { get; private set; }

[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public string Zip { get; private set; }

[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public string State { get; private set; }

[FluentMember(0, "{Name}")]
[FluentContinueWith(0)]
public string Country { get; private set; }

[FluentMethod(0)]
private void Build()
{
State ??= "N/A";
}
}
35 changes: 35 additions & 0 deletions src/ExampleProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@
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
//

Expand Down