The requested page is not available for the requested platform. You are viewing the content for Default platform.

How to: Use XPO Upcasting in XAF

  • 4 min to read

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.

Tip

Upcasting is not supported in Mobile applications.

The following business model will be implemented to demonstrate the UpCasting.

UpCasing1

The code below describes the persistent class hierarchy:

[DefaultClassOptions]
[System.ComponentModel.DefaultProperty("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("Title", ref title, value);
      }
   }
   public string Office {
      get {return office;}
      set {
         SetPropertyValue("Office", ref office, value);
      }
   }
   [Association("Department-Employees")]
   public XPCollection<EmployeeBase> Employees {
      get {return GetCollection<EmployeeBase>("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("Name", ref name, value);
      }
   }
   public string Email {
      get {return email;}
      set {
         SetPropertyValue("Email", ref email, value);
      }
   }
   private Department department;
   [Association("Department-Employees")]
   public Department Department {
      get {return department;}
      set {
         SetPropertyValue("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("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("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.

Now, run the application (whether the Windows Forms or ASP.NET Web application). Invoke a Department Detail View:

UpCasting3

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.

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:

  • <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.

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:

UpCasting2

You can see that the properties of the EmployeeBase class descendants are displayed together with the EmployeeBase class properties.

See Also