Upcoming feature: UpCasting

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.

Tags
2 comment(s)
Geoff Davis
Excellent Oliver, will definately look forward to that and will certainly use it.

Is there anything else to get us excited by in 6.3 for XPO or anything else to that matter?

Not holding you to any dates mind you, I'm perfectly aware of your policy but do you know when the Beta will become available to us? Is it a matter of weeks or months?

Geoff.
12 October, 2006
Oliver Sturm (DevExpress)
Geoff: 6.3, as far as I know, is going to be the last "major" release this year, so I'm sure you can expect a number of exciting things. As we're going to have at least three releases in 2007 as well, we won't be able to wait too long with 6.3... guess it'll still be a number of weeks, though.
13 October, 2006

Please login or register to post comments.