Protected new and construct – Part 2

February 18, 2010 at 18:06
filed under Dynamics AX
Tagged , ,

In part 1, we talked about the use of a protected new method and a static constructor, and how it helps improve your classes.

Now let’s see how it helps you maintain you code better when extending classes.

In the previous example, we printed some general information about a record to the screen.
Let’s say that, in case of CustTable we want to have information printed to to screen that is specific to this record, like the AccountNum or Name field.

We will need to create a new class that extends the class we created earlier.

1. ClassDeclaration KLFShowRecordInfo_CustTable
In the ClassDeclaration, we include the keyword ‘extends’ to specify that our new class extends the functionality of the class KLFShowRecordInfo.

class KLFShowRecordInfo_CustTable extends KLFShowRecordInfo
{
}

2. Construct method KLFShowRecordInfo_CustTable
We will also add a construct method to this class. This will create an instance of this class for us, and set the parm method.
Because we extend a class that contains this parm method, we don’t need to add it to this class again.

public static KLFShowRecordInfo_CustTable construct(Common _record)
{
    KLFShowRecordInfo_CustTable kLFShowRecordInfo_CustTable;
    ;

    kLFShowRecordInfo_CustTable = new KLFShowRecordInfo_CustTable();
    kLFShowRecordInfo_CustTable.parmRecord(_record);

    return kLFShowRecordInfo_CustTable;
}

3. Run method KLFShowRecordInfo_CustTable
In the run method, we print data from the CustTable record to screen.

void run()
{
    CustTable custTable;
    ;

    custTable = this.parmRecord();

    info(custTable.AccountNum);
    info(custTable.Name);
}

You could call the super() method if you want the general information to be printed as well.

4. Construct method KLFShowRecordInfo
The only thing we need to change to the existing class is the construct method.
Modify it like this:

public static KLFShowRecordInfo construct(Common _record)
{
    KLFShowRecordInfo kLFShowRecordInfo;
    ;

    switch (_record.TableId)
    {
        case tablenum(CustTable):
            kLFShowRecordInfo = KLFShowRecordInfo_CustTable::construct(_record);
        break;
        default :
            kLFShowRecordInfo = new KLFShowRecordInfo();
            kLFShowRecordInfo.parmRecord(_record);
        break;
    }

    return kLFShowRecordInfo;
}

The switch will check which table is being processed, and will create an instance of KLFShowRecordInfo_CustTable in case of CustTable. For all other tables, it will construct an instance of KLFShowRecordInfo.

5. TestJob
Let’s test it:

static void KLF_TestRecordInfo(Args _args)
{
    CustTable custTable;
    VendTable vendTable;
    ;

    select firstonly custTable;
    select firstonly vendTable;

    // custTable:
    KLFShowRecordInfo::construct(custTable).run();
    info("----");
    // vendTable:
    KLFShowRecordInfo::construct(vendTable).run();
}

Output:

00000001
IKEA
—-
VendTable

Conclusion
This concludes my articles about the protected new and construct methods. I realise that the examples are very simple, but the principle will remain the same, even in more complex scenarios.

To sum up:
My basic recommendations when creating classes are:
– Use a protected new method
– Use a construct method
– Use parm methods
– Create a run method that contains your business logic
– Instead of modifying existing classes, create new ones that extend existing

If you disagree, be sure to leave a comment :).

2 comments

RSS / trackback

  1. Ismael

    hi, it´s good job,
    regards

  2. Shoot

    Great example! Thank you.

respond