<?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>Learning by Experience &#187; architecture</title> <atom:link href="http://www.inze.be/andries/category/architecture/feed/" rel="self" type="application/rss+xml" /><link>http://www.inze.be/andries</link> <description>Java, Project Management, Life and anything else.</description> <lastBuildDate>Mon, 09 Jan 2012 21:38:00 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Static Application Data</title><link>http://www.inze.be/andries/2008/12/09/static-application-data/</link> <comments>http://www.inze.be/andries/2008/12/09/static-application-data/#comments</comments> <pubDate>Tue, 09 Dec 2008 21:08:02 +0000</pubDate> <dc:creator>Andries Inzé</dc:creator> <category><![CDATA[architecture]]></category> <guid
isPermaLink="false">http://www.inze.be/andries/?p=85</guid> <description><![CDATA[Every project comes with it own application data: the minimal data that is needed to actually start running the application. Typically their is some type or code object that is used throughout the application. When using an ORM tool, it&#8217;s not always straightforward to select the right code. Typically, I&#8217;d advice and create some TypeEnum [...]]]></description> <content:encoded><![CDATA[<p>Every project comes with it own application data: the minimal data that is needed to actually start running the application. Typically their is some <em>type </em>or <em>code</em> object that is used throughout the application. When using an ORM tool, it&#8217;s not always straightforward to <em>select</em> the right code.<br
/> Typically, I&#8217;d advice and create some <em>TypeEnum</em> which has a code that uniquely identifies the record in the database. The disadvantage against such an approach is that everywhere you need these <em>static</em> objects, you need to retrieve them. This clutters the code and poses (a ver minor) extra load on the server.</p><p>A solution:</p><h2>Static Application Data</h2><p>Or as I like to call it: SAD <img
src='http://www.inze.be/andries/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> kuch</p><p>Anyway, the solution to this problem is easy and elegant. At startup, the application initialises a SAD object, which is nothing more then a bean that contains some <em>public static final</em> fields that are filled in when creating the SAD singleton.</p><p>Typically you&#8217;ll get something like this:</p><pre lang="java">public class SADFactory {
	public final MyCodeObject myCodeObject1;
	public final MyCodeObject myCodeObject2;
	public SADFactory(MyService service) {
		myCodeObject1 = service.findById(1L);
		myCodeObject2 = service.findById(2L);
	}
}</pre><p>Now we are set! We can use our special static objects anywhere in our application.</p><p>Using Spring, you can inject the SADFactory where required (services, managed beans,&#8230;). In this fashion the business logic of selecting the right application from the database is contained in a single class.</p><p>Way better then using an enum to do the selecting&#8230;</p><p>Wiith thanks to Yoeri Roels for coming up with this great idea!</p><p>Andries</p> ]]></content:encoded> <wfw:commentRss>http://www.inze.be/andries/2008/12/09/static-application-data/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>The BusinessException pattern</title><link>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/</link> <comments>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/#comments</comments> <pubDate>Mon, 18 Aug 2008 21:08:58 +0000</pubDate> <dc:creator>Andries Inzé</dc:creator> <category><![CDATA[architecture]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[patterns]]></category> <guid
isPermaLink="false">http://www.inze.be/andries/?p=41</guid> <description><![CDATA[This exception is probably one of the most common home-made exceptions, one that pops up in almost every project. I'll try and show you how it works and what the advantages are.]]></description> <content:encoded><![CDATA[<p>This exception is probably one of the most common<em> home-made</em> exceptions, one that pops up in almost every project. I&#8217;ll try and show you how it works and what the advantages are.</p><p>One of the &#8220;problems&#8221; of doing all the business logic in the Service (or UseCase, Business) Layer is getting the right exception message back onto the screen. Lot&#8217;s of times people invent all kinds of home-brewed exceptions: e.g. <em>ResultNotFoundException</em>, <em>InsufficientFundsException</em>, <em>DateTooFarIntoFutureException</em>, &#8230;<br
/> Another approach, which is far worse, is to let specialized exceptions like a <em>ConstraintViolation </em>(hibernate), <em>UserException </em>(axis) and many others. It couples the front-end to back end.</p><p>When defining custom exceptions for every problem, you force the controller to catch all the exceptions that could be thrown separately. Adding a new exception can be cumbersome, and it does not really matter if you define them as checked or unchecked, unless you plan to catch the remaining exceptions with a generic info message like: something went wrong&#8230;</p><h2>What&#8217;s the correct solution?</h2><p>Here comes the BusinessException</p><pre>public class BusinessException
    extends RuntimeException {
	/** A code indicating the cause of
            the exception. */
	private String code;
	/** The exception that is wrapped by
            the business component. */
	private Exception wrappedException;
	/**
	 * Constructor.
	 *
	 * @param code The code indicating the
         * cause of the exception.
	 * @param wrappedException The exception
         * that is wrapped by the business
         * component.
	 */
	public BusinessException(String code,
          Exception wrappedException) {
		this.code = code;
		this.wrappedException =
                  wrappedException;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Exception getWrappedException() {
		return wrappedException;
	}
	public void setWrappedException(
           Exception wrappedException) {
		this.wrappedException = wrappedException;
	}
}</pre><p>When a business rule validation has been detected, a new BusinessException should be throw.</p><p>When an exception is thrown from any layer beneath the Controller layer, it should be wrapped by the BusinessException.</p><p>The <em>code</em> is the key for translation by the view renderer.</p><h2>Placing this in the architecture</h2><p>When we place the BusinessException into a widely use architectural stack we come up with something like this:</p><p
style="text-align: center;"><a
title="BusinessException architecture" href="http://www.inze.be/andries/wp-content/architecture.jpg" target="_blank"><img
class="aligncenter size-medium wp-image-56" title="architecture" src="http://www.inze.be/andries/wp-content/architecture-300x160.jpg" alt="" width="300" height="160" /></a></p><h2>Advantages</h2><p>The advantages are clear:</p><ul><li>The controller and other layers are loosely coupled (e.g. no DAO dependency in Controller)</li><li>The transaction can be rolled back, since the exception is thrown.</li><li>The controllers aren&#8217;t forced to catch many exceptions.</li><li>No need for those custom made exceptions. One exception to rule them all!</li></ul><h2>BusinessException versus other exceptions</h2><p>The businessException should wrap a recoverable violation or exception, that can be displayed in the screen. Transaction is rolled back, and the user can change the input and/or try again.</p><p>Normal exceptions are unrecoverable. The transaction is rolled back, and the user is directed to an error page.</p><p>It&#8217;s not the point to wrap every exception, but only the expected ones. Don&#8217;t wrap a It&#8217;s a really simple pattern, but a very powerful one.</p><p>In essence 4 scenario&#8217;s can happen:</p><ol><li>The first level fails. Typically this is something like a missing required field. The user is redirected to the same page to fix the problem.</li><li>The second validation fails: a business exception is thrown.  Typically this is a business rule that failed, for instance a balance is too low to complete a purchase. It also could be an exception wrapped by the BusinessException. For instance when the database row has changed after you loaded it.</li><li>Everything succeeded, the business layer returned normally.</li><li>The business layer threw an exception (not a BusinessException), the page is redirected to the error page. Unrecoverable, should only be on programming bug.</li></ol><h2>Conclusion</h2><p>A lot has to be said for straight and simple win-win patterns. This one is definately one of those.</p> ]]></content:encoded> <wfw:commentRss>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> </channel> </rss>
