学习011-03-14 Use XPO Upcasting in XAF(在 XAF 中使用 XPO 向上转型)

Use XPO Upcasting in XAF(在 XAF 中使用 XPO 向上转型)

In this topic, you will learn how to use the Upcasting feature of XPO in XAF. It is useful when you need to combine base and derived classes in a single query.
在本主题中,您将学习如何在XAF中使用XPO的升级功能。当您需要在单个查询中组合基类和派生类时,它很有用。

The following business model will be implemented to demonstrate the UpCasting.
将实现以下业务模型来演示UpCast。
在这里插入图片描述

The code below describes the persistent class hierarchy:
下面的代码描述了持久类层次结构:

C# 
[DefaultClassOptions]
[System.ComponentModel.DefaultProperty(nameof(Title))]
   public class Department : BaseObject {
   private string title;
   private string office;
   public Department(Session session) : base(session) {}
   public string Title {
      get {return title;}
      set {
         SetPropertyValue(nameof(Title), ref title, value);
      }
   }
   public string Office {
      get {return office;}
      set {
         SetPropertyValue(nameof(Office), ref office, value);
      }
   }
   [Association("Department-Employees")]
   public XPCollection<EmployeeBase> Employees {
      get {return GetCollection<EmployeeBase>(nameof(Employees));}
   }
}


public class EmployeeBase : BaseObject {
   public EmployeeBase(Session session) : base(session) {}
   private string name;
   private string email;
   public string Name {
      get {return name;}
      set {
         SetPropertyValue(nameof(Name), ref name, value);
      }
   }
   public string Email {
      get {return email;}
      set {
         SetPropertyValue(nameof(Email), ref email, value);
      }
   }
   private Department department;
   [Association("Department-Employees")]
   public Department Department {
      get {return department;}
      set {
         SetPropertyValue(nameof(Department), ref department, value);
      }
   }
}


[DefaultClassOptions]
public class LocalEmployee : EmployeeBase {
   public LocalEmployee(Session session): base(session) {}
   private string insurancePolicyNumber;
   public string InsurancePolicyNumber {
      get {return insurancePolicyNumber;}
      set {
         SetPropertyValue(nameof(InsurancePolicyNumber), ref insurancePolicyNumber, value);
      }
   }
}


[DefaultClassOptions]
public class ForeignEmployee : EmployeeBase {
   public ForeignEmployee(Session session): base(session) {}    
   private DateTime visaExpirationDate;
   public DateTime VisaExpirationDate {
      get {return visaExpirationDate;}
      set {
         SetPropertyValue(nameof(VisaExpirationDate), ref visaExpirationDate, value);
      }
   }
}

In the code above, the Department, LocalEmployee and ForeignEmployee classes use the DefaultClassOptions attribute. To learn more, refer to the Data Annotations in Data Model topic.
在上面的代码中,部门、本地员工和外国员工类使用DefaultClassOptions属性。要了解更多信息,请参阅数据模型中的数据注释主题。

Now, run the Windows Forms, ASP.NET Web, or ASP.NET Core Blazor application. Invoke a Department Detail View:
现在,运行Windows窗体、ASP.NETWeb或ASP.NETCore Blazor应用程序。调用部门详细信息视图:
在这里插入图片描述

The nested Employees List View displays the properties of the EmployeeBase class only. This behavior is by design. However, it is better to display Employee class descendant specific properties in the Employees List View. With the UpCasting feature, this is accomplished with ease.
嵌套的员工列表视图仅显示员工库类的属性。这种行为是设计好的。但是,最好在员工列表视图中显示员工类后代的特定属性。使用UpCast功能,这可以轻松完成。

To add required columns to the List View that represents the Department.Employees collection, invoke the Model Editor. Locate the Views | Department_Employees_ListView | Columns node. Add two additional child nodes via the context menu. Specify their PropertyName property by the following values:
要将所需的列添加到表示部门.员工集合的列表视图中,请调用模型编辑器。找到视图|Department_Employees_ListView|列节点。通过上下文菜单添加两个额外的子节点。通过以下值指定它们的属性:

  • <LocalEmployee>InsurancePolicyNumber
  • <ForeignEmployee>VisaExpirationDate

These values will be recognized by XPO, and the LocalEmployee.InsurancePolicyNumber and ForeignEmployee.VisaExpirationDate properties will be displayed for objects retrieved from the database to the Department.Employees collection.
这些值将被XPO和Local员工识别。InsurancePolicyNumber和Foreign员工. VisaexpirationDate属性将显示为从数据库检索到部门的对象。员工集合。

In addition, set the “Insurance Policy Number” and “Visa Expiration Date” values to the Caption property of the newly added columns.
此外,将“保险单编号”和“签证到期日期”值设置为新添加列的标题属性。

Run the application and invoke the Department Detail View once again:
运行应用程序并再次调用部门详细信息视图:
在这里插入图片描述

You can see that the properties of the EmployeeBase class descendants are displayed together with the EmployeeBase class properties.
您可以看到员工基地类后代的属性与员工基地类属性一起显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值