<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Art Of Creation - Dynamics AX Blog &#187; Patterns</title>
	<atom:link href="http://www.artofcreation.be/tag/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artofcreation.be</link>
	<description>The everyday life of a Dynamics AX developer</description>
	<lastBuildDate>Thu, 02 Feb 2012 12:20:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Try Catch example code</title>
		<link>http://www.artofcreation.be/2011/08/09/try-catch-example-code/</link>
		<comments>http://www.artofcreation.be/2011/08/09/try-catch-example-code/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 18:04:50 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[2009]]></category>
		<category><![CDATA[Batch]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Try Catch]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=1140</guid>
		<description><![CDATA[Hi all! The code below contains a try/catch that I use a lot when developing batch jobs, especially multithreaded ones. It deals with frequently occurring exceptions that, in some cases, can be easily solved by retrying: Deadlocks Update conflicts Duplicate key conflicts Duplicate key conflicts are more rare than update conflicts, but I&#8217;ve seen them [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all!</p>
<p>The code below contains a try/catch that I use a lot when developing batch jobs, especially <a href="http://www.artofcreation.be/2010/10/03/batch-multithreading/">multithreaded ones</a>.<br />
It deals with frequently occurring exceptions that, in some cases, can be easily solved by retrying:</p>
<ul>
<li>Deadlocks</li>
<li>Update conflicts</li>
<li>Duplicate key conflicts</li>
</ul>
<p>Duplicate key conflicts are more rare than update conflicts, but I&#8217;ve seen them pop up under heavy load on InventDim records.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0000ff;">try</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">ttsbegin</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// do stuff here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">ttsCommit</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; catch <span style="color: #000000;">&#40;</span>Exception<span style="color: #00007f;">::</span><span style="color: #000000;">Deadlock</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// retry on deadlock</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">retry</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; catch <span style="color: #000000;">&#40;</span>Exception<span style="color: #00007f;">::</span><span style="color: #000000;">UpdateConflict</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// try to resolve update conflict</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>appl.<span style="color: #000000;">ttsLevel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>xSession<span style="color: #00007f;">::</span><span style="color: #000000;">currentRetryCount</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">&gt;=</span> #RetryNum<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> Exception<span style="color: #00007f;">::</span><span style="color: #000000;">UpdateConflictNotRecovered</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">retry</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> Exception<span style="color: #00007f;">::</span><span style="color: #000000;">UpdateConflict</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; catch<span style="color: #000000;">&#40;</span>Exception<span style="color: #00007f;">::</span><span style="color: #000000;">DuplicateKeyException</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// retry in case of an duplicate key conflict</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>appl.<span style="color: #000000;">ttsLevel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>xSession<span style="color: #00007f;">::</span><span style="color: #000000;">currentRetryCount</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">&gt;=</span> #RetryNum<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> Exception<span style="color: #00007f;">::</span><span style="color: #000000;">DuplicateKeyExceptionNotRecovered</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">retry</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> Exception<span style="color: #00007f;">::</span><span style="color: #000000;">DuplicateKeyException</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2011/08/09/try-catch-example-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating a class, the best practice way</title>
		<link>http://www.artofcreation.be/2010/05/18/creating-a-class-the-best-practice-way/</link>
		<comments>http://www.artofcreation.be/2010/05/18/creating-a-class-the-best-practice-way/#comments</comments>
		<pubDate>Tue, 18 May 2010 11:40:20 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=723</guid>
		<description><![CDATA[Hi all! Some time ago, I talked about the &#8216;construct&#8217; pattern that is use a lot in AX. Although I use it often, it&#8217;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&#8217;ll discuss that in an other [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all!</p>
<p>Some time ago, I talked about the &#8216;construct&#8217; pattern that is use a lot in AX. Although I use it often, it&#8217;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&#8217;ll discuss that in an other post.)</p>
<p>First off, know that you can change the compiler level and the best practices the compiler checks.<br />
To do this, compile something so the compiler output window pops up. Then click the setup button, and adjust the settings.<br />
I used compiler level 4 and enabled all BP checks for this example. </p>
<p>So, let create a simple class. This class will simply print information of an InventTable record to the screen. </p>
<p><strong>1. ClassDeclaration</strong><br />
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:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007f00;">/// &lt;summary&gt;</span><br />
<span style="color: #007f00;">/// Class written to test construct and new... patterns</span><br />
<span style="color: #007f00;">/// &lt;/summary&gt;</span><br />
<span style="color: #007f00;">/// &lt;remarks&gt;</span><br />
<span style="color: #007f00;">/// Delete this class when proven a point :)</span><br />
<span style="color: #007f00;">/// &lt;/remarks&gt;</span><br />
<span style="color: #0000ff;">class</span> KLFORTestBP<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; InventTable inventTable;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Also note that I added documentation to this method. I will do so for other methods too. When you don&#8217;t, you&#8217;ll get a BP deviation warning. </p>
<p><strong>2. Parm method</strong><br />
One of the things that are important (or at least I find important), is using parm methods for your class variables. So, let&#8217;s create one:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007f00;">// use parm methods to access class variables</span><br />
<span style="color: #0000ff;">public</span> InventTable parmInventTable<span style="color: #000000;">&#40;</span>InventTable _inventTable <span style="color: #00007f;">=</span> inventTable<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; ;<br />
&nbsp; &nbsp; inventTable <span style="color: #00007f;">=</span> _inventTable;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> inventTable;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>3. New method</strong><br />
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&#8230;() method instead.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007f00;">// a new method should be protected</span><br />
<span style="color: #007f00;">// use static new... or construct instead</span><br />
<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #0000ff;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>4. Run method</strong><br />
The run method will be our method that contains our logic. Here, we print info to the screen</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007f00;">/// &lt;summary&gt;</span><br />
<span style="color: #007f00;">/// Executes logic on the parameters of this class</span><br />
<span style="color: #007f00;">/// &lt;/summary&gt;</span><br />
<span style="color: #007f00;">/// &lt;remarks&gt;</span><br />
<span style="color: #007f00;">/// Just for demo purposes</span><br />
<span style="color: #007f00;">/// &lt;/remarks&gt;</span><br />
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; ;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>this.<span style="color: #000000;">parmInventTable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">NameAlias</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Note that we are using the parm method instead of directly accessing the variable. </p>
<p><strong>5. Construct method</strong><br />
Because the new method is now protected, we will need a contruct method to be able to create an instance of this class.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> KLFORTestBP construct<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; ;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> KLFORTestBP<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>When a class doesn’t need parameters, you can leave out the &#8216;private&#8217;, but now, it is private because we plan on creating a new&#8230;() method that contains parameters. A construct method shouldn&#8217;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. </p>
<p><strong>6. New&#8230;() method</strong><br />
As said earlier, because we need a parameter (an InventTable record), we use a new&#8230;() instead of the construct method to create an instance of our class. I will name it newFromInventTable() based on the parameter it has.<br />
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.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007f00;">/// &lt;summary&gt;</span><br />
<span style="color: #007f00;">/// Creates a new KLFORTestBP object from an InventTable record</span><br />
<span style="color: #007f00;">/// &lt;/summary&gt;</span><br />
<span style="color: #007f00;">/// &lt;param name=&quot;_inventTable&quot;&gt;</span><br />
<span style="color: #007f00;">/// The InventTable used to create the object</span><br />
<span style="color: #007f00;">/// &lt;/param&gt;</span><br />
<span style="color: #007f00;">/// &lt;returns&gt;</span><br />
<span style="color: #007f00;">/// New instance of KLFORTestBP with the InventTable parameter set</span><br />
<span style="color: #007f00;">/// &lt;/returns&gt;</span><br />
<span style="color: #007f00;">/// &lt;remarks&gt;</span><br />
<span style="color: #007f00;">/// Use a new... method instead of the constuct method because it takes parameters/// A</span><br />
<span style="color: #007f00;">/// &lt;/remarks&gt;</span><br />
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFORTestBP newFromInventTable<span style="color: #000000;">&#40;</span>InventTable _inventTable<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFORTestBP kLFORTestBP;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; kLFORTestBP <span style="color: #00007f;">=</span> KLFORTestBP<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; kLFORTestBP.<span style="color: #000000;">parmInventTable</span><span style="color: #000000;">&#40;</span>_inventTable<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFORTestBP;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>You could also create an init() method that initializes you class (we don&#8217;t need initialization now). The init() method should return a boolean if initialization can go wrong, so you can throw an error.<br />
It would look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFORTestBP newFromInventTable<span style="color: #000000;">&#40;</span>InventTable _inventTable<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFORTestBP kLFORTestBP;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; kLFORTestBP <span style="color: #00007f;">=</span> KLFORTestBP<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; kLFORTestBP.<span style="color: #000000;">parmInventTable</span><span style="color: #000000;">&#40;</span>_inventTable<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #00007f;">!</span>kLFORTestBP.<span style="color: #000000;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> error<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;Item record has no data&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// todo: create label</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFORTestBP;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Remember to create a label for the text string, or you will get a best practice error. </p>
<p><strong>7. Main method</strong><br />
Now, all we need more is a main method, so this class is runable (by pressing F5 or as a menu item).</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">server</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFORTestBP kLFORTestBP;<br />
&nbsp; &nbsp; InventTable inventTable;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>_args <span style="color: #00007f;">&amp;&amp;</span> _args.<span style="color: #000000;">dataset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">==</span> <span style="color: #0000ff;">tablenum</span><span style="color: #000000;">&#40;</span>InventTable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// a button was pressed on a InventTable datasource</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; inventTable <span style="color: #00007f;">=</span> _args.<span style="color: #000000;">record</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// for demo, simple select the first one</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">select</span> <span style="color: #0000ff;">firstonly</span> inventTable;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// display item information</span><br />
&nbsp; &nbsp; kLFORTestBP <span style="color: #00007f;">=</span> KLFORTestBP<span style="color: #00007f;">::</span><span style="color: #000000;">newFromInventTable</span><span style="color: #000000;">&#40;</span>inventTable<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; kLFORTestBP.<span style="color: #000000;">run</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Normally, you will get the record from the args object, but for demo purposes, I added the &#8216;select firstonly&#8217; so you can run the class by pressing F5. As you can see, the new&#8230;() method is used to create an instance of the class, and then the run method is called. </p>
<p><strong>There you go, you now have a best practice deviation free class!</strong></p>
<p><strong>8. Using the class</strong><br />
Using the class as a menu item or in code couldn&#8217;t be easier. </p>
<p>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. </p>
<p>From code, you can simple write this line (assuming there is a variable called inventTable):</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">KLFORTestBP<span style="color: #00007f;">::</span><span style="color: #000000;">newFromInventTable</span><span style="color: #000000;">&#40;</span>inventTable<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">run</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></div>
<p><strong>Conclusion:</strong><br />
When writing code that doesn&#8217;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.</p>
<p>However, you&#8217;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. </p>
<p>More info on MSDN:<br />
<a href="http://msdn.microsoft.com/en-us/library/aa854210.aspx">Best practices for constructors</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/05/18/creating-a-class-the-best-practice-way/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Protected new and construct &#8211; Part 2</title>
		<link>http://www.artofcreation.be/2010/02/18/protected-new-and-construct-part-2/</link>
		<comments>http://www.artofcreation.be/2010/02/18/protected-new-and-construct-part-2/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 17:06:49 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=479</guid>
		<description><![CDATA[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&#8217;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&#8217;s say [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.artofcreation.be/2010/02/15/protected-new-and-construct-part-1/">part 1</a>, we talked about the use of a protected new method and a static constructor, and how it helps improve your classes.</p>
<p>Now let&#8217;s see how it helps you maintain you code better when extending classes.</p>
<p>In the previous example, we printed some general information about a record to the screen.<br />
Let&#8217;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. </p>
<p>We will need to create a new class that extends <a href="http://www.artofcreation.be/2010/02/15/protected-new-and-construct-part-1/">the class we created earlier</a>. </p>
<p><strong>1. ClassDeclaration KLFShowRecordInfo_CustTable</strong><br />
In the ClassDeclaration, we include the keyword &#8216;extends&#8217; to specify that our new class extends the functionality of the class KLFShowRecordInfo.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> KLFShowRecordInfo_CustTable <span style="color: #0000ff;">extends</span> KLFShowRecordInfo<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>2. Construct method KLFShowRecordInfo_CustTable</strong><br />
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.<br />
Because we extend a class that contains this parm method, we don&#8217;t need to add it to this class again.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFShowRecordInfo_CustTable construct<span style="color: #000000;">&#40;</span>Common _record<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFShowRecordInfo_CustTable kLFShowRecordInfo_CustTable;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; kLFShowRecordInfo_CustTable <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> KLFShowRecordInfo_CustTable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; kLFShowRecordInfo_CustTable.<span style="color: #000000;">parmRecord</span><span style="color: #000000;">&#40;</span>_record<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFShowRecordInfo_CustTable;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>3. Run method KLFShowRecordInfo_CustTable</strong><br />
In the run method, we print data from the CustTable record to screen.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; CustTable custTable;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; custTable <span style="color: #00007f;">=</span> this.<span style="color: #000000;">parmRecord</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>custTable.<span style="color: #000000;">AccountNum</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>custTable.<span style="color: #000000;">Name</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>You could call the super() method if you want the general information to be printed as well. </p>
<p><strong>4. Construct method KLFShowRecordInfo</strong><br />
The only thing we need to change to the existing class is the construct method.<br />
Modify it like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFShowRecordInfo construct<span style="color: #000000;">&#40;</span>Common _record<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFShowRecordInfo kLFShowRecordInfo;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">switch</span> <span style="color: #000000;">&#40;</span>_record.<span style="color: #000000;">TableId</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> <span style="color: #0000ff;">tablenum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #000000;">&#41;</span><span style="color: #00007f;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kLFShowRecordInfo <span style="color: #00007f;">=</span> KLFShowRecordInfo_CustTable<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span>_record<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">default</span> <span style="color: #00007f;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kLFShowRecordInfo <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> KLFShowRecordInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kLFShowRecordInfo.<span style="color: #000000;">parmRecord</span><span style="color: #000000;">&#40;</span>_record<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFShowRecordInfo;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>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.</p>
<p><strong>5. TestJob</strong><br />
Let&#8217;s test it:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> KLF_TestRecordInfo<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; CustTable custTable;<br />
&nbsp; &nbsp; VendTable vendTable;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">select</span> <span style="color: #0000ff;">firstonly</span> custTable;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">select</span> <span style="color: #0000ff;">firstonly</span> vendTable;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// custTable:</span><br />
&nbsp; &nbsp; KLFShowRecordInfo<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span>custTable<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">run</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;----&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// vendTable:</span><br />
&nbsp; &nbsp; KLFShowRecordInfo<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span>vendTable<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">run</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Output:</p>
<blockquote><p>00000001<br />
IKEA<br />
&#8212;-<br />
VendTable</p></blockquote>
<p><strong>Conclusion</strong><br />
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. </p>
<p>To sum up:<br />
My basic recommendations when creating classes are:<br />
- Use a protected new method<br />
- Use a construct method<br />
- Use parm methods<br />
- Create a run method that contains your business logic<br />
- Instead of modifying existing classes, create new ones that extend existing</p>
<p>If you disagree, be sure to leave a comment :). </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/02/18/protected-new-and-construct-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Protected new and construct &#8211; Part 1</title>
		<link>http://www.artofcreation.be/2010/02/15/protected-new-and-construct-part-1/</link>
		<comments>http://www.artofcreation.be/2010/02/15/protected-new-and-construct-part-1/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 09:16:31 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=453</guid>
		<description><![CDATA[In this post I want to show you the use of the new() and construct() method when instantiating classes using a simple example class. This is a design that is used a lot an AX. It is best practice to set the new method as protected (MSDN), and it is also best practice to have [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I want to show you the use of the new() and construct() method when instantiating classes using a simple example class. This is a design that is used a lot an AX. </p>
<p>It is best practice to set the new method as protected (<a href="http://msdn.microsoft.com/en-us/library/aa594035.aspx">MSDN</a>), and it is also best practice to have a construct method in your class (<a href="http://msdn.microsoft.com/en-us/library/aa637432.aspx">MSDN</a>). So let&#8217;s see how we should implement these best practices.</p>
<p>Like a good recipe, a good class has a few important ingredients:<br />
- Parm methods for variables<br />
- A protected new method<br />
- A static contructor<br />
- A run method</p>
<p><strong>1. ClassDeclaration</strong></p>
<p>As an example, we will create a new class: KLFShowRecordInfo<br />
The function of the class will be to display some information about a record.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> KLFShowRecordInfo<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Common record;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This class has one variable, a record. The type is Common so we can store any record in this variable.</p>
<p><strong>2. Parm method</strong></p>
<p>First thing is to create a parm-method for this variable (<a href="http://www.artofcreation.be/2009/08/27/editorscripts-create-parm-method-from-class-declaration/">created using parmFromCD editorscript</a>):</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> Common parmRecord<span style="color: #000000;">&#40;</span>Common _record <span style="color: #00007f;">=</span> record<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
;<br />
&nbsp; &nbsp; record <span style="color: #00007f;">=</span> _record;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> record;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Your code will be much cleaner and easier to debug when you use parameter methods.</p>
<p><strong>3. New Method</strong></p>
<p>Next, we will override the new method and make it protected:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #0000ff;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Methods that are declared as protected can only be called from methods in the class and in subclasses of the class where the protected method is declared. When we want to create an instance of this class from an other class, we&#8217;ll have to call the construct method.<br />
When calling the new() method in any other class, we would get the following compiler error:</p>
<blockquote><p>The method is declared protected and may only be called from methods in classes derived from KLFShowRecordInfo.</p></blockquote>
<p><strong>4. Construct method</strong></p>
<p>When using the editorscript to create a construct() method (Right click &#8211; Scripts &#8211; template &#8211; method &#8211; construct), it will look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFShowRecordInfo construct<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> KLFShowRecordInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>However, I always rewrite it like like this because it is easier to extend afterwards:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFShowRecordInfo construct<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFShowRecordInfo kLFShowRecordInfo;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; kLFShowRecordInfo <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> KLFShowRecordInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFShowRecordInfo;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Now we&#8217;ll have to modify the construct method to receive a parameter, and use the parm method to set the variable in our class:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> KLFShowRecordInfo construct<span style="color: #000000;">&#40;</span>Common _record<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; KLFShowRecordInfo kLFShowRecordInfo;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; kLFShowRecordInfo <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> KLFShowRecordInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; kLFShowRecordInfo.<span style="color: #000000;">parmRecord</span><span style="color: #000000;">&#40;</span>_record<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> kLFShowRecordInfo;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>5. Run method</strong><br />
All we need now is some logic to add to this class. Most of the time, I name the method &#8216;run&#8217;, but you can give it any name you like. The method will display the name of the table of the record you pass to it.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; ;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">tableid2name</span><span style="color: #000000;">&#40;</span>this.<span style="color: #000000;">parmRecord</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">TableId</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p><strong>6. Test job</strong><br />
Next, a job to test it:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> KLF_TestRecordInfo<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; CustTable custTable;<br />
&nbsp; &nbsp; ;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">select</span> <span style="color: #0000ff;">firstonly</span> custTable;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #007f00;">// this will not work because the new method is protected</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// kLFShowRecordInfo = new KLFShowRecordInfo();</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; KLFShowRecordInfo<span style="color: #00007f;">::</span><span style="color: #000000;">construct</span><span style="color: #000000;">&#40;</span>custTable<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">run</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>You can see that we only need one line of code to call our class, and we don&#8217;t need a variable, which makes the code cleaner. </p>
<p>Output:</p>
<blockquote><p>CustTable</p></blockquote>
<p><strong>Conclusion</strong><br />
When creating a class, start with declaring the new method as protected. This will force you to use a static construct() method. This in turn will force you to think twice about the structure of your class, and will result in cleaner/better code with a more &#8216;AX-y&#8217; feel.</p>
<p>In <a href="http://www.artofcreation.be/2010/02/18/protected-new-and-construct-part-2/">part 2</a>, I will demonstrate how this design principle helps you to maintain your code when extending functionality. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/02/15/protected-new-and-construct-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

