Upcoming feature: UpCasting

XPO Team Blog
12 October 2006

If you have created persistent class hierarchies, specifically to bind to UIs, you probably know the situation: sometimes you’d like to combine base and derived classes in a single query, but it’s not easy to do or even impossible because the derived classes obviously have an extended set of properties.

To make this clearer, imagine these classes (once again, let me point out that the properties aren't implemented in their full recommended syntax for brevity’s sake):

  public class Thing : XPObject {
    public Thing(Session session) : base(session) { } 
    
    private string name;
    public string Name {
      get { return name; }
      set { name = value; }
  }

  public class SpecialThing : Thing { 
    public SpecialThing(Session session) : base(session) { } 
    
    private int specialDetail;
    public int SpecialDetail {
      get { return specialDetail; }
      set { specialDetail = value; }
    }
  }

Now, in the simplest case you might want to create a collection of all Things, including those of the derived type, and access all their properties. This is a really common thing when the data is being bound to a user interface. So you create a collection of the base class type:

  XPCollection<Thing> coll = new XPCollection<Thing>(session);

The problem is that because this collection is typed for Thing, until now you were only able to access the properties of that type. So even if the collection contained elements that were really of the derived type SpecialThing, you were not able to access the property SpecialDetail, because that property was not known to the base class type. As a workaround, your only chance was to code custom property descriptors – I wrote a blog article about this way back – it’s a possibility, but cumbersome to do for ad-hoc needs.

The good news is, with that new feature we’ll make available in XPO 6.3, you’ll no longer need to write any code for this. Instead, you’ll simply modify the DisplayableProperties property of the XPCollection instance to something like “Oid;Name;<SpecialThing>SpecialDetail”. The “Oid;Name” part is the normal thing – you get that by default. The “<SpecialThing>SpecialDetail” is what’s new here: it describes a property on a type different from the base class type.

The feature is pretty much universal. In addition to DisplayableProperties, you can also use the same syntax in criteria, like so:

  XPCollection<Thing> coll = new XPCollection<Thing>(session, 
    new BinaryOperator("<SpecialThing>SpecialDetail", 42));

Of course the XPView is also compatible with this. I’m sure it’ll be useful in some situations.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
Tags
No Comments

Please login or register to post comments.