|
| 1 | +using FlexFlow.Interfaces; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | + |
| 4 | +namespace FlexFlow.Tests; |
| 5 | + |
| 6 | +public class Order |
| 7 | +{ |
| 8 | + public int Id { get; set; } |
| 9 | + public decimal TotalAmount { get; set; } |
| 10 | + public bool IsProcessed { get; set; } |
| 11 | + public bool IsShipped { get; set; } |
| 12 | + public int RetryCount { get; set; } |
| 13 | +} |
| 14 | + |
| 15 | +public class ProcessingException(string message) : Exception(message) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +public class ComplexWorkflow(ILogger<ComplexWorkflow> logger) : IWorkflow<int, Order> |
| 20 | +{ |
| 21 | + public void Build(IWorkflowBuilder<int, Order> builder) |
| 22 | + { |
| 23 | + builder |
| 24 | + .StartWith(CreateOrder) |
| 25 | + .Then(ValidateOrder) |
| 26 | + .Branch<Order>( |
| 27 | + order => Task.FromResult(order.TotalAmount > 1000), |
| 28 | + highValueBranch => Task.FromResult(highValueBranch |
| 29 | + .Then(ApplyDiscount) |
| 30 | + .Then(ProcessHighValueOrder)), |
| 31 | + lowValueBranch => Task.FromResult(lowValueBranch |
| 32 | + .Then(ProcessLowValueOrder)) |
| 33 | + ) |
| 34 | + .Then(ProcessPayment) |
| 35 | + .Retry(3, TimeSpan.FromSeconds(1)) |
| 36 | + .Catch<ProcessingException>(HandleProcessingException) |
| 37 | + .If( |
| 38 | + order => Task.FromResult(!order.IsProcessed), |
| 39 | + retryBranch => Task.FromResult(retryBranch.Then(RetryProcessing)) |
| 40 | + ) |
| 41 | + .While( |
| 42 | + order => Task.FromResult(!order.IsShipped), |
| 43 | + shippingBranch => Task.FromResult(shippingBranch.Then(AttemptShipping)) |
| 44 | + ) |
| 45 | + .Map(FinalizeOrder) |
| 46 | + .WithLogging(logger); |
| 47 | + } |
| 48 | + |
| 49 | + private Task<Result<Order>> CreateOrder(int orderId) |
| 50 | + { |
| 51 | + var order = new Order { Id = orderId, TotalAmount = new Random().Next(500, 2000) }; |
| 52 | + return Task.FromResult(Result<Order>.Success(order)); |
| 53 | + } |
| 54 | + |
| 55 | + private Task<Result<Order>> ValidateOrder(Order order) |
| 56 | + { |
| 57 | + if (order.TotalAmount <= 0) |
| 58 | + { |
| 59 | + return Task.FromResult(Result<Order>.Failure("Invalid order amount")); |
| 60 | + } |
| 61 | + return Task.FromResult(Result<Order>.Success(order)); |
| 62 | + } |
| 63 | + |
| 64 | + private Task<Result<Order>> ApplyDiscount(Order order) |
| 65 | + { |
| 66 | + order.TotalAmount *= 0.9m; |
| 67 | + return Task.FromResult(Result<Order>.Success(order)); |
| 68 | + } |
| 69 | + |
| 70 | + private async Task<Result<Order>> ProcessHighValueOrder(Order order) |
| 71 | + { |
| 72 | + // Simulate complex processing |
| 73 | + await Task.Delay(100); |
| 74 | + order.IsProcessed = true; |
| 75 | + return Result<Order>.Success(order); |
| 76 | + } |
| 77 | + |
| 78 | + private Task<Result<Order>> ProcessLowValueOrder(Order order) |
| 79 | + { |
| 80 | + // Simulate simple processing |
| 81 | + order.IsProcessed = true; |
| 82 | + return Task.FromResult(Result<Order>.Success(order)); |
| 83 | + } |
| 84 | + |
| 85 | + private Task<Result<Order>> ProcessPayment(Order order) |
| 86 | + { |
| 87 | + if (new Random().Next(0, 10) < 3) // 80% chance of failure |
| 88 | + { |
| 89 | + throw new ProcessingException("Payment processing failed"); |
| 90 | + } |
| 91 | + return Task.FromResult(Result<Order>.Success(order)); |
| 92 | + } |
| 93 | + |
| 94 | + private Task<Result<Order>> HandleProcessingException(ProcessingException ex) |
| 95 | + { |
| 96 | + return Task.FromResult(Result<Order>.Failure($"Handled processing exception: {ex.Message}")); |
| 97 | + } |
| 98 | + |
| 99 | + private Task<Result<Order>> RetryProcessing(Order order) |
| 100 | + { |
| 101 | + order.RetryCount++; |
| 102 | + order.IsProcessed = true; |
| 103 | + return Task.FromResult(Result<Order>.Success(order)); |
| 104 | + } |
| 105 | + |
| 106 | + private Task<Result<Order>> AttemptShipping(Order order) |
| 107 | + { |
| 108 | + if (new Random().Next(0, 10) < 8) // 80% chance of successful shipping |
| 109 | + { |
| 110 | + order.IsShipped = true; |
| 111 | + } |
| 112 | + return Task.FromResult(Result<Order>.Success(order)); |
| 113 | + } |
| 114 | + |
| 115 | + private Order FinalizeOrder(Order order) |
| 116 | + { |
| 117 | + order.IsProcessed = true; |
| 118 | + order.IsShipped = true; |
| 119 | + return order; |
| 120 | + } |
| 121 | +} |
0 commit comments