<?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>try {} catch () &#187; Ext-JS</title>
	<atom:link href="http://blog.gerardin.info/archives/tag/extjs/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.gerardin.info</link>
	<description>Java, Agile, the Web and other nice things</description>
	<lastBuildDate>Tue, 14 Jun 2011 09:48:37 +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>Why Ext-GWT MVC is broken</title>
		<link>http://blog.gerardin.info/archives/40</link>
		<comments>http://blog.gerardin.info/archives/40#comments</comments>
		<pubDate>Wed, 18 Mar 2009 13:13:17 +0000</pubDate>
		<dc:creator>Olivier Gérardin</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Ext-GWT]]></category>
		<category><![CDATA[Ext-JS]]></category>
		<category><![CDATA[gwt-ext]]></category>
		<category><![CDATA[GXT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://gerardin.info/wordpress/?p=40</guid>
		<description><![CDATA[Introducing Ext-GWT Our current project is a GWT project, and we&#8217;re using Ext-GWT as a widget library and framework. Ext-GWT (formerly MyGWT) is a &#8220;pure&#8221; Java/GWT port of the well known Ext-JS JavaScript library, not to be mistaken for gwt-ext, which is a Java/GWT wrapper around Ext-JS&#8230; Still following? In short, gwt-ext requires Ext-JS, while [...]]]></description>
			<content:encoded><![CDATA[<h3>Introducing Ext-GWT</h3>
<p>Our current project is a <a href="http://code.google.com/webtoolkit/overview.html" target="_blank" class="liexternal">GWT</a> project, and we&#8217;re using <a href="http://extjs.com/products/gxt/" target="_blank" class="liexternal">Ext-GWT</a> as a widget library and framework.</p>
<p>Ext-GWT (formerly MyGWT) is a &#8220;pure&#8221; Java/GWT <em>port</em> of the well known <a href="http://extjs.com/products/extjs/" target="_blank" class="liexternal">Ext-JS</a> JavaScript library, not to be mistaken for <a href="http://gwt-ext.com/" target="_blank" class="liexternal">gwt-ext</a>, which is a Java/GWT <em>wrapper</em> around Ext-JS&#8230; Still following?</p>
<p>In short, gwt-ext requires Ext-JS, while Ext-GWT doesn&#8217;t. Ext-GWT is from the same company that makes Ext-JS (appropriatley called Ext), while gwt-ext is an independant work. Also, since version 2.1, Ext-JS removed <a href="http://www.gnu.org/licenses/lgpl.html" target="_blank" class="liexternal">LGPL</a> from its licensing options, which effectively prevents software like gwt-ext from using it, so it&#8217;s stuck with v 2.0.x.</p>
<p>Anyway, this isn&#8217;t my point. Ext-GWT in its version 1.2 features a so-called &#8220;lightweight&#8221; MVC implementation, which we use on our project. Unfortunately, this implementation is quite flawed, and this isn&#8217;t harmless because it allows all sorts of problems to arise, that a clean MVC should prevent.</p>
<h3>YAAADMVC (Yet another attempt at defining MVC)</h3>
<p>First, it&#8217;s hard to come by a good explanation of MVC.  <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank" rel="nofollow" class="liwikipedia">according to Wikipedia</a>, it was first described in 1979 by <a href="http://en.wikipedia.org/wiki/Trygve_Reenskaug" title="Trygve Reenskaug" rel="nofollow" class="liwikipedia">Trygve Reenskaug</a>, and since then it has seen innumerous variations and implementations, some bright, some less bright&#8230; Judging by the number of incorrect or plainly wrong explanations you can find on the web, there must be something inherently hard to get in it. However, I&#8217;ll have my shot at explaining.</p>
<p>MVC is about roles. It defines three roles, namely Model, View and Controller, and how they interact. Some have interpreted these roles to be software components, which led to MVC frameworks, and some as architectural components, which led for example to so-called MVC web frameworks. Here we concentrate on the software component approach.</p>
<p>So what are those three roles?</p>
<ul>
<li>The <strong>model</strong> holds the data and provides an interface to manipulate the data.</li>
<li>The <strong>view</strong> is a graphical representation of the model, and possibly also user controls (such as buttons, etc.). It is also able to generate events to any interested listeners, for example to signal a user action (button pressed, etc.)</li>
<li>The <strong>controller</strong> pilots both the view and the model. It reacts to user interaction in the appropriate manner, including possibly updating the model.</li>
</ul>
<p>This definition might not be universal, and some might disagree on some points, but it is viable.</p>
<p>Some important points:</p>
<ul>
<li>The model does not know anything of views or controllers. Ideally, it is observable and notifies its changes to any interested listeners.</li>
<li>The view only has to know its model, at least enough of it to build the display. If the model is observable, the view will register as a change listener and update the display whenever the model changes. The view does not need to know its controller (and indeed you could use a view without a controller if you didn&#8217;t need user interaction)</li>
<li>The controller listens to events sent by the view, and only the controller should have the intelligence to know what to do when something happens, like a user pressing a button.</li>
</ul>
<p>Why is this important? because it cleanly separates the responsibility of each component, and we know that leads to code that is more readable, more testable and more maintainable. Less coupling, more consistency: the basis of a good architecture.</p>
<p>Here&#8217;s the best diagram I&#8217;ve found describing the relationships between MVC components (from <a href="http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/mvc.html" class="liexternal">http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/mvc.html</a>):</p>
<div id="attachment_70" class="wp-caption aligncenter" style="width: 594px"><a href="http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/mvc.html" title="Image Source " target="_blank" class="liimagelink"><img class="size-full wp-image-70" title="mvc3" src="http://blog.gerardin.info/wp-content/uploads/2009/03/mvc3.jpg" alt="Relationships between components in a MVC architecture" width="584" height="584" /></a><p class="wp-caption-text">Relationships between components in a MVC architecture</p></div>
<h3>MVC in Ext-GWT</h3>
<p>So let&#8217;s have a look at Ext-GWT&#8217;s MVC implementation. This is the constructor of class View:</p>
<pre lang="java">  /**
   * Creates a new view instance.
   *
   * @param controller the parent controller
   */
  public View(Controller controller) {
    this.controller = controller;
  }</pre>
<p>Yes, the view knows its controller&#8230; Why is it so? Looking further into the source code, we can find the explanation:</p>
<pre lang="java">  /**
   * Called when a view needs to pass an event to it's controller.
   *
   * @param event the app event
   */
  protected void fireEvent(AppEvent event) {
    Controller c = controller;
    while (c != null) {
      if (c.canHandle(event)) {
        c.handleEvent(event);
      }
      c = c.parent;
    }
  }</pre>
<p>So apparently, the view knows its controller only to be able to send events to it&#8230; In a correctly designed system, the view should allow the controller to register itself as an event listener, thus eliminating an unnecessary dependency. We can also notice that the knowledge that controllers have a parent, and that the event should be propagated to all ancestor controllers, is all built right there into the view&#8230; ouch.</p>
<p>But there&#8217;s more. The handleEvent() method in class Controller is public, making it possible for anyone to call it. The problem is that, as we can see above, the handleEvent() method should only be called if canHandle() is true. Making handleEvent() a public method allows anyone to bypass this rule.</p>
<p>We find a different flavour of the same design flaw in method Controller.forwardToView():</p>
<pre lang="java">  /**
   * Forward an event to a view. Ensures the view is initialized before
   * forwarding the event.
   *
   * @param view the view to forward the event
   * @param event the event to be forwarded
   */
  public static void forwardToView(View view, AppEvent event) {
    if (!view.initialized) {
      view.initialize();
      view.initialized = true;
    }
    view.handleEvent(event);
  }</pre>
<p>Here, we can see that the method View.initialize() must be called once before the first call to handleEvent(). But this is enforced only here, so it&#8217;s easy to call View.handleEvent() directly and break this rule. If handleEvent() depends on something that is done  in initialize(), you can imagine what can happen. You could also call initialize() and not set initialized to true, then it would be called again the next time&#8230;</p>
<p>You will also notice that the existence of the Controller.forwardToView() method implies that the controller can forward events to a view. This is clearly not appropriate because what the controller really does is send <strong>orders</strong> to the view. And since the controller must know its view, it&#8217;s best to simply call a method of the view when it needs to alter it.</p>
<p>Of course you could send orders disguised as events, but it has two major drawbacks:</p>
<ul>
<li>it complicates the control flow and makes it very hard to understand</li>
<li>it prevents static code analysis and refactoring that is offered by modern IDEs.</li>
</ul>
<p>In addition, for some reason Ext-GWT event types are not defined as Java enum members, but mere integers. This makes it very difficult to identify an event from just its code, for example when stepping through code.</p>
<h3>Bottom line: don&#8217;t use Ext-GWT&#8217;s MVC</h3>
<p>Ext-GWT has a lot of nice things to offer, but clearly its MVC implementation is not one of them. More could be said about it, but I think you get the picture: <strong>don&#8217;t use it</strong>. Write your own clean MVC implementation from scratch, you&#8217;ll save yourself a lot of trouble.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.gerardin.info/archives/40/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

