Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Corsinvest.Fx

Modern Patterns and High-Level Features for C# - Bringing the best from Go, Rust, F#, and Swift

.NET License

Corsinvest.Fx is a collection of independent NuGet packages that bring modern programming patterns and high-level features to C#. Each package is designed to be used standalone or combined with others for maximum flexibility.


🎯 Philosophy

Corsinvest.Fx is NOT just another FP library.

It's a pragmatic suite of modern patterns and high-level features that C# lacks natively, inspired by languages like Go, Rust, F#, and Swift.

Our Principles

Principle What It Means
Pragmatism Solve real problems, not academic exercises
Safety Catch errors at compile-time, not runtime
Readability Clear, elegant code without excessive complexity
ModernitΓ  Best practices from modern languages

What We Include

βœ… Functional Patterns (when useful)
   └─ ResultOf<T,E>, Option<T>, Union Types
   └─ Railway-oriented programming
   └─ Data transformation pipelines

βœ… Modern Language Features
   └─ defer (Go-style resource cleanup)
   └─ Inline assembly (Rust/Zig-style performance - experimental)

Our Motto

"Solve real problems elegantly, don't chase theoretical perfection"


🌟 Why Choose Corsinvest.Fx?

βœ… Use Corsinvest.Fx When You Want

  • Type-safe error handling without exceptions (ResultOf<T,E>, Option<T>)
  • Discriminated unions with pattern matching ([Union] attribute)
  • Go-style resource cleanup (defer)
  • Data transformation pipelines (Pipe extensions)
  • Gradual adoption in existing C# codebases
  • Minimal learning curve for your team

❌ Don't Use Corsinvest.Fx If

Scenario Why Not Use Instead
Pure FP needed Need HKT, Free monads, lenses, etc. LanguageExt, F#
Simple scripts One-off < 100 lines, no maintenance Plain C#
Team not ready Unfamiliar with Result/Option, unwilling to learn Traditional C# patterns
Batteries-included FP Need complete FP ecosystem (HTTP, DB, etc.) LanguageExt + ecosystem

πŸ“¦ Packages

Core Packages

Package Description Status
Corsinvest.Fx.Functional ResultOf<T,E>, Option<T>, [Union] attribute. Railway-oriented programming, pattern matching, LINQ support. βœ… Stable
Corsinvest.Fx.Defer Go-style defer statements for automatic cleanup on scope exit. βœ… Stable

Experimental Packages

Package Description Status
Corsinvest.Fx.Comptime Zig-style compile-time computation using source generators. πŸ§ͺ Experimental
Corsinvest.Fx.Unsafe Inline assembly wrappers and unsafe operations for performance-critical code. πŸ§ͺ Experimental

πŸš€ Quick Start

Installation

Install individual packages via NuGet:

# Functional programming (Result, Option, Union)
dotnet add package Corsinvest.Fx.Functional

# Go-style defer
dotnet add package Corsinvest.Fx.Defer

Quick Examples

1. Type-Safe Error Handling (Functional)

using Corsinvest.Fx.Functional;

var result = ValidateEmail(email)
    .Bind(SaveToDatabase);

result.Match(
    ok => Console.WriteLine("Success!"),
    error => Console.WriteLine($"Error: {error}")
);

2. Automatic Cleanup (Defer)

using static Corsinvest.Fx.Defer.Defer;

var file = File.Open(path, FileMode.Open);
using var _ = defer(() => file.Close());
// File closes automatically

Experimental packages: Unsafe (inline assembly), Comptime (compile-time computation)

πŸ“– See individual package READMEs for complete documentation:


πŸ“š Explore Real-World Examples

The examples/ folder contains practical, runnable code demonstrating all features:

Core Examples

Advanced Examples

Run all examples:

dotnet run --project examples/Corsinvest.Fx.Examples.csproj

πŸ’‘ Feature Highlights

ResultOf - Type-Safe Error Handling

Railway-oriented programming without exceptions:

var result = ValidateEmail(email)
    .Bind(e => ValidateName(name))
    .Bind(n => ValidateAge(age))
    .Map(data => new User(data.Email, data.Name, data.Age))
    .Bind(user => SaveToDatabase(user));

result.Match(
    ok => Console.WriteLine($"Success: {ok.Value.Id}"),
    error => Console.WriteLine($"Error: {error.ErrorValue}")
);

Union Types - Discriminated Unions

Pattern matching for different states:

[Union]
public partial record PaymentMethod
{
    public partial record CreditCard(string Number, string ExpiryDate);
    public partial record PayPal(string Email);
    public partial record BankTransfer(string Iban, string Bic);
}

decimal CalculateFee(PaymentMethod payment) => payment.Match(
    creditCard => 2.5m,
    payPal => 1.5m,
    bankTransfer => 0.0m
);

Option - Null Safety

Eliminate null reference exceptions:

Option<User> FindUser(int id) =>
    users.ContainsKey(id)
        ? Option.Some(users[id])
        : Option.None<User>();

var userName = FindUser(42)
    .Map(u => u.Name)
    .GetValueOr("Guest");

Defer - Automatic Cleanup

Go-style resource management:

using static Corsinvest.Fx.Defer.Defer;

void ProcessFile(string path)
{
    var file = File.Open(path, FileMode.Open);
    using var _ = defer(() => file.Close());

    // File automatically closed on scope exit (even on exception)
    ProcessData(file);
}

πŸ”§ Troubleshooting

For common issues and solutions, please refer to the Troubleshooting section in the README of the specific package you are using:

If you still need help:

  1. Check the examples/ folder for similar use cases.
  2. Search existing issues.
  3. Open a new issue with a minimal reproducible code sample.

πŸ§ͺ Building and Testing

# Restore dependencies
dotnet restore

# Build all projects
dotnet build

# Run all tests
dotnet test

# Run tests with coverage
pwsh tests/RunTestsAndCoverage.ps1

Quality Metrics:

  • βœ… Comprehensive test suite with high coverage
  • βœ… Clean build without warnings or errors
  • βœ… Multiple real-world examples

πŸ“– Documentation

Each package has its own detailed README:


🀝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ”— Links


πŸ™ Acknowledgments

Inspired by functional programming languages and modern systems languages:

  • F# - Discriminated unions and Result types
  • Rust - Result/Option types and pattern matching
  • Go - Defer statement for resource cleanup
  • Zig - Compile-time execution philosophy
  • Swift - Union types and modern syntax

Made with ❀️ by Corsinvest

About

A pragmatic C# toolkit for modern features and safer code, inspired by Go, Rust, Zig, and F#

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages