DarkPeak.Functional
A functional programming library for .NET providing monadic types and railway-oriented programming patterns.
Core Types
- Option<T> — Eliminates null reference exceptions with explicit presence/absence
- Result<T, TError> — Railway-oriented error handling without exceptions
- Either<TLeft, TRight> — Symmetric dual-value type for branching logic
- OneOf<T1, ..., Tn> — Discriminated unions with 2-8 explicitly typed cases
- Validation<T, TError> — Accumulates multiple errors instead of short-circuiting
Resilience & Caching
- Retry — Configurable retry policies with backoff strategies
- Circuit Breaker — Prevents cascading failures by short-circuiting requests to failing dependencies
- Memoize — Function caching with TTL, LRU eviction, and distributed cache support
Integration Packages
- DarkPeak.Functional.Http — Wraps
HttpClientinResult<T, Error>for exception-free HTTP communication - DarkPeak.Functional.AspNet — Converts
Result<T, Error>toIResultandProblemDetailsfor minimal APIs - DarkPeak.Functional.Redis — Redis distributed cache provider for
MemoizeandMemoizeResult - DarkPeak.Functional.Dapper — Wraps Dapper queries and commands in
Result<T, Error>with typed database error mapping - DarkPeak.Functional.EntityFramework — Wraps EF Core operations in
Result<T, Error>with typed error handling - DarkPeak.Functional.HealthChecks — Exposes circuit breaker and cache provider status via ASP.NET Core
IHealthCheckimplementations - DarkPeak.Functional.Mediator — Integrates
Result<T, Error>with Mediator for source-generated CQRS pipelines
Quick Start
using DarkPeak.Functional;
// Option — no more nulls
Option<string> name = Option.Some("Alice");
var greeting = name.Map(n => $"Hello, {n}!");
// Result — railway-oriented programming
Result<int, ValidationError> parsed = Result.Success<int, ValidationError>(42);
var doubled = parsed.Map(x => x * 2);
// OneOf — multiple valid shapes
OneOf<string, int, bool> response = true;
var description = response.Match(
text => text,
number => number.ToString(),
flag => flag ? "enabled" : "disabled");
// Fluent chaining
var result = await Option.Some("42")
.ToResult(new ValidationError { Message = "Missing" })
.Map(int.Parse)
.Map(x => x * 2)
.Match(
success: x => $"Result: {x}",
failure: e => $"Error: {e.Message}");
Installation
dotnet add package DarkPeak.Functional
Learn More
- Getting Started — Installation, concepts, and first steps
- OneOf — Discriminated unions with 2-8 cases and
Eitherinterop - Orchestration — Combining Validation and Result pipelines in real-world scenarios
- Example: Blazor Form — Using Validation in a Blazor component with inline field errors
- API Reference — Full API documentation generated from source