PersistentAliasAttribute Class
Indicates that a property is not persistent and its value is calculated based upon the values of a specific field(s).
Namespace: DevExpress.Xpo
Assembly: DevExpress.Xpo.v24.1.dll
NuGet Packages: DevExpress.Win.PivotGrid, DevExpress.Win.TreeMap, DevExpress.Xpo
NuGet Package: DevExpress.Xpo
Declaration
Remarks
The expression to be evaluated can be passed as the constructor’s parameter or specified via the PersistentAliasAttribute.AliasExpression property. You can use both persistent and non-persistent fields to specify the expressions. The expression is evaluated at the server side when only persistent properties are used. When a non-persistent property is referred, XPO extracts a persistent part from an expression, sends it to the database server, and processes the rest on the client. Note that this is only possible when an operation involves object loading and won’t work in XPView.
The following code shows how to define a property as non-persistent and calculate its value based upon the values of specific persistent fields. The ExtendedPrice property is marked as a non-persistent, using the PersistentAliasAttribute
attribute. The expression to evaluate the property’s value is specified as the attribute’s parameter. In addition, the property’s getter evaluates this expression using the XPBaseObject.EvaluateAlias method.
using DevExpress.Xpo;
public class Order : XPObject {
public Order() {}
public decimal UnitPrice {
get { return fUnitPrice; }
set { SetPropertyValue(nameof(UnitPrice), ref fUnitPrice, value); }
}
decimal fUnitPrice;
public int Qty {
get { return fQty; }
set { SetPropertyValue(nameof(Qty), ref fQty, value); }
}
int fQty;
public decimal Discount {
get { return fDiscount; }
set { SetPropertyValue(nameof(Discount), ref fDiscount, value); }
}
decimal fDiscount;
[PersistentAlias("UnitPrice*Qty*(1-Discount)")]
public decimal ExtendedPrice {
get { return Convert.ToDecimal(EvaluateAlias(nameof(ExtendedPrice))); }
}
}
The following example shows how to use the PersistentAliasAttribute
to implement a public read-only property exposing a value of the private persistent property. The expression passed in the PersistentAliasAttribute
class constructor specifies the name of the field which stores the value at runtime. The field is decorated with the PersistentAttribute whose PersistentAttribute.MapTo property holds the name of the data store column where the field’s value is stored.
using DevExpress.Xpo;
// ...
[Persistent("DefaultAddress")]
private string mDefaultAddress;
[PersistentAlias(nameof(mDefaultAddress))]
public string DefaultAddress {
get {
return mDefaultAddress;
}
}
The PersistentAliasAttribute
has the following limitations:
While a criteria expression can return an object reference, this is not supported in all scenarios. You can return an object reference by directly referencing a property as follows:
"Iif(Part is null, MyCustOrderLine.Part, Part)"
In this snippet, the
Part
orMyCustOrderLine.Part
property references thePart
object and this object is returned correctly. However, you cannot retrieve reference properties from functions. The following expression does not work:"Iif(Part is null, MyCustOrderLine, MyCustOrderLine2).Part"
The
PersistentAlias
expressions with more than 2 nesting levels of aggregate functions are not supported if the expression is calculated on the server, for example:Collection[X = ^.Collection[Y = ^.Collection[Y = M].Min(Y)].Max(X)].Single()
In some cases, criteria that contains such expressions as
SomeLogicalFunction(Type)
and<Type>ObjectProperty
may not work. In such cases, the evaluator handles both parts of the expression, and when it processes the<Type>ObjectProperty
expression, the evaluator assumes that the object has anObjectProperty
. However, in the case of a class hierarchy, this behavior may be different. The following Criteria with upcasting does not work inPersistentCriteriaEvaluationBehavior.InTransaction
mode:(IsInstanceOfType(This, 'TypeA') And <TypeA>BooleanPropertyA) Or (IsInstanceOfType(This, 'TypeB') And <TypeB>BooleanPropertyB)
or
IIf(IsInstanceOfType(This, 'TypeA'), <TypeA>BooleanPropertyA, <TypeB>BooleanPropertyB)
Use the following equivalent Criteria instead:
IIf(IsInstanceOfType(This, 'TypeA'), <TypeA>BooleanPropertyA, false)
or
IIf(IsInstanceOfType(This, 'TypeB'), <TypeB>BooleanPropertyB, false)
Note
PersistentAliasAttribute does not require database schema changes and does not create additional table columns. To use computed database columns, apply the FetchOnlyAttribute.