Package Description
$ dotnet add package AltaSoft.Simpra.MetadataSimpra 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.Simpra
Or in your .csproj:
<PackageReference Include="AltaSoft.Simpra" Version="1.0.0" />
Arithmetic
+, -, *, /, // (integer division), postfix % (percent), min, max
10 // 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, plus any in, all in, any not in, all not in
x 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 value
nullable has value
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:
is, like, matches).Example:
let str = 'abc'
$case_sensitive off
return str like 'A%' # true
Local variables declared with let are 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
method("hi")obj.proplist[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 valueLists
sum(list) – sum of valueslength(list) – number of elementsStrings
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: 100
const 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: false
const string expression = """
let values = [10, 20, 30]
return sum(values) / length(values)
""";
var result = simpra.Execute<decimal, TestModel>(model, expression);
Console.WriteLine(result); // Output: 20
const 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: true
whenconst 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: 0
const 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: 15
You 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: 30
Mutability 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: 150
AltaSoft.Simpra is licensed under the MIT License.