Tip Com.RickSeven.FormulaEngine.Core - A powerful .NET library for formula evaluation

rickseven

Member
Joined
Apr 17, 2025
Messages
6
Location
Indonesia
Programming Experience
10+
A powerful and flexible formula evaluation engine using Roslyn Scripting with dynamic runtime context. Ideal for scenarios where business logic needs to be defined at runtime through C# expressions or formulas.

🚀 Features
  • Evaluate C# expressions dynamically at runtime
  • Support for nested objects and complex data structures
  • JSON context support for easy integration
  • Rich set of operations:
    • Numeric operations (arithmetic, Math functions)
    • String manipulation and formatting
    • DateTime operations
    • Logical operations and conditionals
    • Collection handling with indexing
  • Culture-specific formatting support
  • Comprehensive error handling
  • No external dependencies other than Roslyn and BCL

📦 Installation 🔗

Install via NuGet:
C#:
dotnet add package Com.RickSeven.FormulaEngine.Core --version 1.0.0

🎨 Usage Examples

Basic Operations
C#:
var context = new Dictionary<string, object>
{
    { "revenue", 10000 },
    { "cost", 7000 },
    { "margin", 0.3 },
    { "desc", "Result is: " }
};

string formula = "desc + ((revenue - cost) * margin).ToString()";

var engine = new FormulaEngine();
var result = engine.Evaluate(formula, context);

// Output: "Result is: 900"
Console.WriteLine(result);

Math Functions
C#:
var context = new Dictionary<string, object>
{
    { "baseValue", 2.0 },
    { "exponent", 3.0 }
};

string formula = "Math.Pow(baseValue, exponent)";

var engine = new FormulaEngine();
var result = engine.Evaluate(formula, context);

// Output: 8.0
Console.WriteLine(result);

Culture-Specific Formatting
C#:
var jsonContext = @"{
    ""revenue"": 1000.50,
    ""cost"": 750.25,
    ""taxRate"": 0.2,
    ""description"": ""Profit calculation: ""
}";

var formula = @"description + ((revenue - cost) * (1 - taxRate)).ToString(""C2"", new System.Globalization.CultureInfo(""id-ID""))";

var engine = new FormulaEngine();
var result = engine.Evaluate(formula, jsonContext);

// Output: Profit calculation: Rp200,20
Console.WriteLine(result);

🗒️ Notes

  • The engine supports full C# expression syntax
  • Nested objects can be accessed using dot notation
  • Array elements can be accessed using index notation
  • JSON context is automatically parsed and made available to formulas
  • Culture-specific formatting is supported through standard .NET formatting
  • Error handling is provided through FormulaEvaluationException
 
Back
Top Bottom