1. 程式人生 > >PersistentAliasAttribute & CalculatedAttribute & CalculatedPersistentAliasAttribute

PersistentAliasAttribute & CalculatedAttribute & CalculatedPersistentAliasAttribute

表達 font 服務器 eal logic -s area direct model

一,PersistentAliasAttribute-[XPO提供]

Indicates that a property is not persistent and its value is calculated based upon the values of a specific field(s).

指示某個屬性不是持久性的, 並且它的值是根據特定字段的值計算的。

The expression to be evaluated can be passed as the constructor‘s parameter or specified via the 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.

要計算的表達式可以作為構造函數的參數傳遞或通過 AliasExpression 屬性指定。您可以使用持久性字段和非持久域來指定表達式。當只使用持久性屬性時, 在服務器端計算表達式。當引用非持久性屬性時, xpo 從表達式中提取持久性部分, 將其發送到數據庫服務器, 並在客戶端上處理其余內容。請註意, 只有當操作涉及對象加載並且無法在 XPView 中工作時, 才可能出現這種情況。

技術分享 Example

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 thePersistentAliasAttribute 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.EvaluateAliasmethod.

下面的代碼演示如何將屬性定義為非持久性, 並根據特定持久性字段的值計算其值。使用 PersistentAliasAttribute 屬性將ExtendedPrice

屬性標記為非持久性。

用於計算屬性值的表達式被指定為屬性的參數。此外, 該屬性的 getter 使用 XPBaseObject EvaluateAliasmethod 計算此表達式。

using DevExpress.Xpo;

public class Order : XPObject {
   public Order() {}
   public decimal UnitPrice;
   public
int Qty; public decimal Discount; [PersistentAlias("UnitPrice*Qty*(1-Discount)")] public decimal ExtendedPrice { get { return Convert.ToDecimal(EvaluateAlias("ExtendedPrice")); } } }

Note that while a criteria expression can return an object reference, this is not supported in all scenarios. Returning an object reference by directly referencing a property, as in the following code snippet, is fully supported.

請註意, 雖然條件表達式可以返回對象引用, 但在所有方案中不支持此項。完全支持通過直接引用屬性 (如下面的代碼段) 返回對象引用。


[PersistentAlias("Iif(Part is null, MyCustOrderLine.Part, Part)")]
public Part CustomPart {
    get {
        return (Part)EvaluateAlias("CustomPart");
    }
}

In this code snippet, a Part object referenced by the Part or MyCustOrderLine.Part property will be returned correctly. However, retrieving reference properties from function and aggregation operators is not supported. So, the following persistent alias declaration will not work.

在此代碼段中, Part對象引用Part或MyCustOrderLine.Part屬性,部件屬性將正確返回。

但是, 不支持從函數和聚合運算符檢索引用屬性。因此, 下列持久性別名聲明將不起作用。

[PersistentAlias("Iif(Part is null, MyCustOrderLine, MyCustOrderLine2).Part")]
public Part CustomPart {
    get {
        return (Part)EvaluateAlias("CustomPart");
    }
}

技術分享 Example

The following example shows how to apply the PersistentAliasAttribute to the DefaultAddress property. When initializing a new instance of the PersistentAliasAttribute class, the name of the field which stores theDefaultAddress property‘s value is specified. The mDefaultAddress field has the PersistentAttribute whose PersistentAttribute.MapTo property holds the name of the column in the data store where the field‘s value is stored.

下面的示例演示如何將 PersistentAliasAttribute 應用於 DefaultAddress 屬性。在初始化 PersistentAliasAttribute 類的新實例時, 將指定存儲 theDefaultAddress 屬性值的字段的名稱。mDefaultAddress 字段具有 PersistentAttribute, 其 PersistentAttribute. MapTo 是字段名。

using DevExpress.Xpo;

// ...
[Persistent("DefaultAddress")]
private string mDefaultAddress;

[PersistentAlias("mDefaultAddress")]
public string DefaultAddress {
    get {
        return mDefaultAddress;
    }
}

二,CalculatedAttribute - [XAF提供]

Applied to business class properties. Specifies an expression used to calculate the target property value in the Data View mode.

應用於業務類屬性。指定用於在數據DataView 模式下計算目標屬性值的表達式。

Below is an example of using the CalculatedAttribute.

using DevExpress.ExpressApp.DC;
// ... 
public class Person {    
    // ... 
    public String FirstName { get; set; }
    public String LastName { get; set; }
    [Calculated("FirstName + ‘ ‘ + LastName")]
    public String FullName {
        get {
            return FirstName + " " + LastName;
        }
    }
}


Properties, used in the Expression should be persistent to be able to run an SQL query at the database level.

在表達式中使用的屬性應該是持久的, 以便能夠在數據庫級別運行 sql 查詢。

三,CalculatedPersistentAliasAttribute - [XAF提供]

Applied to a business class. Allows you to dynamically configure a persistent alias for the target business class‘ property.

動態的指定計算字段的公式.

Non-persistent calculated properties for which filtering and sorting should be performed on the database level can be implemented via the PersistentAlias attribute. The PersistentAlias attribute takes a single parameter which specifies the expression used to calculate the property‘s value on the database server side. A persistent alias must be specified in code as the attribute‘s parameter. However, in certain scenarios, the property may be required to have a configurable persistent alias, and it must be possible to configure it in a deployed application by an administrator. In this instance, the CalculatedPersistentAliasAttribute attribute should be used.

