CSharpEssentials.Rules 3.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpEssentials.Rules --version 3.0.2
                    
NuGet\Install-Package CSharpEssentials.Rules -Version 3.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpEssentials.Rules" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Rules" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Rules" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.Rules --version 3.0.2
                    
#r "nuget: CSharpEssentials.Rules, 3.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSharpEssentials.Rules@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.Rules&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Rules&version=3.0.2
                    
Install as a Cake Tool

CSharpEssentials.Rules

CSharpEssentials.Rules is a composable, functional rule engine for .NET. It allows you to define business logic as small, reusable rules and combine them into complex workflows using a fluent API.

🚀 Features

  • Composable Logic: Build complex logic from simple, single-responsibility rules.
  • Flow Control: Support for Linear (sequences), Conditional (If/Else), And, and Or rule combinations.
  • Async Support: First-class support for asynchronous rules.
  • Result Pattern: Built on top of CSharpEssentials.Results to return success/failure status.
  • Context-Driven: Rules operate on a shared TContext state.

📦 Installation

dotnet add package CSharpEssentials.Rules

🛠 Usage

1. Defining Rules

You can implement the IRule<TContext> interface or use functional adapters.

Using Interface:

using CSharpEssentials.Rules;
using CSharpEssentials.ResultPattern;

public class UserContext 
{ 
    public int Age { get; set; }
    public bool HasLicense { get; set; }
}

public class AgeRule : IRule<UserContext>
{
    public Result Evaluate(UserContext context, CancellationToken ct = default)
    {
        if (context.Age < 18)
            return Result.Failure("User.Underage", "User must be at least 18.");
        return Result.Success();
    }
}

Using Func:

Func<UserContext, Result> licenseCheck = ctx => 
    ctx.HasLicense 
        ? Result.Success() 
        : Result.Failure("User.NoLicense", "User must have a license.");

var rule = licenseCheck.ToRule();

2. Executing Rules

Use the RuleEngine to evaluate a rule against a context.

var context = new UserContext { Age = 20, HasLicense = true };
var rule = new AgeRule();

Result result = RuleEngine.Evaluate(rule, context);

if (result.IsSuccess)
{
    Console.WriteLine("Rule passed!");
}

3. Composing Rules (Linear)

Run rules in a sequence. If one fails, execution stops.

var complexRule = Rule.Linear(new AgeRule())
                      .Next(licenseCheck.ToRule());

Result result = RuleEngine.Evaluate(complexRule, context);

4. Conditional Rules

Execute different rules based on the result of a condition.

var workflow = Rule.If(new AgeRule(),
    then: new GrantAccessRule(),
    @else: new DenyAccessRule());

5. And / Or Logic

Combine multiple rules.

  • And: All rules must pass.
  • Or: At least one rule must pass.
var combined = Rule.And(
    new AgeRule(),
    new CitizenshipRule()
);

var alternative = Rule.Or(
    new PassportRule(),
    new DriverLicenseRule()
);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CSharpEssentials.Rules:

Package Downloads
CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.5 121 5/7/2026
3.0.4 114 5/6/2026
3.0.3 120 5/5/2026
3.0.2 130 5/5/2026
3.0.1 134 5/3/2026
3.0.0 125 5/3/2026
2.1.0 298 11/26/2025
2.0.9 247 9/30/2025
2.0.8 219 9/29/2025
2.0.7 223 9/29/2025
2.0.6 222 9/29/2025
2.0.5 219 9/29/2025
2.0.4 220 9/28/2025
2.0.3 235 9/28/2025
2.0.2 230 9/28/2025
2.0.1 225 9/28/2025
2.0.0 221 9/28/2025