Rulify.Net 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rulify.Net --version 1.1.0
                    
NuGet\Install-Package Rulify.Net -Version 1.1.0
                    
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="Rulify.Net" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rulify.Net" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Rulify.Net" />
                    
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 Rulify.Net --version 1.1.0
                    
#r "nuget: Rulify.Net, 1.1.0"
                    
#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 Rulify.Net@1.1.0
                    
#: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=Rulify.Net&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Rulify.Net&version=1.1.0
                    
Install as a Cake Tool

Rulify.Net

A powerful and flexible rule engine for .NET applications. Rulify.Net provides a comprehensive solution for implementing business rules, validation logic, and decision-making processes in your applications.

NuGet License: MIT .NET

Features

  • Dynamic Rule Definition: Create rules using lambda expressions and delegates
  • Priority-Based Execution: Execute rules in order of priority
  • Async Support: Full support for asynchronous rule evaluation
  • Collection Evaluation: Evaluate rules against collections of data
  • Event Hooks: Monitor rule execution with events
  • Thread-Safe: Safe for concurrent operations
  • Dependency Injection Ready: Easy integration with DI containers

Installation

dotnet add package Rulify.Net

Quick Start

Basic Usage

using Rulify.Net;

// Create a rule engine
var engine = new RuleEngine<string>();

// Add a rule
engine.AddRule(new DelegateRule<string>(
    "length-check", 
    "Length Check", 
    context => context.Length > 5 ? RuleResult.Success() : RuleResult.Failure("Too short")
));

// Evaluate a context
var results = engine.Evaluate("Hello World");
foreach (var result in results)
{
    Console.WriteLine($"Rule: {result.IsSuccess}, Message: {result.ErrorMessage}");
}

Async Rules

var asyncRule = new DelegateRule<string>(
    "async-check",
    "Async Check",
    context => RuleResult.Success(), // Sync fallback
    async context => 
    {
        await Task.Delay(100); // Simulate async work
        return RuleResult.Success();
    }
);

engine.AddRule(asyncRule);
var results = await engine.EvaluateAsync("test");

Collection Evaluation

var contexts = new[] { "short", "medium length", "very long context" };
var results = engine.EvaluateCollection(contexts);

foreach (var kvp in results)
{
    Console.WriteLine($"Context: {kvp.Key}");
    foreach (var result in kvp.Value)
    {
        Console.WriteLine($"  Result: {result.IsSuccess}");
    }
}

Event Monitoring

engine.RuleEvaluating += (sender, args) =>
{
    Console.WriteLine($"Evaluating rule: {args.Rule.Name}");
};

engine.RuleEvaluated += (sender, args) =>
{
    Console.WriteLine($"Rule {args.Rule.Name} completed: {args.Result?.IsSuccess}");
};

API Reference

IRuleEngine<TContext>

Main interface for the rule engine.

  • AddRule(IRule<TContext> rule): Add a rule to the engine
  • RemoveRule(string ruleId): Remove a rule by ID
  • GetRules(): Get all rules ordered by priority
  • Evaluate(TContext context): Evaluate all rules against a context
  • EvaluateAsync(TContext context): Evaluate all rules asynchronously
  • EvaluateCollection(IEnumerable<TContext> contexts): Evaluate against multiple contexts
  • ClearRules(): Remove all rules

IRule<TContext>

Interface for individual rules.

  • Id: Unique identifier
  • Name: Human-readable name
  • Description: Rule description
  • Priority: Execution priority (higher = first)
  • IsAsync: Whether the rule supports async evaluation
  • Evaluate(TContext context): Synchronous evaluation
  • EvaluateAsync(TContext context): Asynchronous evaluation

RuleResult

Result of rule evaluation.

  • IsSuccess: Whether the rule passed
  • ErrorMessage: Error message if failed
  • Data: Additional data returned by the rule
  • ExecutionTime: Time taken to execute the rule

Advanced Usage

Custom Rule Implementation

public class CustomRule : RuleBase<MyContext>
{
    public CustomRule() : base("custom-id", "Custom Rule", "Description", priority: 10)
    {
    }

    public override RuleResult Evaluate(MyContext context)
    {
        // Custom logic here
        return context.IsValid ? RuleResult.Success() : RuleResult.Failure("Invalid context");
    }
}

Priority-Based Execution

Rules are executed in order of priority (highest first). Rules with the same priority are executed in the order they were added.

engine.AddRule(new DelegateRule<string>("rule1", "Rule 1", context => RuleResult.Success(), priority: 1));
engine.AddRule(new DelegateRule<string>("rule2", "Rule 2", context => RuleResult.Success(), priority: 10));
// Rule 2 will execute before Rule 1

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Ertugrul Kara

License

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

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 148 12/12/2025
1.1.0 199 10/27/2025
1.0.0 167 10/25/2025