Expressions Basics
A product can integrate expressions that call C# code using IExpressionFactory.CreateDelegate()
 (or one of its several extension methods).
Advantages and Drawbacks
Pure expressions
Standard expressions and functions have the following advantages:
They can be mapped to the database, improving ORM speed.
They can be serialized in remoting drivers
They can be analyzed and used by Quino to optimize other operations
They have the following drawbacks:
The logic is limited to a single-statement (with some conditional logic, like that provided by theÂ
If
 function)Evaluation is not easily debugged (there's no source-level debugger for Quino expressions)
Delegate expressions
Delegate expressions (and Expression Eigene Funktionen) have the following advantages:
They can contain more complex logic
They can be easily debugged
They have the following drawbacks:
They cannot be mapped to the database (there is a mechanism for mapping custom functions to databases. See Expression Eigene Funktionen)
They cannot be serialized in remoting drivers
They cannot be analyzed by Quino
With those pros and cons in mind, the following document explains how to add C# delegates to expressions.
Context
The context passed to a delegate expression when evaluated contains the following objects by default:
TheÂ
IDataSession
TheÂ
IDataObject
 to which the property belongs
A product can either use the session to get other dependencies or it can retrieve them from the IExpressionContext
 directly with GetInstance()
. A product can also register its own IExpressionContextFactory
 to control which objects are added.
Examples
Simple expression
Let's start with a calculated property that doesn't use a delegate. Instead, it just returns the value of another property:
personBuilder
.Add
.CalculatedProperty("Seniority", MetaType.Integer)
.Value("YearsActive");
Accessing the object in the context
The next example shows how to get this property with a delegate, but without any dependency on Modell Generieren.
personBuilder
.Add
.CalculatedProperty("Seniority", MetaType.Integer)
.ValueExpression<Person>(c => c.YearsActive + 1);
This example has two problems that we need to address:
It accesses the property by name rather than statically
It requires that the context contains an object of typeÂ
IDataObject
Indicating computability
In many cases the product will want to indicate that the expression cannot be evaluated with some contexts.
In these cases, the product should use a different overload that returns a bool
 and sets an out
 parameter for the value instead.
person
.Add
.CalculatedProperty("Bonus", MetaType.Integer)
.ValueExpression(TryGetBonus);
// ...
private bool TryGetBonus(IExpressionContext context, out object value)
{
if (context.TryGetInstance<Person>(out var person))
{
if (person.Company != null)
{
if (context.TryGetSingle(out ISystemClock clock))
{
if (person.HireDate < clock.UtcNow)
{
value = person.Company.GetBonus(person.HireDate.Year);
return true;
}
}
}
}
value = null;
return return false;
}
Comparing parsed and delegate expressions
At the same time, you can see how powerful and useful the Expression Text Formatierung is: the GetSalutation()
 method has a lot of hand-coding just to get the same behavior as the much shorter text-formatting expression.
Note: The example below assumes that the fieldÂ
_expressionParser
 is an injectedÂIExpressionParser
.
Mapping to SQL
Until the issue QNO-6478 provides more well-integrated support for mapping DelegateExpressions
 to SQL, a product can build custom mapping with the following pattern.
The example below extends the standard DelegateExpression
 to implement IMappableExpression
 and map the simple expression not Active
 for people.
Note the following in the example above:
ExecuteLocally
 only returnsÂtrue
 if the context contains aÂPerson
When mapping SQL, the context is either empty or contains only a session (but no person), soÂ
ExecuteLocally
 returns falseThe methodÂ
TryMapExpressionToSql
 handles the SQL-mapping case for PostgreSql and SQl Server.
You can use the expression in a query as follows:
You can use the expression in metadata as follows:
Â