<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Two interesting ways to architect Apex triggers</title>
	<atom:link href="http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/feed" rel="self" type="application/rss+xml" />
	<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers</link>
	<description></description>
	<lastBuildDate>Thu, 04 Mar 2010 13:37:32 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Interesting ways of architecting Apex Triggers &#171; jonathanrico.</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-206270</link>
		<dc:creator>Interesting ways of architecting Apex Triggers &#171; jonathanrico.</dc:creator>
		<pubDate>Tue, 26 Jan 2010 05:55:02 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-206270</guid>
		<description>[...] came across a blog post at gokubi.com (http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers) about some interesting ways of architecting Apex [...]</description>
		<content:encoded><![CDATA[<p>[...] came across a blog post at gokubi.com (<a href="http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers" rel="nofollow">http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers</a>) about some interesting ways of architecting Apex [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wes</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-191514</link>
		<dc:creator>Wes</dc:creator>
		<pubDate>Tue, 29 Sep 2009 16:03:23 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-191514</guid>
		<description>Building on Jon&#039;s idea why not make the trigger type available as method a parameter and/or return type? That way frameworks could be developed and triggers would have tiny implementations.

In the Mike vs George debate I&#039;d side with Mike. I&#039;d disagree with static classes being more easy to maintain. A clean OO class architecture will always win out on the maintainability debate mostly because of potential class hiearchies coupled with good ol&#039; polymorphism. 

That said I am a fan of static classes, but would usually use them in utility classes or architectures that subscribe to factory patterns e.g. If I were using an enum as you have above I might&#039;ve declared it as static in a utility class.</description>
		<content:encoded><![CDATA[<p>Building on Jon&#8217;s idea why not make the trigger type available as method a parameter and/or return type? That way frameworks could be developed and triggers would have tiny implementations.</p>
<p>In the Mike vs George debate I&#8217;d side with Mike. I&#8217;d disagree with static classes being more easy to maintain. A clean OO class architecture will always win out on the maintainability debate mostly because of potential class hiearchies coupled with good ol&#8217; polymorphism. </p>
<p>That said I am a fan of static classes, but would usually use them in utility classes or architectures that subscribe to factory patterns e.g. If I were using an enum as you have above I might&#8217;ve declared it as static in a utility class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Leach</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189480</link>
		<dc:creator>Mike Leach</dc:creator>
		<pubDate>Tue, 08 Sep 2009 19:39:23 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189480</guid>
		<description>It&#039;s always convenient when a static method pops up in autocomplete (or Intellisense) and always worth exploring.

Thread safety is really the only issue to review when using static methods. Assuming the static method is not dependent on any globally scoped objects, then either approach is fine.</description>
		<content:encoded><![CDATA[<p>It&#8217;s always convenient when a static method pops up in autocomplete (or Intellisense) and always worth exploring.</p>
<p>Thread safety is really the only issue to review when using static methods. Assuming the static method is not dependent on any globally scoped objects, then either approach is fine.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gokubi.com &#187; Blog Archive &#187; Apex Architecture</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189465</link>
		<dc:creator>gokubi.com &#187; Blog Archive &#187; Apex Architecture</dc:creator>
		<pubDate>Tue, 08 Sep 2009 16:55:37 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189465</guid>
		<description>[...] gokubi.com       &#171; Two interesting ways to architect Apex triggers [...]</description>
		<content:encoded><![CDATA[<p>[...] gokubi.com       &laquo; Two interesting ways to architect Apex triggers [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George Scott</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189318</link>
		<dc:creator>George Scott</dc:creator>
		<pubDate>Sun, 06 Sep 2009 19:07:11 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189318</guid>
		<description>Mike&#039;s approach is definitely the more object-oriented approach and a cleaner design from that perspective.

There are a couple of reasons I would use static methods over the object-oriented approach: maintenance and performance.  

If a lot of people will be editing the code over time (maintenance) and some of them are not professional programmers, it is sometimes best not to introduce object-oriented concepts if they are not needed.  This really depends on the skill set of those maintaing the code.

On the performance side, creating an object has a cost associated with it, both in terms of creation, and later when it has to be garbage-collected by the system.  So I tend to avoid creating temporary objects if I can avoid it.  This is just a habit after years of tuning server-side Java code, and may not be relevant for Apex.</description>
		<content:encoded><![CDATA[<p>Mike&#8217;s approach is definitely the more object-oriented approach and a cleaner design from that perspective.</p>
<p>There are a couple of reasons I would use static methods over the object-oriented approach: maintenance and performance.  </p>
<p>If a lot of people will be editing the code over time (maintenance) and some of them are not professional programmers, it is sometimes best not to introduce object-oriented concepts if they are not needed.  This really depends on the skill set of those maintaing the code.</p>
<p>On the performance side, creating an object has a cost associated with it, both in terms of creation, and later when it has to be garbage-collected by the system.  So I tend to avoid creating temporary objects if I can avoid it.  This is just a habit after years of tuning server-side Java code, and may not be relevant for Apex.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189306</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 06 Sep 2009 15:17:55 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189306</guid>
		<description>George, I&#039;ve never gotten my head around when I should chose a static implementation, and when I should make the class one that gets instantiated. In this case, it seems like I Mike&#039;s idea to instantiate and then call individual methods is pretty efficient. In the constructor we can grab the two trigger sets into local variables for use later in the other methods.

It keeps the signatures of the methods clean, but they can only be called when the class has been instantiated.

Would love to hear your thoughts on static vs. instantiated in this case.

Thanks for all the great comments everyone!</description>
		<content:encoded><![CDATA[<p>George, I&#8217;ve never gotten my head around when I should chose a static implementation, and when I should make the class one that gets instantiated. In this case, it seems like I Mike&#8217;s idea to instantiate and then call individual methods is pretty efficient. In the constructor we can grab the two trigger sets into local variables for use later in the other methods.</p>
<p>It keeps the signatures of the methods clean, but they can only be called when the class has been instantiated.</p>
<p>Would love to hear your thoughts on static vs. instantiated in this case.</p>
<p>Thanks for all the great comments everyone!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189257</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sat, 05 Sep 2009 21:10:40 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189257</guid>
		<description>That&#039;s an interesting take on it Mike. It&#039;s similar to George&#039;s example but passes the sets in the controller. I like that as then you&#039;re only handling them once, and then each method can process those sets appropriately.</description>
		<content:encoded><![CDATA[<p>That&#8217;s an interesting take on it Mike. It&#8217;s similar to George&#8217;s example but passes the sets in the controller. I like that as then you&#8217;re only handling them once, and then each method can process those sets appropriately.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189256</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sat, 05 Sep 2009 21:08:47 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189256</guid>
		<description>Thanks for that example George. Static calls make sense, and seem to in this case. Thanks.</description>
		<content:encoded><![CDATA[<p>Thanks for that example George. Static calls make sense, and seem to in this case. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Leach</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189244</link>
		<dc:creator>Mike Leach</dc:creator>
		<pubDate>Sat, 05 Sep 2009 18:29:17 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189244</guid>
		<description>On second thought, I&#039;d probably prefer calling unique (possibly abstract) methods for each event type in the Factory container.

MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old);

//switch or if statement block
process.OnBeforeInsert();
process.OnAfterInsert();
process.OnBeforeUpdate();
process.OnAfterInsert();


Microsoft really killed their Win32 API with too many macros/structs/enums. .NET corrected this mistake.</description>
		<content:encoded><![CDATA[<p>On second thought, I&#8217;d probably prefer calling unique (possibly abstract) methods for each event type in the Factory container.</p>
<p>MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old);</p>
<p>//switch or if statement block<br />
process.OnBeforeInsert();<br />
process.OnAfterInsert();<br />
process.OnBeforeUpdate();<br />
process.OnAfterInsert();</p>
<p>Microsoft really killed their Win32 API with too many macros/structs/enums. .NET corrected this mistake.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George Scott</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189243</link>
		<dc:creator>George Scott</dc:creator>
		<pubDate>Sat, 05 Sep 2009 17:58:25 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189243</guid>
		<description>Interesting post Steve.  I&#039;m always curious about different design patterns for Apex code.

Being a long time Java programmer, I have one question for you.  Is there any reason you are performing the logic in a constructor versus a public static method in the class?  This is the approach I typically use, no need for enums or multiple classes.  For example:

if(Trigger.isInsert &amp;&amp; Trigger.isBefore){
   MyObjectClass.beforeInsert(Trigger.new, Trigger.old);
}
else if (Trigger.isAfter &amp;&amp; Trigger.isInsert ){
   MyObjectClass.afterInsert(Trigger.new, Trigger.old);
}
...

In the MyObjectClass you just define the methods as static so they can be called without an object instance:

public class MyObjectClass {

  public static void beforeInsert(MyObject__c[] myObjects, MyObject__c[] myOldObjects) {
    ...
  }

  pubilc static void afterInsert(MyObject__c[] myObjects, MyObject__c[] myOldObjects) {
    ...
  }</description>
		<content:encoded><![CDATA[<p>Interesting post Steve.  I&#8217;m always curious about different design patterns for Apex code.</p>
<p>Being a long time Java programmer, I have one question for you.  Is there any reason you are performing the logic in a constructor versus a public static method in the class?  This is the approach I typically use, no need for enums or multiple classes.  For example:</p>
<p>if(Trigger.isInsert &amp;&amp; Trigger.isBefore){<br />
   MyObjectClass.beforeInsert(Trigger.new, Trigger.old);<br />
}<br />
else if (Trigger.isAfter &amp;&amp; Trigger.isInsert ){<br />
   MyObjectClass.afterInsert(Trigger.new, Trigger.old);<br />
}<br />
&#8230;</p>
<p>In the MyObjectClass you just define the methods as static so they can be called without an object instance:</p>
<p>public class MyObjectClass {</p>
<p>  public static void beforeInsert(MyObject__c[] myObjects, MyObject__c[] myOldObjects) {<br />
    &#8230;<br />
  }</p>
<p>  pubilc static void afterInsert(MyObject__c[] myObjects, MyObject__c[] myOldObjects) {<br />
    &#8230;<br />
  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189236</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sat, 05 Sep 2009 16:01:52 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189236</guid>
		<description>Great idea Jon.

There are two pieces of data: operation and type. Operation would be an enum for the DML operation, Type could be before or after. Type could be an enum. Or you could just use the isBefore boolean.

Trigger.Operation enum and Trigger.Type enum (or the current Trigger.isBefore)

Or you could flatten everything into Trigger.Action, an enum that would combine the two values to &quot;Before Insert&quot; and &quot;After Insert&quot;, etc.

 Sounds like we need an idea.</description>
		<content:encoded><![CDATA[<p>Great idea Jon.</p>
<p>There are two pieces of data: operation and type. Operation would be an enum for the DML operation, Type could be before or after. Type could be an enum. Or you could just use the isBefore boolean.</p>
<p>Trigger.Operation enum and Trigger.Type enum (or the current Trigger.isBefore)</p>
<p>Or you could flatten everything into Trigger.Action, an enum that would combine the two values to &#8220;Before Insert&#8221; and &#8220;After Insert&#8221;, etc.</p>
<p> Sounds like we need an idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Leach</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189235</link>
		<dc:creator>Mike Leach</dc:creator>
		<pubDate>Sat, 05 Sep 2009 15:56:27 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189235</guid>
		<description>Good question. Why is the public enum declared twice? Does Apex not support global namespace scope?</description>
		<content:encoded><![CDATA[<p>Good question. Why is the public enum declared twice? Does Apex not support global namespace scope?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Mountjoy</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189233</link>
		<dc:creator>Jon Mountjoy</dc:creator>
		<pubDate>Sat, 05 Sep 2009 15:29:05 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189233</guid>
		<description>It strikes me that what is really needed here, to induce the DRY principle, is to have the type of trigger (result of Trigger.isInsert, Trigger.isAfter etc.) available as an Enum in the first place. Perhaps an Idea is called for?  Something like: Trigger.triggerAction.  Otherwise the skeleton code you have in the second case is going to repeated for each different trigger you write - and worse, you have no alternative but to repeat it.</description>
		<content:encoded><![CDATA[<p>It strikes me that what is really needed here, to induce the DRY principle, is to have the type of trigger (result of Trigger.isInsert, Trigger.isAfter etc.) available as an Enum in the first place. Perhaps an Idea is called for?  Something like: Trigger.triggerAction.  Otherwise the skeleton code you have in the second case is going to repeated for each different trigger you write &#8211; and worse, you have no alternative but to repeat it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Leach</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189182</link>
		<dc:creator>Mike Leach</dc:creator>
		<pubDate>Fri, 04 Sep 2009 23:26:44 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189182</guid>
		<description>I like the enum declaration. Great idea.

Another thing that helps readability is to just set transaction state in the constructor then call an Invoke() method on the trigger to do the actual work.

Otherwise the lvalue stuff from the constructor just looks like dead code.

MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeInsert);
process.Invoke(); // or alternatively process.Execute();

Of course, this all begs the question &quot;Does Apex require an lvalue when calling new?&quot; (I don&#039;t know).

Would be even nicer to use a fluent expression like this:
new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeInsert).Invoke();</description>
		<content:encoded><![CDATA[<p>I like the enum declaration. Great idea.</p>
<p>Another thing that helps readability is to just set transaction state in the constructor then call an Invoke() method on the trigger to do the actual work.</p>
<p>Otherwise the lvalue stuff from the constructor just looks like dead code.</p>
<p>MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeInsert);<br />
process.Invoke(); // or alternatively process.Execute();</p>
<p>Of course, this all begs the question &#8220;Does Apex require an lvalue when calling new?&#8221; (I don&#8217;t know).</p>
<p>Would be even nicer to use a fluent expression like this:<br />
new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeInsert).Invoke();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Ross</title>
		<link>http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers/comment-page-1#comment-189179</link>
		<dc:creator>Jeremy Ross</dc:creator>
		<pubDate>Fri, 04 Sep 2009 23:03:59 +0000</pubDate>
		<guid isPermaLink="false">http://gokubi.com/?p=906#comment-189179</guid>
		<description>I&#039;m always interested in finding ways to bring traditional good programming practices to salesforce, which is all too often difficult to do.  

In this case, I&#039;m not sure that the second way is much of an improvement.  You still have if isupdate/isbefore blocks in both the trigger and the implementation class.  Why not move the invocation of the someBeforeInsertMethod-type methods to the trigger?  Then your implementation class is cleaner.  Just 4 methods sans big ugly if blocks.  

I&#039;ve also used the Command pattern with some success.  Doing things the &quot;right&quot; way (here comes the rant) is still troublesome in force.com because creating classes, interfaces, etc. in the IDE is slow and laborious, roundtrip saves are slow and there are no namespaces/packages like Java, .NET, etc.  If force.com wants to be considered a serious platform, they have to fix these things.  Until then, I&#039;ll prefer to write non-trivial apps on a different platform.</description>
		<content:encoded><![CDATA[<p>I&#8217;m always interested in finding ways to bring traditional good programming practices to salesforce, which is all too often difficult to do.  </p>
<p>In this case, I&#8217;m not sure that the second way is much of an improvement.  You still have if isupdate/isbefore blocks in both the trigger and the implementation class.  Why not move the invocation of the someBeforeInsertMethod-type methods to the trigger?  Then your implementation class is cleaner.  Just 4 methods sans big ugly if blocks.  </p>
<p>I&#8217;ve also used the Command pattern with some success.  Doing things the &#8220;right&#8221; way (here comes the rant) is still troublesome in force.com because creating classes, interfaces, etc. in the IDE is slow and laborious, roundtrip saves are slow and there are no namespaces/packages like Java, .NET, etc.  If force.com wants to be considered a serious platform, they have to fix these things.  Until then, I&#8217;ll prefer to write non-trivial apps on a different platform.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
