← 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 compileParams = new[]
{
    RuleParameter.ForCompile("customer", typeof(Customer))
};

For execution, pass a parameter with a real value:

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

var executeParams = new[]
{
    RuleParameter.ForExecute("customer", typeof(Customer), customer)
};

Multiple Parameters

Rules can accept multiple parameters directly — up to 16. Wrap the rule in a workflow and let the workflow compile it.

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

var parameters = new[]
{
    RuleParameter.ForCompile("price", typeof(decimal)),
    RuleParameter.ForCompile("quantity", typeof(int))
};

workflow.Compile(parameters);

5. Validate, Compile, Execute

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

// Compile once — types and names only
workflow.Compile(compileParams);

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

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.