Skip to content

Commit c6914e5

Browse files
committed
Add C# CombinedPatterns
1 parent b31b6cf commit c6914e5

28 files changed

+616
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Constants
2+
{
3+
public const string DAPR_PUBSUB_COMPONENT = "shippingpubsub";
4+
public const string DAPR_PUBSUB_TOPIC = "shipment-registration-events";
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Dapr.Client;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
builder.Services.AddDaprClient();
5+
var app = builder.Build();
6+
app.UseCloudEvents();
7+
8+
app.MapPost("/checkDestination", (
9+
Order order) => {
10+
Console.WriteLine($"checkDestination: Received input: {order}.");
11+
var result = new ShippingDestinationResult(IsSuccess: true);
12+
return Results.Ok(result);
13+
});
14+
15+
app.MapPost("/registerShipment", async (
16+
Order order,
17+
DaprClient daprClient) => {
18+
Console.WriteLine($"registerShipment: Received input: {order}.");
19+
var status = new ShipmentRegistrationStatus(OrderId: order.Id, IsSucces: true);
20+
21+
if (order.Id == string.Empty)
22+
{
23+
Console.WriteLine($"Order Id is empty!");
24+
}
25+
else
26+
{
27+
await daprClient.PublishEventAsync(
28+
Constants.DAPR_PUBSUB_COMPONENT,
29+
Constants.DAPR_PUBSUB_TOPIC,
30+
status);
31+
}
32+
33+
return Results.Created();
34+
});
35+
36+
app.Run();
37+
38+
public record Order(string Id, OrderItem OrderItem, CustomerInfo CustomerInfo);
39+
public record OrderItem(string ProductId, string ProductName, int Quantity, decimal TotalPrice);
40+
public record CustomerInfo(string Id, string Country);
41+
public record ShipmentRegistrationStatus(string OrderId, bool IsSucces, string Message = "");
42+
public record ShippingDestinationResult(bool IsSuccess, string Message = "");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:5261",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7086;http://localhost:5261",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Dapr.AspNetCore" Version="1.15.2" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Dapr.Client;
2+
using Dapr.Workflow;
3+
4+
namespace WorkflowApp.Activities;
5+
6+
public class CheckInventory : WorkflowActivity<OrderItem, ActivityResult>
7+
{
8+
DaprClient _daprClient;
9+
10+
public CheckInventory(DaprClient daprClient)
11+
{
12+
_daprClient = daprClient;
13+
}
14+
15+
public override async Task<ActivityResult> RunAsync(WorkflowActivityContext context, OrderItem orderItem)
16+
{
17+
Console.WriteLine($"{nameof(CheckInventory)}: Received input: {orderItem}.");
18+
19+
var productInventory = await _daprClient.GetStateAsync<ProductInventory>(
20+
Constants.DAPR_INVENTORY_COMPONENT,
21+
orderItem.ProductId);
22+
23+
if (productInventory == null)
24+
{
25+
return new ActivityResult(IsSuccess: false);
26+
}
27+
28+
var isAvailable = productInventory.Quantity >= orderItem.Quantity;
29+
return new ActivityResult(IsSuccess: isAvailable);
30+
}
31+
}
32+
33+
public record ActivityResult(bool IsSuccess, string Message = "");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Net;
2+
using Dapr.Workflow;
3+
4+
namespace WorkflowApp.Activities;
5+
6+
public class CheckShippingDestination : WorkflowActivity<Order, ActivityResult>
7+
{
8+
private readonly HttpClient _httpClient;
9+
10+
public CheckShippingDestination(HttpClient httpClient)
11+
{
12+
_httpClient = httpClient;
13+
}
14+
15+
public override async Task<ActivityResult> RunAsync(WorkflowActivityContext context, Order order)
16+
{
17+
Console.WriteLine($"{nameof(CheckShippingDestination)}: Received input: {order}.");
18+
19+
var response = await _httpClient.PostAsJsonAsync("/checkDestination", order);
20+
if (response.StatusCode != HttpStatusCode.OK)
21+
{
22+
throw new Exception($"Failed to register shipment. Reason: {response.ReasonPhrase}.");
23+
}
24+
25+
var result = await response.Content.ReadFromJsonAsync<ShippingDestinationResult>();
26+
return new ActivityResult(IsSuccess: result.IsSuccess);
27+
}
28+
}
29+
30+
public record ShippingDestinationResult(bool IsSuccess);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Dapr.Workflow;
2+
3+
namespace WorkflowApp.Activities;
4+
5+
public class ProcessPayment : WorkflowActivity<Order, PaymentResult>
6+
{
7+
public override Task<PaymentResult> RunAsync(WorkflowActivityContext context, Order order)
8+
{
9+
Console.WriteLine($"{nameof(ProcessPayment)}: Received input: {order}.");
10+
return Task.FromResult(new PaymentResult(IsSuccess: true));
11+
}
12+
}
13+
14+
public record PaymentResult(bool IsSuccess);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Dapr.Client;
2+
using Dapr.Workflow;
3+
4+
namespace WorkflowApp.Activities;
5+
6+
public class RegisterShipment : WorkflowActivity<Order, RegisterShipmentResult>
7+
{
8+
private readonly DaprClient _daprClient;
9+
10+
public RegisterShipment(DaprClient daprClient)
11+
{
12+
_daprClient = daprClient;
13+
}
14+
15+
public override async Task<RegisterShipmentResult> RunAsync(WorkflowActivityContext context, Order order)
16+
{
17+
Console.WriteLine($"{nameof(RegisterShipment)}: Received input: {order}.");
18+
19+
await _daprClient.PublishEventAsync(
20+
Constants.DAPR_PUBSUB_COMPONENT,
21+
Constants.DAPR_PUBSUB_TOPIC,
22+
order);
23+
24+
return new RegisterShipmentResult(IsSucces: true);
25+
}
26+
}
27+
28+
public record RegisterShipmentResult(bool IsSucces);

0 commit comments

Comments
 (0)