RRegistry 0.4.0

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

Rule Registry

A generic Rule Registry that allows storing and then looking for Rules & Values matching a given element.

Overview

This project provides a flexible and configurable registry system that can hold and select appropriate values based on defined rules. It is particularly useful for scenarios such as assigning taxes based on properties of elements like invoices or products.

Features

  • Generic Registry: Use with any types, conditions, and values.
  • Typed Registry Implementations: Create specific registry implementations with one or no generic values.
  • Priority Handling: Rules can be prioritized, and the registry will consider these priorities when matching rules.

Basic Usage

Defining a Rule

Define a condition:

static bool IsDomesticToyCondition(FooProduct element) => 
    element.Type == "TOY" && 
    element.CountryOfOrigin == "PL";

Define a rule, and value that should be assigned to matched elements:

var domesticToyRule = new Rule<FooProduct, decimal>()
{
    Name = "Domestic Toys",
    Condition = IsDomesticToyCondition,
    OutputValue = 0.05m
};

Creating a Registry

Create a registry instance with the given set of rules:

var taxRegistry = new BaseRegistry<FooProduct>(new[] { domesticToyRule });

Matching a Rule

Match a rule for a given element:

var product = new FooProduct
{  
    Name = "Radio controlled car",  
    Type = "TOY",  
    CountryOfOrigin = "PL"  
};  
var foundTaxRule = taxRegistry.MatchRule(product);
// returns: foundTaxRule => Rule "Domestic Toys" with OutputValue 0.05

Example

Here's a complete example demonstrating the basic usage of the Rule Registry:

using System;
using System.Collections.Generic;
using RRegistry;
using RRegistry.Samples.TaxRegistry;

public class FooProduct
{
    public string Name { get; set; }
    public string Type { get; set; }
    public string CountryOfOrigin { get; set; }
}

public class Program
{
    static bool IsDomesticToyCondition(FooProduct element) => 
        element.Type == "TOY" && 
        element.CountryOfOrigin == "PL";

    public static void Main()
    {
        var domesticToyRule = new Rule<FooProduct, decimal>()
        {
            Name = "Domestic Toys",
            Condition = IsDomesticToyCondition,
            OutputValue = 0.05m,
            Priority = 1
        };

        var taxRegistry = new TaxRegistry<FooProduct>(new[] { domesticToyRule });

        var product = new FooProduct
        {  
            Name = "Radio controlled car",  
            Type = "TOY",  
            CountryOfOrigin = "PL"  
        };  
        var foundTaxRule = taxRegistry.MatchRule(product);

        if (foundTaxRule != null)
        {
            Console.WriteLine($"Matched Rule: {foundTaxRule.Name}, Tax: {foundTaxRule.OutputValue}");
        }
        else
        {
            Console.WriteLine("No matching rule found.");
        }
    }
}

Advanced Usage

Matching Multiple Rules

You can also match multiple rules for a given element:

var matchedRules = taxRegistry.MatchRules(product);
foreach (var rule in matchedRules)
{
    Console.WriteLine($"Matched Rule: {rule.Name}, Tax: {rule.OutputValue}");
}

Matching Rules with Priority

The registry takes the priority of rules into consideration when matching:

var highPriorityRule = new Rule<FooProduct, decimal>()
{
    Name = "High Priority Rule",
    Condition = IsDomesticToyCondition,
    OutputValue = 0.10m,
    Priority = 2
};

var taxRegistry = new TaxRegistry<FooProduct>(new[] { domesticToyRule, highPriorityRule });

var foundTaxRule = taxRegistry.MatchRule(product);
// foundTaxRule => Rule "High Priority Rule" with OutputValue 0.10

Conclusion

The Rule Registry library provides a powerful and flexible way to define and match rules based on various conditions and priorities. It can be easily extended and customized to fit different use cases and requirements.

Product Compatible and additional computed target framework versions.
.NET 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 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.
  • net9.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
0.4.0 135 11/24/2024