← Back to Documentation Index

Getting Started

Installation

dotnet add package RoslynRules --version 1.0.0

Or reference the project directly:

<ProjectReference Include="..\RoslynRules\RoslynRules.csproj" />

Your First Rule

1. Define a Model

public class Customer
{
    public string Name { get; set; } = "";
    public int Age { get; set; }
    public bool IsAdult { get; set; }
}

2. Author a Rule

A Rule is authored on its own, but it is not an executable unit by itself. You never compile or run a rule directly — it must be wrapped in a workflow, which owns the compiler and drives execution.

using RoslynRules.Models;

var adultRule = new Rule
{
    Description = "Check if customer is an adult",
    Expression = "customer.Age >= 18",
    Action = "customer.IsAdult = true",
    IsActive = true
};

3. Wrap the Rule in a Workflow

The workflow is the unit that compiles and executes rules.

var workflow = new Workflow
{
    Description = "Customer validation",
    Rules = new List<Rule> { adultRule }
};

4. Define Parameters

For compilation, only the parameter type and name are needed.

var compileParam = RuleParameter.ForCompile("customer", typeof(Customer));

For execution, pass a parameter with a real value:

var customer = new Customer { Name = "Alice", Age = 25 };

var executeParam = RuleParameter.ForExecute("customer", typeof(Customer), customer);

Wrapping Multiple Values

A rule takes exactly one parameter. When a rule needs several values, wrap them in a small struct or class and pass one RuleParameter of that type; the expression references the wrapper’s members. Wrap the rule in a workflow and let the workflow compile it.

// One type holds the former separate values
public record Order(decimal Price, int Quantity);

var workflow = new Workflow
{
    Rules = new List<Rule>
    {
        new Rule
        {
            Description = "Price validation",
            Expression = "order.Price > 0 && order.Quantity > 0"
        }
    }
};

// A single parameter: the Order wrapper
workflow.Compile(RuleParameter.ForCompile("order", typeof(Order)));

5. Validate, Compile, Execute

// Catch errors before compiling
workflow.Validate();

// Compile once — type and name only
workflow.Compile(compileParam);

// Execute many times with different values
var results = workflow.Execute(executeParam);

foreach (var result in results)
{
    Console.WriteLine($"Success: {result.Success}");
}

Key point: Compile with ForCompile() (types only). Execute with ForExecute() (real values). Separate compilation from execution.

Next Steps


Back to top

MIT License. Built with Roslyn + Typed Delegates.