Overriding Properties with ASNA Visual RPG (AVR)

In this post I’ll demonstrate how to override properties in an AVR Class.

The example application developed for this post, and all of the source code can be downloaded at the following link;

Property Override Example

The sample database files, which are included with the example application download, can be imported into a local DataGate database on your PC.

Lets start off by describing a business domain object model defined by the following classes.

  • Order – represents an order header with a collection of one-to-many line items
  • Item – represents a line item of an order

These are the same classes I used in my previous post on navigation properties in AVR.

Here are code snippets from these two classes;

Order Class

Item Class

In the business application I want to have two versions of the Item object.

  • Version One – The Order property of the Item is populated with the actual Order that the item belongs to. In this case the Item is a stand alone object.
  • Version Two – The item object is part of a collection of items that will populate the Items property of an Order. In this case it would be redundant to populate the Order property of each Item. I want the Order property of the Items to have an instance of the Order object with unpopulated properties

I solved this problem using the class inheritance and class member override capabilities in AVR. There are three features of the Item class that support class inheritance and class member overrides;

  1. The class declaration includes the Modifier keyword with a value of *Extensible as follows;
    BegClass Item Access(*Public) Modifier(*Extensible). This means the Item class can be inherited by sub-classes.
  2. The Order property is defined with the Modifier keyword with a value of *Overridable as follows;
    BegProp Order Type(Order) Access(*Public) Modifier(*Overridable). This means the Order property can be implemented in a completely different way by child-classes that are derived from the Item parent-class.
  3. The _order field that the Order property is based on is declared with the Access keyword with a value of *Protected a follows;
    DclFld _order Type(Order) Access(*protected). This means the _order field is only accessible by members of the Item class and any classes derived from the Item class.

Now I can create a sub-class of Item that overrides the Order property with a completely different implementation of the setter method. Here is the source code for a class that I called ItemOrder;

There are three elements of the ItemOrder class that support class inheritance and overriding class members;

  1. The class declaration includes the Extends keyword with a value of Item as follows;
    BegClass ItemOrder Access(*Public) Extends(Item). This means ItemOrder is derived from the parent class Item.
  2. The Order property is defined with the Modifier keyword with a value of *Overrides as follows;
    BegProp Order Type(Order) Access(*Public) Modifier(*Overrides). This means that when an instance of an Item object is based on the ItemOrder class, the implementation of the Order property will be overridden by ItemOrder class.
  3. All members of the Item base class, declared with the Access keyword value of *Protected, are accessible to the ItemOrder class. Remember the field _order was declared as protected in the Item base class.

In AVR, getter methods are coded inside the BegGet and EndGet keywords. Setter methods are coded inside the BegSet and EndSet keywords. Even though they are framed by some funny looking keywords, getter and setter methods work like any other method in AVR. In the Order property of the ItemOrder class, the setter method has a custom implementation for setting the value of the protected _order field. There are two lines of code in the setter method as follows;

The first line creates a new instance of the OrderDbContext class, which controls all data access to the Order Header file. The second line of code assigns a value to the _order field by invoking the GetOrder method of OrderDbContext. The GetOrder method expects one argument to be passed to it, the order number, which is the key to the Order Header file. The GetOrder method returns a fully populated Order object which is assigned to _order.

With AVR’s class inheritance and class member override capabilities, I now have two ways of implementing the Item object in the business domain object model.

  1. Stand-alone Item – an instance of the ItemOrder class
    In this implementation an order object, with a valid order number, is assigned to the Order property of the ItemOrder class. This invokes the setter method of the Order property of ItemOrder, which will assign an Order object populated from the database.
  2. An Items that is part of a collection of items – an instance of the Item class
    In this implementation an instance of the Order object is assigned to the Order property of the Item class. This invokes the setter method of the Order property of Item, which will assign an Order Object not populated from the database.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">