Simpra is a powerful expression language designed for evaluating dynamic expressions in .NET applications. It enables developers to execute logic dynamically within their applications, similar to how scripting languages work but optimized for business rules.
To install AltaSoft.Simpra, use NuGet:
dotnet add package AltaSoft.SimpraOr in your .csproj:
<PackageReference Include="AltaSoft.Simpra" Version="1.0.0" />- Dynamic Expression Execution: Evaluate expressions at runtime.
- Rich Syntax: Conditionals, arithmetic, lists, pattern matching, regex, membership, null checks.
- Mutability Control: Toggle between immutable and mutable execution.
- String & List Operations: Manipulate and aggregate data easily.
- Custom Functions: Extend with user-defined or async functions.
- Safe Evaluation: No loops, recursion, or external resource access.
-
Arithmetic
+,-,*,/,//(integer division), postfix%(percent),min,max10 // 3 # 3 50% # 0.5 5 min 2 # 2 -
Comparison
is,is not,<,<=,>,>=x is 10 y is not null a < b and < c # chained comparison -
Logical
and,or,not -
Membership
in,not in, plusany in,all in,any not in,all not inx in [1, 2, 3] all in [true, true, false] -
Pattern Matching
matches(regex)like(SQL-like patterns)
email matches "[a-z]+@[a-z]+\\.[a-z]+" name like "Jo%" -
Null Handling
has valuenullable has value
- if β¦ then β¦ else β¦ end
- when β¦ then β¦ else β¦ end
- return
if x > 10 then
return "big"
else
return "small"
end
when color then
"red" -> "Stop"
"green" -> "Go"
else "Unknown"
end
Simpra supports runtime directives that influence evaluation:
- $mutable on/off β Enables or disables mutability for model properties (local variables are always mutable).
- $case_sensitive on/off β Controls case sensitivity in string comparisons (
is,like,matches).
Example:
let str = 'abc'
$case_sensitive off
return str like 'A%' # true
-
Local variables declared with
letare always mutable:let x = 10 x += 5 # 15 x -= 2 # 13 -
Model properties can only be reassigned if mutability is enabled with $mutable on:
$mutable on Transfer.Amount = Transfer.Amount + 50 -
By default, $mutable is off, meaning model properties are read-only.
let items = [1, 2, 3]
items[0] # 1
sum(items) # 6
length(items) # 3
- Function calls:
method("hi") - Properties:
obj.prop - Indexers:
list[1]
Simpra supports chained comparison expressions without repeating the left-hand operand:
let x = 5
return x > 0 and < 10 # true
This allows constructs like a < b and < c for concise range checks.
Simpra provides a set of internal functions out-of-the-box:
-
Math
abs(x)β absolute valueround(x)β round to nearest integermin(a, b, β¦),max(a, b, β¦)β smallest/largest value
-
Lists
sum(list)β sum of valueslength(list)β number of elements
-
Strings
number(str)β converts to numberdate(str)β converts to datesubstring(str, index, count)β Retrieves part of a string starting at a 1-based position.
In addition to in and not in, Simpra supports any in, all in, any not in, and all not in:
let values = [1, 2, 3]
2 any in values # true
[2, 4] all in values # false
2 any not in values # false
[4, 5] all not in values # true
The % operator in Simpra is postfix percent, not modulus.
50% # 0.5
200% # 2.0
This makes it easy to express percentages directly inside formulas.
var simpra = new Simpra();
var model = new TestModel { Transfer = new Transfer { Amount = 50, Currency = "USD" } };
const string expression = "return Transfer.Amount * 2";
var result = simpra.Execute<int, TestModel>(model, expression);
Console.WriteLine(result); // Output: 100const string expression = """
let transfer = Transfer
return transfer.Amount > 100 and transfer.Currency is 'USD'
""";
var result = simpra.Execute<bool, TestModel>(model, expression);
Console.WriteLine(result); // Output: falseconst string expression = """
let values = [10, 20, 30]
return sum(values) / length(values)
""";
var result = simpra.Execute<decimal, TestModel>(model, expression);
Console.WriteLine(result); // Output: 20const string expression = """
let value = 'abc123'
return value matches '[a-zA-Z_][a-zA-Z_0-9]*'
""";
var result = simpra.Execute<bool, TestModel>(model, expression);
Console.WriteLine(result); // Output: trueconst string expression = """
let amount = Transfer.Amount
let divisor = 0
let result = when divisor is not 0 then amount / divisor else 0 end
return result
""";
var result = simpra.Execute<int, TestModel>(model, expression);
Console.WriteLine(result); // Output: 0const string expression = """
let str = 'hello'
return Upper(str) + ' WORLD'
""";
var result = simpra.Execute<string, TestModel, TestFunctions>(model, new TestFunctions(), expression);
Console.WriteLine(result); // Output: "HELLO WORLD"const string expression = """
let a = 10
let b = 3
let half = 50%
return [ a // b, half, a min b, a max b ]
""";
var result = simpra.Execute<List<decimal>, TestModel>(model, expression);
Console.WriteLine(string.Join(", ", result)); // Output: "3, 0.5, 3, 10"By default, expressions in Simpra are immutable. However, you can enable mutability:
const string expression = """
$mutable on
let x = 10
x = x + 5
return x
""";
var result = simpra.Execute<int, TestModel>(model, expression);
Console.WriteLine(result); // Output: 15You can define custom functions and use them in expressions:
public class CustomFunctions
{
public static string Reverse(string input) => new string(input.Reverse().ToArray());
}
const string expression = "return Reverse('hello')";
var result = simpra.Execute<string, TestModel, CustomFunctions>(model, new CustomFunctions(), expression);
Console.WriteLine(result); // Output: "olleh"Simpra supports calling asynchronous methods within expressions:
public class AsyncFunctions
{
public async Task<int> GetExchangeRateAsync() => await Task.FromResult(3);
}
const string expression = "return GetExchangeRateAsync() * 10";
var result = await simpra.ExecuteAsync<int, TestModel, AsyncFunctions>(model, new AsyncFunctions(), expression);
Console.WriteLine(result); // Output: 30Mutability can be enabled to allow modifying object properties:
const string expression = @"
$mutable on
Transfer.Amount = Transfer.Amount + 50
return true
";
var model = new TestModel { Transfer = new Transfer { Amount = 100 } };
var result = simpra.Execute<bool, TestModel>(model, expression);
Console.WriteLine(result); // Output: true
Console.WriteLine(model.Transfer.Amount); // Output: 150AltaSoft.Simpra is licensed under the MIT License.