可通過 PersistentAlias 屬性實現在數據庫級別上執行篩選和排序的非持化的計算屬性

PersistentAlias 屬性采用一個參數來指定用於計算數據庫服務器端屬性值的表達式。

必須在代碼中將持久性別名指定為屬性的參數。

但是, 在某些情況下, 該屬性可能需要具有可配置的持久別名, 並且必須可以在部署的應用程序中由管理員對其進行配置。

在此實例中, 應使用 CalculatedPersistentAliasAttribute 屬性。

The CalculatedPersistentAliasAttribute contains two properties initialized via the attribute‘s constructor. The first parameter specifies the property name for which you would like to set up a persistent alias. The second parameter specifies the property which returns the alias‘ expression. The property which returns the alias‘ expression can return different expressions based on a specific rule. For example, you can implement logic which loads the alias expression from a configuration file which can be customized by a system administrator.

Note that the CalculatedPersistentAliasAttribute attribute is initialized at the application startup, while the application is being set up. So, if you want to change the value returned by the property which specifies the alias expression, this should be done while the application is being initialized, for example, in a module‘s constructor.

The following code snippet illustrates use of the CalculatedPersistentAliasAttribute. The Product class contains the calculated ProductName property, which has a configurable persistent alias:

CalculatedPersistentAliasAttribute 構造函數中有兩個參數。 第一個參數指定要為其設置永久別名的屬性名。 第二個參數指定返回別名 "表達式" 的屬性。 返回別名 "表達式" 的屬性可以根據特定規則返回不同的表達式。例如, 您可以實現從配置文件中加載別名表達式的邏輯, 系統管理員可以自定義它。

請註意, 在應用程序初始化時, CalculatedPersistentAliasAttribute 屬性在應用程序啟動時初始化。因此, 如果要更改指定別名表達式的屬性返回的值, 則應在初始化應用程序時 (例如在模塊的構造函數中) 執行此操作。

下面的代碼段說明了 CalculatedPersistentAliasAttribute 的使用。該產品類包含計算的產品屬性, 它具有可配置的持久別名: Show Me

The complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1771.

[CalculatedPersistentAlias("ProductName", "ProductNameAlias"), DefaultClassOptions]
//ProductName 這個屬性的值將來從ProductNameAlias中取得公式內容.
public class Product : BaseObject { public Product(Session session) : base(session) { } private static string productNameFormat = "{Manufacturer} {Model}"; private static string productNameAlias = "concat(Manufacturer, Model)"; public string ProductName { get { return ObjectFormatter.Format( productNameFormat, this, EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty ); } } [Browsable(false)] public static string ProductNameAlias { get { return productNameAlias; } } public static void SetProductNameFormat(string productNameFormat, string productNameAlias) { if(!string.IsNullOrEmpty(productNameFormat)) { Product.productNameFormat = productNameFormat; } if(!string.IsNullOrEmpty(productNameAlias)) { Product.productNameAlias = productNameAlias; } } public string Manufacturer { get { return GetPropertyValue<string>("Manufacturer"); } set { SetPropertyValue<string>("Manufacturer", value); } } public string Model { get { return GetPropertyValue<string>("Model"); } set { SetPropertyValue<string>("Model", value); } } public string Revision { get { return GetPropertyValue<string>("Revision"); } set { SetPropertyValue<string>("Revision", value); } } }


The persistent alias‘ value is initialized from the application‘s configuration file in a module‘s constructor:

Note that a property which returns the persistent alias‘ expression must be declared as public and static (Public and Shared in VB).

在模塊的構造函數中, 從應用程序的配置文件中初始化持久性別名 "值:

請註意, 返回持久性別名" 表達式的屬性必須聲明為公共和靜態 (在 vb 中為公共和共享)。

public sealed partial class CalculatedAliasModule : ModuleBase {
    public CalculatedAliasModule() {
        InitializeComponent();
         
        Product.SetProductNameFormat(    
            ConfigurationManager.AppSettings["ProductNameFormat"],    
            ConfigurationManager.AppSettings["ProductNameAlias"]    
            );   
    }
}

此外, 還需要以下列方式重寫 ModuleBase. CustomizeTypesInfo 方法:

using DevExpress.ExpressApp.Xpo;
// ... 
public override void CustomizeTypesInfo(ITypesInfo typesInfo) {
    base.CustomizeTypesInfo(typesInfo);
    CalculatedPersistentAliasHelper.CustomizeTypesInfo(typesInfo);
}

Note that two business classes from the Business Class Library use the CalculatedPersistentAliasAttribute attribute. The Person class uses this attribute to configure the format of the calculated FullName property, and theAddress class uses this attribute to configure the calculated FullAddress property. To learn how to configure the format of these properties, refer to the How to: Change the Format Used for the FullAddress and FullName Properties topic.

請註意, 業務類庫中的兩個業務類使用 CalculatedPersistentAliasAttribute 屬性。"人員" 類使用此特性來配置計算的 fullname 屬性的格式, 而地址類使用此特性來配置計算的 FullAddress 屬性。

要了解如何配置這些屬性的格式,請參閱如何: 更改用於 FullAddress 和 fullname 屬性主題的格式。



PersistentAliasAttribute & CalculatedAttribute & CalculatedPersistentAliasAttribute