Creating a class, the best practice way
Hi all!
Some time ago, I talked about the ‘construct’ pattern that is use a lot in AX. Although I use it often, it’s not perfect because it violates some best practices. (Also, you could end up with a large class hierarchy of classes that extend each other, but I’ll discuss that in an other post.)
First off, know that you can change the compiler level and the best practices the compiler checks.
To do this, compile something so the compiler output window pops up. Then click the setup button, and adjust the settings.
I used compiler level 4 and enabled all BP checks for this example.
So, let create a simple class. This class will simply print information of an InventTable record to the screen.
1. ClassDeclaration
When we think about this class a bit, we immediately realize that we need to declare an InventTable record as a member of out class:
/// Class written to test construct and new... patterns
/// </summary>
/// <remarks>
/// Delete this class when proven a point :)
/// </remarks>
class KLFORTestBP
{
InventTable inventTable;
}
Also note that I added documentation to this method. I will do so for other methods too. When you don’t, you’ll get a BP deviation warning.
2. Parm method
One of the things that are important (or at least I find important), is using parm methods for your class variables. So, let’s create one:
public InventTable parmInventTable(InventTable _inventTable = inventTable)
{
;
inventTable = _inventTable;
return inventTable;
}
3. New method
You should never write code in the new method, or add parameters to it. On the contrary, you should always set this method to protected, and use a static construct() or a static new…() method instead.
// use static new... or construct instead
protected void new()
{
}
4. Run method
The run method will be our method that contains our logic. Here, we print info to the screen
/// Executes logic on the parameters of this class
/// </summary>
/// <remarks>
/// Just for demo purposes
/// </remarks>
public void run()
{
;
info(this.parmInventTable().NameAlias);
}
Note that we are using the parm method instead of directly accessing the variable.
5. Construct method
Because the new method is now protected, we will need a contruct method to be able to create an instance of this class.
{
;
return new KLFORTestBP();
}
When a class doesn’t need parameters, you can leave out the ‘private’, but now, it is private because we plan on creating a new…() method that contains parameters. A construct method shouldn’t contain parameters, and should only be used to create a new instance of your class. It is also the only place in your application where you can call the new() method of your class.
6. New…() method
As said earlier, because we need a parameter (an InventTable record), we use a new…() instead of the construct method to create an instance of our class. I will name it newFromInventTable() based on the parameter it has.
In this method, we call the contruct() method to create an instance (instead of new()), set the parm method from the parameter and return the object.
/// Creates a new KLFORTestBP object from an InventTable record
/// </summary>
/// <param name="_inventTable">
/// The InventTable used to create the object
/// </param>
/// <returns>
/// New instance of KLFORTestBP with the InventTable parameter set
/// </returns>
/// <remarks>
/// Use a new... method instead of the constuct method because it takes parameters/// A
/// </remarks>
public static KLFORTestBP newFromInventTable(InventTable _inventTable)
{
KLFORTestBP kLFORTestBP;
;
kLFORTestBP = KLFORTestBP::construct();
kLFORTestBP.parmInventTable(_inventTable);
return kLFORTestBP;
}
You could also create an init() method that initializes you class (we don’t need initialization now). The init() method should return a boolean if initialization can go wrong, so you can throw an error.
It would look like this:
{
KLFORTestBP kLFORTestBP;
;
kLFORTestBP = KLFORTestBP::construct();
kLFORTestBP.parmInventTable(_inventTable);
if(!kLFORTestBP.init())
{
throw error("Item record has no data"); // todo: create label
}
return kLFORTestBP;
}
Remember to create a label for the text string, or you will get a best practice error.
7. Main method
Now, all we need more is a main method, so this class is runable (by pressing F5 or as a menu item).
{
KLFORTestBP kLFORTestBP;
InventTable inventTable;
;
if(_args && _args.dataset() == tablenum(InventTable))
{
// a button was pressed on a InventTable datasource
inventTable = _args.record();
}
else
{
// for demo, simple select the first one
select firstonly inventTable;
}
// display item information
kLFORTestBP = KLFORTestBP::newFromInventTable(inventTable);
kLFORTestBP.run();
}
Normally, you will get the record from the args object, but for demo purposes, I added the ’select firstonly’ so you can run the class by pressing F5. As you can see, the new…() method is used to create an instance of the class, and then the run method is called.
There you go, you now have a best practice deviation free class!
8. Using the class
Using the class as a menu item or in code couldn’t be easier.
You can drag/drop the class to the action menu item node in the AOT to create a menu item, and you will be able to use it on any form with the inventTable datasource.
From code, you can simple write this line (assuming there is a variable called inventTable):
Conclusion:
When writing code that doesn’t contain any best practice deviations (especially when checking them all on compiler level 4), you quickly end up with a lot of methods in your class and wondering if the effort is worth it. It can be a bit painful to get your code best practice deviation free.
However, you’ll also realise that following the best practices has its advantages. Your code will be cleaner, easier to understand, easier to debug because of the parm, constuct and run methods, less subject to change, contain less errors and your class will be used in the way you intended when you wrote it.
More info on MSDN:
Best practices for constructors
This was originally posted here.
*This post is locked for comments