Masterly.BusinessRules 3.0.1

dotnet add package Masterly.BusinessRules --version 3.0.1
                    
NuGet\Install-Package Masterly.BusinessRules -Version 3.0.1
                    
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="Masterly.BusinessRules" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Masterly.BusinessRules" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Masterly.BusinessRules" />
                    
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 Masterly.BusinessRules --version 3.0.1
                    
#r "nuget: Masterly.BusinessRules, 3.0.1"
                    
#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 Masterly.BusinessRules@3.0.1
                    
#: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=Masterly.BusinessRules&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Masterly.BusinessRules&version=3.0.1
                    
Install as a Cake Tool

Masterly.BusinessRules

A composable business rule engine for .NET.

Masterly.BusinessRules

Nuget Nuget GitHub last commit GitHub Build CodeQL Analysis Publish to NuGet Docs

Full documentation: a7mdfre7at.github.io/Masterly.BusinessRules

Overview

Masterly.BusinessRules is a business rule engine for .NET. You write each rule, such as "an order must contain at least one item", as a small named unit with a code and a message. You then check one rule or many, and you get back a clear list of every rule that failed.

Every feature works with both synchronous and asynchronous rules. The core package has no dependencies. Five optional packages add text expressions, logging and tracing, dependency injection, feature management and test tools.

What you can do


Installation

Install the core package first. Then add only the companion packages you need. All six packages ship together with the same version number.

Package What it is for Target framework Extra dependencies
Masterly.BusinessRules The core: rules, builders, composition, batch checking, context, caching, feature gates, parameters and specifications netstandard2.1 None
Masterly.BusinessRules.Expressions Rule conditions written as text, with explanations that show the bound values netstandard2.1 None
Masterly.BusinessRules.Diagnostics Structured ILogger logging, OpenTelemetry spans and metrics for rule evaluation netstandard2.1 Microsoft.Extensions.Logging.Abstractions 10.0.14, System.Diagnostics.DiagnosticSource 10.0.14
Masterly.BusinessRules.DependencyInjection Assembly scanning, injectable rule runners and per-tenant rule sets netstandard2.1 Microsoft.Extensions.DependencyInjection.Abstractions 10.0.14
Masterly.BusinessRules.FeatureManagement Drives feature-gated rules from Microsoft.FeatureManagement flags and variants netstandard2.1 Microsoft.FeatureManagement 4.8.0
Masterly.BusinessRules.Testing Outcome coverage, boundary finder, property checks and mutants for your test projects net8.0, net10.0 CsCheck 4.9.1 on .NET 8 and 9, 5.0.0 on .NET 10

Every companion package also depends on the core package. The versions shown are the minimum versions each package references.

dotnet add package Masterly.BusinessRules
dotnet add package Masterly.BusinessRules.Expressions
dotnet add package Masterly.BusinessRules.Diagnostics
dotnet add package Masterly.BusinessRules.DependencyInjection
dotnet add package Masterly.BusinessRules.FeatureManagement
dotnet add package Masterly.BusinessRules.Testing

Or, in the Visual Studio Package Manager Console, run Install-Package Masterly.BusinessRules, and the same for each companion package you need.

Note: netstandard2.1 runs on .NET Core 3.0 or later and on .NET 5 or later. It does not run on .NET Framework. The Testing package needs a test project that targets .NET 8 or later.


Quick Start

This example defines two rules for an order, checks one of them, and then checks both together.

1. Define a rule. Derive from BaseBusinessRule and supply a code, a message and the condition. A rule is broken when IsBroken() returns true. Write the condition that describes the violation, not the valid state.

using Masterly.BusinessRules;

// Order and Customer are your own domain classes.
// Here Order.Items is a List<OrderLine> and Customer.IsActive is a bool.
public sealed class OrderMustHaveItemsRule : BaseBusinessRule
{
    private readonly Order _order;

    public OrderMustHaveItemsRule(Order order)
    {
        _order = order;
    }

    public override string Code => "ORDER.NO_ITEMS";

    public override string Message => "An order must contain at least one item.";

    public override bool IsBroken() => _order.Items.Count == 0;
}

public sealed class CustomerMustBeActiveRule : BaseBusinessRule
{
    private readonly Customer _customer;

    public CustomerMustBeActiveRule(Customer customer)
    {
        _customer = customer;
    }

    public override string Code => "CUSTOMER.INACTIVE";

    public override string Message => "The customer account must be active.";

    public override bool IsBroken() => !_customer.IsActive;
}

2. Check one rule. IsBroken() answers the question. Check() throws a BusinessRuleValidationException when the rule is broken and does nothing when it passes.

// order comes from your application
OrderMustHaveItemsRule rule = new OrderMustHaveItemsRule(order);

bool isBroken = rule.IsBroken();

rule.Check();

3. Check several rules. BusinessRuleChecker.CheckAll evaluates every rule you pass. If any are broken, it throws one exception that lists all of them.

// order and customer come from your application
try
{
    BusinessRuleChecker.CheckAll(
        new OrderMustHaveItemsRule(order),
        new CustomerMustBeActiveRule(customer));
}
catch (BusinessRuleValidationException exception)
{
    foreach (BusinessRuleResult broken in exception.BrokenRules)
    {
        Console.WriteLine($"{broken.Code} ({broken.Severity}): {broken.Message}");
    }
}

With an empty order and an inactive customer, this prints:

ORDER.NO_ITEMS (Error): An order must contain at least one item.
CUSTOMER.INACTIVE (Error): The customer account must be active.

Next steps


Documentation

Topic Guide
Getting started Overview, Installation, Quick start
Writing rules Rule classes and context, Builders, Subject-typed rules, Combining rules
Running rules Running many rules, Understanding failures, Conditional and cached rules
Advanced topics Feature flags and shadow rollout, Per-tenant rules and parameters, Specifications, Rule expressions
Integrations Logging, tracing and metrics, Dependency injection, Testing your rules
Examples Real-world examples
Reference API reference, Upgrading from 2.x, Changelog

Upgrading from 2.x? Most code compiles against 3.0 without changes, but 3.0 is a binary break: rebuild every assembly that references Masterly.BusinessRules. A few behaviours changed too. See Upgrading from 2.x.

If this library helps you, please give it a star on GitHub.


License

MIT - use it freely in your projects. See LICENSE.txt.

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 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. 
.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.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Masterly.BusinessRules:

Package Downloads
Masterly.BusinessRules.Diagnostics

Structured ILogger observer plus OpenTelemetry ActivitySource spans and Meter instruments for Masterly.BusinessRules rule evaluation.

Masterly.BusinessRules.Expressions

Rule expression DSL for Masterly.BusinessRules: parse a textual condition once, evaluate it against the rule context and explain the verdict with bound values.

Masterly.BusinessRules.Testing

Testing utilities for Masterly.BusinessRules: outcome coverage, boundary finder and CsCheck-backed rule properties.

Masterly.BusinessRules.FeatureManagement

Microsoft.FeatureManagement adapter supplying Masterly.BusinessRules feature-gated rules with their rollout state.

Masterly.BusinessRules.DependencyInjection

Microsoft.Extensions.DependencyInjection integration for Masterly.BusinessRules: assembly scanning, injectable rule runners and the scoped rule registry.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 242 11/2/2026
3.0.0 246 10/28/2026
2.0.0 657 12/31/2025
1.0.0 252 5/19/2025