Skip to content

kathelon/CSharpEssentials

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

194 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CSharpEssentials

CSharpEssentials

Build

Tests

pr-21 NuGet Downloads License: MIT .NET Packages

Modular .NET NuGet ecosystem that bridges OOP and Functional Programming in C#.

Packages

Package Description Docs NuGet
CSharpEssentials Meta-package: Results + Errors + Maybe + Any + Core + Enums πŸ“– NuGet
CSharpEssentials.Results Result<T> β€” railway-oriented error handling without exceptions πŸ“– NuGet
CSharpEssentials.Errors Structured Error values with type, code, description, metadata πŸ“– NuGet
CSharpEssentials.Maybe Maybe<T> β€” explicit optionals, no null reference exceptions πŸ“– NuGet
CSharpEssentials.Any Any<T1,T2,...> β€” type-safe discriminated unions πŸ“– NuGet
CSharpEssentials.Core String/GUID/collection utilities πŸ“– NuGet
CSharpEssentials.Enums [StringEnum] source generator β€” AOT-safe enum↔string πŸ“– NuGet
CSharpEssentials.Rules Composable rule engine with .And()/.Or()/.Linear()/.Next() πŸ“– NuGet
CSharpEssentials.Mediator CQRS pipeline behaviors: validation, logging, caching, transactions πŸ“– NuGet
CSharpEssentials.Entity EntityBase<TId>, soft deletion, domain events πŸ“– NuGet
CSharpEssentials.EntityFrameworkCore EF Core interceptors (audit, events, slow queries) + pagination πŸ“– NuGet
CSharpEssentials.AspNetCore GlobalExceptionHandler, ResultEndpointFilter, Swagger versioning πŸ“– NuGet
CSharpEssentials.Http HttpClient extensions returning Result<T> πŸ“– NuGet
CSharpEssentials.Json Pre-configured System.Text.Json options and converters πŸ“– NuGet
CSharpEssentials.RequestResponseLogging Request/response body logging middleware πŸ“– NuGet
CSharpEssentials.GcpSecretManager GCP Secret Manager β†’ IConfiguration provider πŸ“– NuGet
CSharpEssentials.Time Testable IDateTimeProvider wrapping TimeProvider πŸ“– NuGet
CSharpEssentials.Validation High-performance model-first validation with Result<T> integration πŸ“– NuGet
CSharpEssentials.Clone ICloneable<T> β€” typed deep-copy for entity collections πŸ“– NuGet

Documentation

API Reference β€” Complete guide to every package, method, and pattern in the ecosystem. Covers all 19 packages with method tables, philosophy, code examples, and cross-cutting FP patterns.

Installation

# Core functional modules
dotnet add package CSharpEssentials

# Individual packages
dotnet add package CSharpEssentials.Rules
dotnet add package CSharpEssentials.Mediator

Core Concepts

Result<T> β€” Railway-Oriented Error Handling

Stop throwing exceptions for expected failures. Chain operations that short-circuit on the first error.

// ❌ Traditional: exceptions as control flow
public User GetUser(Guid id)
{
    var user = _db.Find(id);
    if (user == null) throw new NotFoundException("User not found");
    return user;
}

// βœ… Result<T>: explicit, composable error handling
public Result<User> GetUser(Guid id)
{
    var user = _db.Find(id);
    if (user is null) return Error.NotFound("User not found");
    return Result.Success(user);
}

// Chain operations β€” stops at first failure, no try/catch needed
Result<UserDto> result = GetUser(id)
    .Bind(user => ValidateAge(user))
    .Bind(user => CheckPermissions(user))
    .Map(user => new UserDto(user));

Maybe<T> β€” Explicit Optionals

Model the absence of a value without null references.

// ❌ Null checks everywhere, easy to miss
string display = user?.Profile?.DisplayName?.Trim()?.ToUpper() ?? "ANONYMOUS";

// βœ… Maybe<T>: pipeline that handles absence at every step
string display = Maybe.Return(user)
    .Bind(u  => Maybe.Return(u.Profile))
    .Bind(p  => Maybe.Return(p.DisplayName))
    .Map(n   => n.Trim().ToUpper())
    .Fallback("ANONYMOUS");

Any<T1,T2,...> β€” Discriminated Unions

A value that is exactly one of several types β€” exhaustively matched at compile time.

Any<Circle, Rectangle, Triangle> shape = new Circle(radius: 5);

double area = shape.Match(
    circle    => Math.PI * circle.Radius * circle.Radius,
    rectangle => rectangle.Width * rectangle.Height,
    triangle  => 0.5 * triangle.Base * triangle.Height
    // compiler error if any case is missing
);

Rules β€” Composable Validation

Build complex validation logic from simple, reusable rules that return Result<T>.

var minAge     = Rule.Create<User>(u => u.Age >= 18,          Error.Validation("Must be 18+"));
var validEmail = Rule.Create<User>(u => u.Email.Contains('@'), Error.Validation("Invalid email"));
var verified   = Rule.Create<User>(u => u.IsVerified,          Error.Validation("Account not verified"));

// AND β€” all rules must pass, collects every failure
Result<User> result = RuleEngine
    .Create(minAge, validEmail, verified)
    .Evaluate(user);

// OR β€” at least one rule must pass
Result<User> adminOrMod = RuleEngine
    .Create(isAdmin, isModerator)
    .Or()
    .Evaluate(user);

AspNetCore β€” Result β†’ HTTP, Automatically

Map Result<T> to correct HTTP status codes without writing any error-mapping boilerplate.

// Program.cs
app.AddCSharpEssentials(); // registers GlobalExceptionHandler + ProblemDetails

// Controller β€” business logic returns Result<T>, HTTP mapping is automatic
[HttpGet("{id:guid}")]
public IActionResult GetProduct(Guid id)
    => _productService.GetProduct(id).Match(
        onSuccess: product => Ok(product),
        onFailure: errors  => errors.ToActionResult() // 404 / 400 / 409 based on ErrorType
    );

// Service β€” chain multiple steps, first failure short-circuits
public Result<Order> PlaceOrder(Guid productId, int quantity)
    => _productService.GetProduct(productId)
        .Then(p => ValidateStock(p, quantity))
        .Then(p => ReserveStock(p, quantity))
        .Then(p => CreateOrder(p, quantity));

AI Skills

Install skills for Claude Code, Cursor, Codex, and 50+ other AI agents:

npx skills add senrecep/CSharpEssentials

Each package has a dedicated skill with accurate API examples, correct namespaces, and common pitfalls.

About

Modular .NET NuGet ecosystem that bridges OOP and Functional Programming in C#.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • C# 92.5%
  • HTML 5.9%
  • Shell 1.2%
  • Python 0.4%