Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wisedev-code/workflowguard.net

WorkflowGuard is a .NET library that provides an easy-to-use attribute-based mechanism for applying resilient policies to method executions.
https://github.com/wisedev-code/workflowguard.net

csharp dotnet nuget open-source retry-strategies validation

Last synced: 2 months ago
JSON representation

WorkflowGuard is a .NET library that provides an easy-to-use attribute-based mechanism for applying resilient policies to method executions.

Awesome Lists containing this project

README

        


# WorkflowGuard

**WorkflowGuard** is a .NET library that provides an easy-to-use mechanism for applying resilient policies to method executions. It works similar to `Polly`, but offers simplistic way to support retry, fallback, and circuit breaker policies, enabling developers to ensure their applications are more resilient to transient faults and outages.

## Features

- **Attribute-Based Configuration:** Apply retry, fallback, and circuit breaker policies using custom attributes.
- **Dynamic Proxies:** Automatically intercept method calls to apply policies without modifying business logic.
- **Seamless Integration:** Integrates smoothly with ASP.NET Core dependency injection and minimal APIs.

## Usage

### Attribute-Based Example

1. **Define your service interface and implementation:**

```csharp
public interface IService
{
[WorkflowGuard(retryPolicy: "DefaultRetryPolicy", fallbackPolicy: "DefaultFallbackPolicy", circuitBreakerPolicy: "DefaultCircuitBreakerPolicy")]
Task GetCustomerAsync(int id);

[WorkflowGuard(retryPolicy: "DefaultRetryPolicy", fallbackPolicy: "DefaultFallbackPolicy", circuitBreakerPolicy: "DefaultCircuitBreakerPolicy")]
Task CreateOrderAsync(Order order);
}

public class Service : IService
{
public async Task GetCustomerAsync(int id)
{
// Implementation
}

public async Task CreateOrderAsync(Order order)
{
// Implementation
}
}
```

2. **Register your services and apply the `AddWorkflowAttributes` extension in your startup configuration:**

```csharp
var builder = WebApplication.CreateBuilder(args);

// Configure WorkflowGuard
builder.Services.AddSingleton(sp => new WorkflowGuard(
new Dictionary
{
["DefaultRetryPolicy"] = new RetryPolicy(3, TimeSpan.FromSeconds(2))
},
new Dictionary
{
["DefaultFallbackPolicy"] = new FallbackPolicy(() => Task.FromResult(new { Message = "Fallback result" }))
},
new Dictionary
{
["DefaultCircuitBreakerPolicy"] = new CircuitBreakerPolicy(3, TimeSpan.FromSeconds(30))
}
));

// Register your services
builder.Services.AddSingleton();

// Apply workflow attributes
builder.Services.AddWorkflowAttributes();

var app = builder.Build();

app.MapGet("/customer/{id}", async (int id, IService service) =>
{
var customer = await service.GetCustomerAsync(id);
return Results.Ok(customer);
});

app.MapPost("/order", async (Order order, IService service) =>
{
var createdOrder = await service.CreateOrderAsync(order);
return Results.Ok(createdOrder);
});

app.Run();
```

### Direct Usage Example (without Attributes)

**Manually apply policies using `IWorkflowGuard` in your API endpoints:**

app.MapGet("/customer/{id}", async (int id, IService service, IWorkflowGuard workflowGuard) =>
{
var customer = await workflowGuard.ExecuteAsync(
() => service.GetCustomerAsync(id),
"DefaultRetryPolicy",
"DefaultFallbackPolicy",
"DefaultCircuitBreakerPolicy"
);
return Results.Ok(customer);
});

app.MapPost("/order", async (Order order, IService service, IWorkflowGuard workflowGuard) =>
{
var createdOrder = await workflowGuard.ExecuteAsync(
() => service.CreateOrderAsync(order),
"DefaultRetryPolicy",
"DefaultFallbackPolicy",
"DefaultCircuitBreakerPolicy"
);
return Results.Ok(createdOrder);
});

app.Run();
## Installation

```bash
dotnet add package WorkflowGuard