<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3247085757012917195</id><updated>2012-01-26T16:32:02.330-08:00</updated><category term='C#'/><category term='Introduction'/><category term='RoboGuice'/><category term='JAVA'/><category term='ActionBarSherlock'/><category term='Database design'/><category term='Itemrenderer'/><category term='SWC'/><category term='Calendar'/><category term='Dependency Injection'/><category term='Weborb Autofac AMF remoting dependency injection'/><category term='AIR'/><category term='List'/><category term='Moq'/><category term='Flex'/><category term='Weborb FlourineFx AMF remoting dependency injection'/><category term='Threads'/><category term='Generics'/><category term='Android'/><category term='Unit Testing'/><title type='text'>Flex Amusements</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-6213963523800190403</id><published>2011-12-05T21:24:00.001-08:00</published><updated>2012-01-26T16:32:02.353-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Android'/><category scheme='http://www.blogger.com/atom/ns#' term='RoboGuice'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>Some really simple messaging</title><content type='html'>&lt;span class="Apple-style-span" style="font-size: large;"&gt;Update:&lt;/span&gt;&lt;br/&gt;The approach below will work, but &lt;a href="http://code.google.com/p/roboguice/wiki/Events" target="_blank"&gt;Roboguice's Events&lt;/a&gt; are more elegant.&lt;br /&gt;&lt;br /&gt;I've been working through my first non-trivial Android app, and had a need for some simple messaging (&lt;span class="inlineCode"&gt;Intent&lt;/span&gt;s didn't seem like a good fit for simple, internal, app messages).  I came up with the following classes.&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// a message with a simple type-identifierpublic interface IMessage {  String getMessageType();}// a class that subscribes to a messagepublic interface ISubscriber&lt;t extends IMessage&gt; {  void onMessage(T message);}// a simple router sending messages to subscribers, subscribing to and// unsubscribing from messagespublic interface IBus {  &lt;t extends IMessage&gt; void subscribe(T message, ISubscriber&lt;t&gt; callback);  &lt;t extends IMessage&gt; void unsubscribe(T message, ISubscriber&lt;t&gt; callback);  &lt;t extends IMessage&gt; void sendMessage(T message);}]]&gt;&lt;/script&gt;&lt;span class="inlineCode"&gt;IMessage&lt;/span&gt; is simply a message object whose type is identified by a string. My favorite implementation is an &lt;span class="inlineCode"&gt;enum&lt;/span&gt;, but any class would do.  Here's a simple message:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// a simple implementation of IMessagepublic enum TestMessage implements IMessage {  TestDataReady,  TestDataErrored,  Etc;   @Override  public String getMessageType() {    return Integer.toString(hashCode());  }  // we could add additional data here, e.g.   // public String testData;}]]&gt;&lt;/script&gt;&lt;span class="inlineCode"&gt;ISubscriber&amp;lt;T extends IMessage&amp;gt;&lt;/span&gt; describes a simple class which subscribes to a message, having its &lt;span class="inlineCode"&gt;onMessage()&lt;/span&gt; method invoked when that message arrives.  Here's a snippet of an activity and a subscriber:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// in onStart()bus.subscribe(TestMessage.TestDataReady, onTestDataReady);//snipprivate ISubscriber&lt;testmessage&gt; onTestDataReady = new ISubscriber&lt;testmessage&gt;() {@Override  public void onMessage(TestMessage message) {     getIntoReadyStateEtc();  }};// afterwards in onPause()bus.unsubscribe(TestMessage.TestDataReady, onTestDataReady);//snip]]&gt;&lt;/script&gt;&lt;span class="inlineCode"&gt;IBus&lt;/span&gt; simply connects up the messages and subscribers, here's my simple implementation of that.&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// and an implementation of the simple message-router@Singletonpublic class Bus implements IBus {  final Handler handler = new Handler(); // associated with the main thread  long threadId; // store the id of the main thread    public Bus() {    threadId = Thread.currentThread().getId();  }  // message type-identifier -&gt; ArrayList of subscribers  protected ConcurrentHashMap&lt;String, ArrayList&lt;ISubscriber&lt;? extends IMessage&gt;&gt;&gt; subscriptions =       new ConcurrentHashMap&lt;String, ArrayList&lt;ISubscriber&lt;? extends IMessage&gt;&gt;&gt;();  // a class wants to subscribe to a message  @Override  public &lt;t extends IMessage&gt; void subscribe(T message, ISubscriber&lt;t&gt; subscriber) {    String messageType = message.getMessageType();    if (!this.subscriptions.containsKey(messageType)) {      ArrayList&lt;ISubscriber&lt;? extends IMessage&gt;&gt; possiblyExistingList = subscriptions          .putIfAbsent(messageType, new ArrayList&lt;ISubscriber&lt;? extends IMessage&gt;&gt;());           // thanks to Derek Young's post: http://dmy999.com/article/34/correct-use-of-concurrenthashmap      // non-null if another thread already added this value      if (possiblyExistingList == null) {         this.subscriptions.put(messageType, new ArrayList&lt;ISubscriber&lt;? extends IMessage&gt;&gt;());      }    }    this.subscriptions.get(messageType).add(subscriber);  }    // a class is no longer interested in receiving a message  @Override  public &lt;t extends IMessage&gt; void unsubscribe(T message, ISubscriber&lt;t&gt; subscriber) {    String messageType = message.getMessageType();        for (ISubscriber&lt;? extends IMessage&gt; existingSubscriber : subscriptions.get(messageType)) {      if (existingSubscriber == subscriber) {        this.subscriptions.get(messageType).remove(existingSubscriber);        return;      }    }  }  // sends a message to all the relevant subscribers  @SuppressWarnings("unchecked")  @Override  public &lt;t extends IMessage&gt; void sendMessage(T message) {    // if this method is not invoked on the UI thread, send the message on the UI thread    if(Thread.currentThread().getId() != threadId) {      handler.post(new SendMessageOnUiThread&lt;t&gt;(message));      return;    }        String messageType = message.getMessageType();        for (ISubscriber&lt;? extends IMessage&gt; subscriber : subscriptions.get(messageType)) {      ((ISubscriber&lt;t&gt;) subscriber).onMessage(message);    }  }    // the class name says it all  class SendMessageOnUiThread&lt;t extends IMessage&gt; implements Runnable {    T message;    public PostToUiThread(T message) {      this.message = message;    }    @Override    public void run() {      sendMessage(message);    }  };}//snip]]&gt;&lt;/script&gt;This approach works best if the &lt;span class="inlineCode"&gt;IBus&lt;/span&gt; implementation lives outside the scope of an &lt;span class="inlineCode"&gt;Activity&lt;/span&gt;.  I've had good luck with instantiating the above Bus and injecting it where needed via &lt;a href="http://code.google.com/p/roboguice/" target="_blank"&gt;Roboguice&lt;/a&gt;. There's probably an easier way to send simple messages around, but this is what I'm using until I find that :)Here's the &lt;a href="https://github.com/maxnachlinger/EventBusTest" target="_blank"&gt;source-code for a simple test app&lt;/a&gt; outlining the above approach.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-6213963523800190403?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/6213963523800190403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=6213963523800190403' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6213963523800190403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6213963523800190403'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2011/12/simple-messaging.html' title='Some really simple messaging'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-7080214062003716288</id><published>2011-11-23T13:52:00.001-08:00</published><updated>2011-11-27T11:51:30.375-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Generics'/><category scheme='http://www.blogger.com/atom/ns#' term='Android'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>A bit of (crazy) code to manage Android fragments</title><content type='html'>Here&amp;#39;s a bit of code I use to manage &lt;a href="http://developer.android.com/sdk/compatibility-library.html" target="_blank"&gt;Compatibility Library Fragments&lt;/a&gt; in Android. Hopefully this will help someone else out.&lt;br /&gt;&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// snipprivate static enum FragmentVisbility {SHOW, HIDE};public class NullFragment extends Fragment {} // simple NullObject@SuppressWarnings("unchecked")protected &lt;T extends Fragment&gt; T fragmentById(int id) { Fragment fragment = getSupportFragmentManager().findFragmentById(id); if(fragment == null) {  Log.w(TAG, "fragmentById(), fragment: "+getString(id)+" not found!");  fragment = new NullFragment();  } return (T) fragment;}protected void showFragmentIds(int... fragmentIds) { setFragmentsVisibility(FragmentVisbility.SHOW, fragmentIds);}protected void hideFragmentIds(int... fragmentIds) { setFragmentsVisibility(FragmentVisbility.HIDE, fragmentIds);}private void setFragmentsVisibility(FragmentVisbility visibility, int... fragmentIds) { FragmentTransaction fragmentTransaction = getSupportFragmentManager()   .beginTransaction(); Fragment fragment; for (int fragmentId : fragmentIds) {  fragment = fragmentById(fragmentId);  if(fragment instanceof NullFragment) {   continue;  }  if(visibility == FragmentVisbility.SHOW) {   fragmentTransaction.show(fragment);  } else {   fragmentTransaction.hide(fragment);  } } fragmentTransaction.commit();}//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;fragmentById()&lt;/span&gt;&lt;br /&gt;&lt;span class="inlineCode"&gt;fragmentById()&lt;/span&gt; is doing a few odd things. &lt;span class="inlineCode"&gt;&amp;lt;T extends Fragment&amp;gt; T&lt;/span&gt; is saying that we don't care about type-safety beyond the assurance that type &lt;span class="inlineCode"&gt;T&lt;/span&gt; extends the &lt;span class="inlineCode"&gt;Fragment&lt;/span&gt; class. It's worth noting that if we dropped &lt;span class="inlineCode"&gt;&amp;lt;T extends Fragment&amp;gt;&lt;/span&gt; in favor of &lt;span class="inlineCode"&gt;&amp;lt;T&amp;gt; T&lt;/span&gt;, then we'd lose &lt;em&gt;all&lt;/em&gt; type safety. Here's how &lt;span class="inlineCode"&gt;fragmentById()&lt;/span&gt; is used:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[MyFragment myFragment = fragmentById(R.id.myFragmentId); // worksFragment justAFragment = fragmentById(R.id.myFragmentId); // works String thisWillBreak = fragmentById(R.id.myFragmentId); // compile error// runtime ClassCastExceptionAnotherFragment breaksAtRuntime = fragmentById(R.id.myFragmentId);]]&gt;&lt;/script&gt;The main benefits of this method are that it gets rid of a cast and it isolates the compatibility library bits off to the side (e.g. the calls to &lt;span class="inlineCode"&gt;getSupportFragmentManager()&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;showFragmentIds() / hideFragmentIds() / setFragmentsVisibility()&lt;/span&gt;&lt;br /&gt;The &lt;span class="inlineCode"&gt;Enum&lt;/span&gt; argument is an attempt at readibility, a boolean wouldn't be readable and would have the downside of limiting my ability to, say, put a 3rd visibility mode in to rip out a fragment completely. Here's what that method looks like with a &lt;span class="inlineCode"&gt;boolean&lt;/span&gt; parameter:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// Wait, it's "true" that I want to set the visibility of these fragments. // What does that even mean?setFragmentsVisibility(true, fragmentIds);// Ah, I understand the code below more easily.setFragmentsVisibility(FragmentVisbility.SHOW, fragmentIds);]]&gt;&lt;/script&gt;&lt;span class="inlineCode"&gt;&amp;lt;/rant&amp;gt;&lt;/span&gt;. The only other thing to note about &lt;span class="inlineCode"&gt;setFragmentsVisibility()&lt;/span&gt; is the varargs parameter &lt;span class="inlineCode"&gt;int... fragmentIds&lt;/span&gt;. This approach makes the calling code nice and simple, e.g.:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[hideFragmentIds(R.id.myFragmentId1, R.id.myFragmentId2);]]&gt;&lt;/script&gt;As always, I welcome suggestions for accomplishing this kind of thing in a more elegant way :)&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;One thing to note:&lt;/span&gt;&lt;br /&gt;The above solution works fine for trivial &lt;span class="inlineCode"&gt;Fragment&lt;/span&gt; cases, but for more interesting things (such as specifying enter and exit animations), your best bet is to stick with the built-in fragment support.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-7080214062003716288?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/7080214062003716288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=7080214062003716288' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7080214062003716288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7080214062003716288'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2011/11/bit-of-crazy-code-to-manage-android.html' title='A bit of (crazy) code to manage Android fragments'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-6180539522339768762</id><published>2011-11-21T11:37:00.001-08:00</published><updated>2011-11-24T09:16:19.217-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RoboGuice'/><category scheme='http://www.blogger.com/atom/ns#' term='ActionBarSherlock'/><title type='text'>ActionBarSherlock and RoboGuice can be friends</title><content type='html'>&lt;span class="Apple-style-span" style="font-size: large;"&gt;The ActionBar&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In Android applications, &lt;a href="http://www.androidpatterns.com/uap_pattern/action-bar" target="_blank"&gt;the ActionBar is quickly emerging as a UI standard&lt;/a&gt;. Although its great, the &lt;span class="inlineCode"&gt;ActionBar&lt;/span&gt; is only supported on Android 3.0 (API level 11, aka Honeycomb) and later. Since (either from prudence or incompetance) carriers seem to push new versions of Android out rather slowly, &lt;a href="http://developer.android.com/resources/dashboard/platform-versions.html" target="_blank"&gt;the majority of phones running Android are on 2x&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;ActionBarSherlock and RoboGuice&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There's a problem then, the &lt;span class="inlineCode"&gt;ActionBar&lt;/span&gt; is nice, but only a small percentage of phones support it. Luckily, lots of smart folks have created &lt;span class="inlineCode"&gt;ActionBar&lt;/span&gt; libraries for pre-3.0 Android, my favorite is &lt;a href="http://actionbarsherlock.com/" target="_blank"&gt;ActionBarSherlock&lt;/a&gt;. ActionBarSherlock's API is identical to the native 3.0 &lt;span class="inlineCode"&gt;ActionBar&lt;/span&gt;, and if the native &lt;span class="inlineCode"&gt;ActionBar&lt;/span&gt; is available, ActionBarSherlock politely steps aside and uses it.&lt;br /&gt;&lt;br /&gt;Another necessity, at least for me, when developing non-trivial applications is a dependency injection framework. DI frameworks are a big help, allowing me to attack large problems with a set of simple, testable, classes which collaborate but are not coupled. For Android, I'm currently using &lt;a href="http://code.google.com/p/roboguice/" target="_blank"&gt;RoboGuice&lt;/a&gt; a popular extension of Google's Guice dependency injection framework. RoboGuice also ships with a nice set of classes easing some of the more annoying parts of developing Android apps. The problem is:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;These libraries don't work in the same app&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To make ActionBarSherlock work as well as it does, it includes &lt;a href="http://developer.android.com/sdk/compatibility-library.html" target="_blank"&gt;the compatibility library&lt;/a&gt; and patches a few of the classes in it. A few of RoboGuice's classes such as &lt;span class="inlineCode"&gt;RoboListActivity&lt;/span&gt; and &lt;span class="inlineCode"&gt;RoboFragment&lt;/span&gt; extend classes from the compatibility library &lt;em&gt;but not the ActionBarSherlock-patched compatibility library&lt;/em&gt;. The good news is that these libraries are solving different problems, and we can attack the DI side of the problem using RoboGuice and composition.&lt;br /&gt;&lt;br /&gt;Here's a simple solution: extend the ActionBarSherlock-patched compatibility classes and get injection via composition using &lt;span class="inlineCode"&gt;RoboGuice.getInjector()&lt;/span&gt;.&lt;br /&gt;&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// snipimport roboguice.RoboGuice;import android.os.Bundle;// this will use the ListFragment from ActionBarSherlockimport android.support.v4.app.ListFragment;import android.view.View;public class IocListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  // RoboGuice injection via Composition  RoboGuice.getInjector(getActivity()).injectMembersWithoutViews(this); } @Override public void onViewCreated(View view, Bundle savedInstanceState) {  super.onViewCreated(view, savedInstanceState);  RoboGuice.getInjector(getActivity()).injectViewMembers(this); }}]]&gt;&lt;/script&gt;You can now extend from &lt;span class="inlineCode"&gt;IocListFragment&lt;/span&gt; (or any other class you need to create in this way such as IocFragment) and get &lt;span class="inlineCode"&gt;@Inject&lt;/span&gt; along with a nice ActionBar:&lt;script class="brush: java" type="syntaxhighlighter"&gt;&lt;![CDATA[// snippublic class GameHistoryFragment extends IocListFragment { @InjectView(R.id.noGameHistoryTextView) TextView noGameHistoryTextView;  @InjectView(R.id.currentGameButton) private Button currentGameButton;  @Inject GameHistoryModel gameHistoryModel;// snip]]&gt;&lt;/script&gt;Fortunately you'll only need to extend a few classes in this way to make both of these libraries work in the same app. This solution feels a bit clunky, so if you know a better solution to this problem, please share it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-6180539522339768762?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/6180539522339768762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=6180539522339768762' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6180539522339768762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6180539522339768762'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2011/11/actionbarsherlock-and-roboguice-can-be.html' title='ActionBarSherlock and RoboGuice can be friends'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-8852900170255162727</id><published>2010-10-08T22:29:00.000-07:00</published><updated>2011-12-08T23:35:04.689-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Unit Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><category scheme='http://www.blogger.com/atom/ns#' term='Moq'/><title type='text'>Dependency Injection Part 5: Mocking and Cool Tests</title><content type='html'>&lt;a href="http://flexamusements.blogspot.com/2010/10/dependency-injection-part-4-tests.html" target="_blank"&gt;In my last post&lt;/a&gt;, we wrote a set of unit tests exercising &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;, &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt;.  We tested these classes thoroughly, and in isolation since their behavior was easier to test in a controlled environment.  Its now time to test our main stimulation class &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Remember our humble &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class?  If not, here's a quick refresh:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip  // our "dependencies" are now interfaces  private readonly IMessageService _messageService;  private readonly IInputService _inputService;  private readonly IOutputService _outputService;  // we're now injecting Interfaces, this loosens our coupling to our "injected" dependencies  public Magic8BallSimulator(IMessageService messageService, IInputService inputService,   IOutputService outputService) {   _messageService = messageService;   _inputService = inputService;   _outputService = outputService;  }  public void Run() {   _outputService.PrintWelcome();   string message = string.Empty;   _outputService.PrintInputPrompt();   _inputService.GetInput();   while (!_inputService.ExitWasRequested()) {    message = _messageService.GetMessage();    _outputService.PrintMessage(message);    _outputService.PrintInputPrompt();    _inputService.GetInput();   }   _outputService.PrintExit();  }// snip]]&gt;&lt;/script&gt;This class depends on &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; and those dependencies are injected via &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;'s constructor.  Since unit testing requires a controlled environment, we'll need to make sure that the instances of &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; that we instantiate and inject behave in predictable ways.  Remember, unit tests aim to test a simple unit of code, and for our purposes a "unit" is a method within a class.  &lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;So we have a problem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We need to make sure that the instances of &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; that we instantiate and inject into &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; behave in predictable ways, but we want those instances to behave in &lt;i&gt;different predictable ways for different tests&lt;/i&gt;. Consider this test:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: green;"&gt;[Test]: We want to make sure &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; prints an exit message before it exits.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To get our injected instances to behave in the way we need them to for this test, we'll have to do the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Create a &lt;span class="inlineCode"&gt;InputServiceStub&lt;/span&gt; class implementing &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;. All the method-bodies in in this class will be empty except for &lt;span class="inlineCode"&gt;ExitWasRequested()&lt;/span&gt; which will always return &lt;span class="inlineCode"&gt;true&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Create a &lt;span class="inlineCode"&gt;OutputServiceStub&lt;/span&gt; class implementing &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;. All the method-bodies in this class will also be empty empty expect for &lt;span class="inlineCode"&gt;PrintExit()&lt;/span&gt; which will set a public &lt;span class="inlineCode"&gt;PrintExitCalled&lt;/span&gt; property to &lt;span class="inlineCode"&gt;true&lt;/span&gt; when called.&lt;/li&gt;&lt;li&gt;Create a &lt;span class="inlineCode"&gt;MessageServiceStub&lt;/span&gt; class implementing &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt; with empty methods.&lt;/li&gt;&lt;li&gt;Instantiate the 3 classes above. Instantiate &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; and inject our 3 test instances&lt;/li&gt;&lt;li&gt;call &lt;span class="inlineCode"&gt;Magic8BallSimulator.Run()&lt;/span&gt;, and then make sure that &lt;span class="inlineCode"&gt;OutputServiceStub.PrintExitCalled&lt;/span&gt; is &lt;span class="inlineCode"&gt;true&lt;/span&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;No Thanks!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That's too much code for us to write and maintain to test 1 case.  We still have to test &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;, however, after-all &lt;i&gt;they pay us to write software&lt;/i&gt; :)  The problem is, we don't want to have to write &lt;b&gt;three classes to test 1 case&lt;/b&gt;.  One way out might be to create 3 abstract classes implementing &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; and then extend those class only filling in the method-bodies we need for our test. That still turns out to be a ton of code to maintain, and we'd like our application to be fun to work on, not a pain in the ass.  Here's a better way.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;I love Moq.  Check this out&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We can leverage the badass, open-source, &lt;a href="http://code.google.com/p/moq/" target="_blank"&gt;Moq library&lt;/a&gt; to create mock implementations of &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; and then use lambda expressions to stand in for the methods we want.  We can then inject those mock classes into &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;.  Feel the &lt;i&gt;lightness&lt;/i&gt; of the test below :)&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip  [Fact]  public void PrintExitIsCalledOnExitRequested() {   // mocked IMessageService   var messageService = new Mock&lt;imessageservice&gt;();   var inputService = new Mock&lt;iinputservice&gt;();   // this is cool, we're getting a free mock of IInputService and we can specify   // how the ExitWasRequested() method will behave by a lambda expression (as you   // can see, it always returns true   inputService.Setup(s =&gt; s.ExitWasRequested()).Returns(true);   // outservice mock   var outputService = new Mock&lt;ioutputservice&gt;();   // setup and run our simulator   Magic8BallSimulator magic8BallSimulator = new Magic8BallSimulator(    messageService.Object,    inputService.Object,    outputService.Object   );   magic8BallSimulator.Run();   // a simple assertion *verifying* that the PrintExit() method was called   outputService.Verify(s =&gt; s.PrintExit());  }// snip]]&gt;&lt;/script&gt;&lt;b&gt;Hey, make sure you read the comments above&lt;/b&gt;, that's where the good explanations are!  Now that we're able to use Moq to mock our dependencies, additional tests are easy!  Here are 3 more:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip  [Fact]  public void GetMessageIsNotCalledAfterExitRequested() {   var messageService = new Mock&lt;imessageservice&gt;();      var inputService = new Mock&lt;iinputservice&gt;();   inputService.Setup(s =&gt; s.ExitWasRequested()).Returns(true);      var outputService = new Mock&lt;ioutputservice&gt;();   Magic8BallSimulator magic8BallSimulator = new Magic8BallSimulator(    messageService.Object,    inputService.Object,    outputService.Object   );   // verify that GetMessage() was *not* called   messageService.Verify(s =&gt; s.GetMessage(), Times.Never());  }  [Fact]  public void PrintWelcomeIsCalledOnRun() {   var messageService = new Mock&lt;imessageservice&gt;();   var inputService = new Mock&lt;iinputservice&gt;();   inputService.Setup(s =&gt; s.ExitWasRequested()).Returns(true);   var outputService = new Mock&lt;ioutputservice&gt;();   Magic8BallSimulator magic8BallSimulator = new Magic8BallSimulator(    messageService.Object,    inputService.Object,    outputService.Object   );   magic8BallSimulator.Run();   outputService.Verify(s =&gt; s.PrintWelcome());  }  [Fact]  public void PrintInputPromptIsCalledOnRun() {   var messageService = new Mock&lt;imessageservice&gt;();   var inputService = new Mock&lt;iinputservice&gt;();   inputService.Setup(s =&gt; s.ExitWasRequested()).Returns(true);   var outputService = new Mock&lt;ioutputservice&gt;();   Magic8BallSimulator magic8BallSimulator = new Magic8BallSimulator(    messageService.Object,    inputService.Object,    outputService.Object   );   magic8BallSimulator.Run();   outputService.Verify(s =&gt; s.PrintInputPrompt());  }// snip]]&gt;&lt;/script&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;That's it.  It's been fun!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here's the &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass5.zip"&gt;source code for this post&lt;/a&gt;. Thanks so much for reading these posts on Dependency Injection and Unit Testing.  I had lots of fun writing these and only hope my silly examples and code-snippets were clear. If you have any questions, go ahead and leave a comment and I'll try to help you out.&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-8852900170255162727?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/8852900170255162727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=8852900170255162727' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/8852900170255162727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/8852900170255162727'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/10/dependency-injection-part-5-mocking-and.html' title='Dependency Injection Part 5: Mocking and Cool Tests'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-3443653605888254978</id><published>2010-10-07T22:22:00.000-07:00</published><updated>2010-11-11T23:16:03.823-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Weborb FlourineFx AMF remoting dependency injection'/><title type='text'>AMF Remoting, Dependency Injection, and exposing Interfaces to Flex - FlourineFx version</title><content type='html'>In the &lt;a href="http://flexamusements.blogspot.com/2010/10/amf-remoting-dependency-injection-and.html" target="_blank"&gt;previous post&lt;/a&gt;, I showed one way to wire Autofac and WebORB together to achieve AMF-Remoting as well as Dependency Injection.  This post builds on the last one by providing an inplementation using &lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;Autofac&lt;/a&gt; and &lt;a href="http://www.fluorinefx.com/" target="_blank"&gt;FlourineFx&lt;/a&gt;, the latter is a great open-source .NET AMF Remoting solution.&lt;br /&gt;&lt;br /&gt;Here's the &lt;a href="http://maxnachlinger.com/miscExamples/flourineFxAutofac/FlourineFxAutofacTest.zip" target="_blank"&gt;the source code for this post&lt;/a&gt; so you won't have to figure out the context of the snippets below.&lt;br /&gt;&lt;br /&gt;Add the following to your &lt;span class="inlineCode"&gt;WEB-INF\flex\services-config.xml&lt;/span&gt;:&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt; &lt;factories&gt;  &lt;factory id="autofac" class="FlourineFxAutofacTest2.Gateway.AutofacFactory"/&gt; &lt;/factories&gt; &lt;services&gt;  &lt;service id="remoting-service"     class="flex.messaging.services.RemotingService"     messageTypes="flex.messaging.messages.RemotingMessage"&gt;   &lt;destination id="fluorine"&gt;    &lt;channels&gt;     &lt;channel ref="my-amf"/&gt;    &lt;/channels&gt;    &lt;properties&gt;     &lt;factory&gt;autofac&lt;/factory&gt;     &lt;source&gt;*&lt;/source&gt;    &lt;/properties&gt;   &lt;/destination&gt;  &lt;/service&gt; &lt;/services&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;To tie FlourineFx to Autofac, we'll create and register our own custom Factory (we have to implement the FlourineFx's &lt;span class="inlineCode"&gt;IFlexFactory&lt;/span&gt; interface) and instantiate our objects by resolving them from the DI Container.&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class AutofacFactory : IFlexFactory {  public FactoryInstance CreateFactoryInstance(string id, Hashtable properties) {   AutofacFactoryInstance instance = new AutofacFactoryInstance(this, id, properties);   instance.Source = properties["source"] as string;   return instance;  }  public object Lookup(FactoryInstance factoryInstance) {   AutofacFactoryInstance autofacFactoryInstance = factoryInstance as AutofacFactoryInstance;   return autofacFactoryInstance.Lookup();  } } sealed class AutofacFactoryInstance : FactoryInstance {  public AutofacFactoryInstance(AutofacFactory factory, string id, Hashtable properties)   : base(factory, id, properties) { }  public override object Lookup() {   object newInstance = new object();   // deferring to the DI Container for our class instance   var containerProviderAccessor =    (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;   var scope = containerProviderAccessor.ContainerProvider.RequestLifetime;   scope.TryResolveNamed(this.Source, typeof(object), out newInstance);   if (newInstance == null) {    // fail-over to FlourineFx for Type mapping    FactoryInstance factory = new DotNetFactory().CreateFactoryInstance(this.Id, this.Properties);    newInstance = factory.Lookup();   }   return newInstance;  } }//snip]]&gt;&lt;/script&gt;As in the previous example using WebORB, you now modify your Flex client to use our new destination and exposed interface like this:&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt; &lt;mx:RemoteObject   id="ro"   destination="fluorine"   source="FlourineFxAutofacTest2.ITestService"   fault="ro_faultHandler(event)"&gt;  &lt;mx:method name="GetMessage" result="ro_getMessageResultHandler(event)"/&gt; &lt;/mx:RemoteObject&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;If there's an easier way to integrate FlourineFx and Autofac, I'd love to hear it, though the above is quite easy :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-3443653605888254978?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/3443653605888254978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=3443653605888254978' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/3443653605888254978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/3443653605888254978'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/10/amf-remoting-dependency-injection-and_07.html' title='AMF Remoting, Dependency Injection, and exposing Interfaces to Flex - FlourineFx version'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-7771309788876562088</id><published>2010-10-07T00:51:00.000-07:00</published><updated>2010-11-11T23:01:18.881-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Weborb Autofac AMF remoting dependency injection'/><title type='text'>AMF Remoting, Dependency Injection, and exposing Interfaces to Flex</title><content type='html'>Imagine that we've built a large middle-tier in C# for a Flex client and have decided to remote objects to and from Flex via AMF. AMF is a great choice for us since it's a lightweight, binary, protocol which is easily de-serialized by the Flash Player (we picked the Flash Player as our client because of its current popularity).&lt;br /&gt;&lt;br /&gt;Since our middle-tier code-base is large and complex, we've attacked that complexity by making lots of simple, loosely coupled, Single Responsibility classes. We've read about Dependency Injection and expect that if we use that general approach, we'll end up with code which is both flexible and easy to test.  We'd like to avoid writing the Dependency Injection plumbing ourselves however, so we've decided to leverage &lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;Autofac&lt;/a&gt; to help. We've also decided to use &lt;a href="http://www.themidnightcoders.com/products/weborb-for-net/overview.html" target="_blank"&gt;WebORB&lt;/a&gt; to handle the translation of our .NET objects to Flex DTOs/VOs.  Using Autofac and WebORB will allow us to focus on our business logic without concerning ourselves with translating to and from AMF and writing the necessary plumbing for Dependency Injection respectively. &lt;br /&gt;&lt;br /&gt;We've decided to create a set of service classes which Flex can access through WebORB.  Since these classes are handling client requests directly, they often depend on other injected classes to handle some of the work-load. These dependencies also keep the service classes clean, small and easy to understand.  We'd also like to provide these dependent classes to our service classes through Autofac.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;There are problems&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We've chosen two great tools to help, but we've a few problems to work out first:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Out of the box, to use our service classes in WebORB we'll need to create default constructors in them.  This is unpleasant since we'd like to inject our dependent classes as  constructor arguments.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;To expose our Service classes to Flex, we have 2 options:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Expose our service classes directly to Flex, so our client can remote to, say, &lt;span class="inlineCode"&gt;WebORBAutofacTest.TestService&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Create an alias which maps to each of our service classes so our client can remote to, say, AppTestService&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;The problem with (1) is that our Flex clients shouldn't know about the specific classes they're using on the middle-tier. This leaves us free to modify, rename, of delete those classes without the fear of breaking our client. Although this makes (2) a better option, (2) is still a bit more work because we'll have to maintain our aliases which reside in a non compiler-checked config file somewhere.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;We have no idea how to get new instances of our service classes created and their dependencies injected.  In other words, we cannot get WebORB working with Autofac.&lt;/li&gt;&lt;/ol&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Our goal&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;We want to inject dependencies into our service classes as constructor arguments (and we'd like for Autofac to instantiate these dependent classes for us).&lt;/li&gt;&lt;li&gt;We'd like to expose only the interfaces implemented by our service classes to our Flex client (that's a more familiar approach on the mid-tier)&lt;/li&gt;&lt;li&gt;and finally, we don't want to constantly update configuration files for AMF remoting&lt;/li&gt;&lt;/ol&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;A solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First go ahead and get &lt;a href="http://maxnachlinger.com/miscExamples/weborbAutofac/WebORBAutofacTest.zip" target="_blank"&gt;the source code for this post&lt;/a&gt; so you won't have to cut and paste annoying code snippets.&lt;br /&gt;&lt;br /&gt;Next we'll follow the &lt;a href="http://code.google.com/p/autofac/wiki/AspNetIntegration" target="_blank"&gt;ASP.NET Integration instructions&lt;/a&gt; in the all-around great Autofac wiki.  After you've read the wiki entry, have a look at this simple &lt;span class="inlineCode"&gt;ContainerSetup&lt;/span&gt; class below.  This class is called by our &lt;span class="inlineCode"&gt;Global.asax&lt;/span&gt; class to register our dependencies and help &lt;span class="inlineCode"&gt;Global.asax&lt;/span&gt; implement &lt;span class="inlineCode"&gt;IContainerProviderAccessor&lt;/span&gt;:&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class ContainerSetup {  public IContainer BuildContainer() {   var builder = new ContainerBuilder();   builder.RegisterType&lt;testservice&gt;().As&lt;itestservice&gt;();   builder.RegisterType&lt;testconfig&gt;().As&lt;itestconfig&gt;();   return builder.Build();  } }// snip]]&gt;&lt;/script&gt;Here's the relevant section of my &lt;span class="inlineCode"&gt;Global&lt;/span&gt; application class:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class Global : HttpApplication, IContainerProviderAccessor {  static ContainerProvider containerProvider;  public IContainerProvider ContainerProvider {   get { return containerProvider; }  }  protected void Application_Start(object sender, EventArgs e) {   SetupContainerProvider();  }  private static void SetupContainerProvider() {   ContainerSetup setup = new ContainerSetup();   containerProvider = new ContainerProvider(    setup.BuildContainer()   );  }// snip]]&gt;&lt;/script&gt;The snippet above informs the classes in our application that the main application class &lt;span class="inlineCode"&gt;Global&lt;/span&gt; is an &lt;span class="inlineCode"&gt;IContainerProviderAccessor&lt;/span&gt; providing access to a &lt;span class="inlineCode"&gt;ContainerProvider&lt;/span&gt; allowing callers to access the DI Container and resolve dependencies.  We'll make good use of this property.&lt;br /&gt;&lt;br /&gt;To tie WebORB to Autofac, we'll create and register our own custom Activator (we have to implement the &lt;span class="inlineCode"&gt;IActivator&lt;/span&gt; interface, see the &lt;a href="http://www.themidnightcoders.com/weborb-net-doc" target="_blank"&gt;WebORB docs&lt;/a&gt; if you want to know more about Custom Activators) and instantiate our objects by resolving them from the DI Container.&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class DependencyActivator : IActivator {  private bool resolvedViaContainer = false;  private Type currentType;  private object newInstance = new object();  public object Activate(Type type) {   currentType = type;   ResolveFromContainer();   resolvedViaContainer = true;   // fail-over to WebORB for Type mapping, (the call below will fail if    // our class lacks a default constructor)   if (newInstance == null) {    ResolveFromWebORB();    resolvedViaContainer = false;   }   return newInstance;  }  private void ResolveFromContainer() {   var containerProviderAccessor =    (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;   var scope = containerProviderAccessor.ContainerProvider.RequestLifetime;   scope.TryResolve(currentType, out newInstance);  }  private void ResolveFromWebORB() {   newInstance = ObjectFactories.CreateServiceObject(currentType);  }  public bool DisposeServiceAfterInvocation() {   // we will let the DI Container destroy our class   if (resolvedViaContainer)    return false;   // WebORB can destroy our class   return true;  } }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Configuration files&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We now have to register our new &lt;span class="inlineCode"&gt;DependencyActivator&lt;/span&gt; class in &lt;span class="inlineCode"&gt;weborb.config&lt;/span&gt; luckily we'll only have to modify these configuration files &lt;i&gt;once&lt;/i&gt;:&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt;&lt;activators&gt;&lt;!--default Activators here--&gt;   &lt;activator&gt;    &lt;activationmodename&gt;dependency&lt;/activationModeName&gt;    &lt;classname&gt;WebORBAutofacTest.WebUI.ObjectActivators.DependencyActivator&lt;/className&gt;   &lt;/activator&gt;&lt;/activators&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;Now we have to update 2 XML files which our Flex client will compile against.  These files define how and where Flex will send data when remoting.  First we'll create a new channel-definition in &lt;span class="inlineCode"&gt;WEB-INF\flex\services-config.xml&lt;/span&gt;:&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt;&lt;channels&gt;&lt;!--default channels here--&gt;  &lt;channel-definition id="amf-dependency" class="mx.messaging.channels.AMFChannel"&gt;   &lt;endpoint uri="weborb.aspx?activate=dependency" class="flex.messaging.endpoints.AMFEndpoint"/&gt;   &lt;properties&gt;    &lt;polling-enabled&gt;false&lt;/polling-enabled&gt;   &lt;/properties&gt;  &lt;/channel-definition&gt;&lt;/channels&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;Then we'll add a destination in &lt;span class="inlineCode"&gt;WEB-INF\flex\remoting-config.xml&lt;/span&gt;:&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt; &lt;destination channels="amf-dependency" id="DependencyGenericDestination"&gt;  &lt;channels&gt;   &lt;channel ref="amf-dependency" /&gt;  &lt;/channels&gt;  &lt;properties&gt;   &lt;source&gt;*&lt;/source&gt;  &lt;/properties&gt; &lt;/destination&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;We now have Dependency Injection in our service classes via Autofac&lt;/span&gt;&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class TestService : ITestService {  private ITestConfig _config;  // config will be instantiated and injected for us  public TestService(ITestConfig config) {   _config = config;  }  public string GetMessage() {   return _config.Message;  } }//snip]]&gt;&lt;/script&gt;Now we can finally modify our Flex client to use the new &lt;span class="inlineCode"&gt;DependencyGenericDestination&lt;/span&gt; in its &lt;span class="inlineCode"&gt;RemoteObject&lt;/span&gt;s.  Notice that we're exposing &lt;span class="inlineCode"&gt;WebORBAutofacTest.ITestService&lt;/span&gt; &lt;b&gt;hey that's an interface!&lt;/b&gt;&lt;script class="brush: xml" type="syntaxhighlighter"&gt;&lt;![CDATA[&lt;!--snip--&gt; &lt;mx:RemoteObject   id="ro"   destination="DependencyGenericDestination"   source="WebORBAutofacTest.ITestService"   fault="ro_faultHandler(event)"&gt;  &lt;mx:method name="GetMessage" result="ro_getMessageResultHandler(event)"/&gt; &lt;/mx:RemoteObject&gt;&lt;!--snip--&gt;]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Solution benefits and drawbacks&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Benefits to this approach:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;You get a &lt;span class="inlineCode"&gt;Type&lt;/span&gt; passed into &lt;span class="inlineCode"&gt;DependencyActivator&lt;/span&gt;, which works nicely with Autofac's &lt;span class="inlineCode"&gt;Resolve&lt;/span&gt; method.&lt;/li&gt;&lt;li&gt;You do not have to make any modifications to your service classes for this to work (in fact your service classes do not need to depend on WebORB or Autofac at all).&lt;/li&gt;&lt;li&gt;You can expose your Service classes as Interfaces.&lt;/li&gt;&lt;/ol&gt;Drawbacks to this approach:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Instantiation of classes that don't use Autofac will be slowed down by &lt;span class="inlineCode"&gt;DependencyActivator&lt;/span&gt; going to Autofac first. To avoid this slow-down you'll have to pick a different &lt;span class="inlineCode"&gt;destination&lt;/span&gt; in the relevant &lt;span class="inlineCode"&gt;RemoteObject&lt;/span&gt;s in your Flex client&lt;/li&gt;&lt;/ol&gt;If any of you folks see a better way of accomplishing the goals I set out to tackle using WebORB and Autofac, by all means post a comment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-7771309788876562088?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/7771309788876562088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=7771309788876562088' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7771309788876562088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7771309788876562088'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/10/amf-remoting-dependency-injection-and.html' title='AMF Remoting, Dependency Injection, and exposing Interfaces to Flex'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-575331231482960923</id><published>2010-10-05T21:53:00.000-07:00</published><updated>2011-12-08T23:41:45.276-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Unit Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><title type='text'>Dependency Injection Part 4: Tests</title><content type='html'>&lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html" target="_blank"&gt;In my last post&lt;/a&gt;, we identified that the &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; class violated the Single Responsibility Principle by concerning itself with:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Setting up our application, (in fact &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; created an instance of every class in the application)&lt;/li&gt;&lt;li&gt;Running our application (via &lt;span class="inlineCode"&gt;simulator.Run()&lt;/span&gt;)&lt;/li&gt;&lt;/ul&gt;(In case you've forgotten, the Single Responsibility Principle is the assertion that &lt;i&gt;there should never be more than one reason for a class to change&lt;/i&gt;).  We addressed the disparate responsibilities in &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; by creating the &lt;span class="inlineCode"&gt;ContainerSetup&lt;/span&gt; class and leveraging &lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;Autofac&lt;/a&gt; (a badass Dependency Injection framework) to create and wire-in instances of our dependencies for us (i.e. setup our application).  &lt;br /&gt;&lt;br /&gt;We've arrived at code which is easy to change.  That's valuable for our &lt;i&gt;requirements&lt;/i&gt; will certainly change. There's only one rather large lingering concern:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;I'm not sure it works&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As our code stands now, I've no justification for believing that our application will work correctly in &lt;i&gt;every circumstance&lt;/i&gt;.  If you're sure, then you're &lt;i&gt;new to this&lt;/i&gt; and new developers seem to know &lt;i&gt;everything&lt;/i&gt; :)  For us to be sure our code is solid, we need evidence, but why gather evidence if you're certain this application won't break? See the problem? We'll gather our evidence via a suite of simple Unit Tests.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Unit Tests&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A &lt;b&gt;Unit Test&lt;/b&gt; is a simple test of a small &lt;i&gt;unit&lt;/i&gt; of code, for our purposes, units will be single methods.  Remember our &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt; class?&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class ConsoleInputService : IInputService {  private string input = "input-initializer";  public string GetInput() {   input = Console.ReadLine();   return input;  }  public bool ExitWasRequested() {   return String.IsNullOrEmpty(input);  } }// snip]]&gt;&lt;/script&gt;Here's a simple set of unit tests for &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;.  We're testing the following cases below:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The &lt;span class="inlineCode"&gt;ConsoleInputService.GetInput()&lt;/span&gt; accurately returns console input&lt;/li&gt;&lt;li&gt;The &lt;span class="inlineCode"&gt;ConsoleInputService.ExitWasRequested()&lt;/span&gt; returns true when it reads a new-line from console input&lt;/li&gt;&lt;li&gt;The &lt;span class="inlineCode"&gt;ConsoleInputService.ExitWasRequested()&lt;/span&gt; returns true when it reads empty input from the console&lt;/li&gt;&lt;li&gt;The &lt;span class="inlineCode"&gt;ConsoleInputService.ExitWasRequested()&lt;/span&gt; returns true when it reads a new-line from console input&lt;/li&gt;&lt;li&gt;The &lt;span class="inlineCode"&gt;ConsoleInputService.ExitWasRequested()&lt;/span&gt; returns true when it reads a string containing only spaces from console input&lt;/li&gt;&lt;/ul&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class ConsoleInputServiceTests {  private ConsoleInputMock consoleMock;  public ConsoleInputServiceTests() {   consoleMock = new ConsoleInputMock();  }  [Fact]  public void ConsoleInputIsReturnedByGetInput() {   string testInput = "This is test input.";   consoleMock.GivenConsoleInputOf(testInput);   ConsoleInputService service = new ConsoleInputService();   string input = service.GetInput();   consoleMock.CloseConsoleInput();   Assert.Equal(testInput, input);  }  [Fact]  public void ExitIsRequestedOnNewlineInput() {   consoleMock.GivenConsoleInputOf(Environment.NewLine);   ConsoleInputService service = new ConsoleInputService();   service.GetInput();   consoleMock.CloseConsoleInput();   bool exitWasRequested = service.ExitWasRequested();   Assert.True(exitWasRequested);  }  [Fact]  public void ExitIsRequestedOnEmptyInput() {   consoleMock.GivenConsoleInputOf(String.Empty);   ConsoleInputService service = new ConsoleInputService();   service.GetInput();   consoleMock.CloseConsoleInput();   bool exitWasRequested = service.ExitWasRequested();   Assert.True(exitWasRequested);  }  [Fact]  public void ExitIsRequestedOnWhitespaceInput() {   consoleMock.GivenConsoleInputOf(" ");   ConsoleInputService service = new ConsoleInputService();   service.GetInput();   consoleMock.CloseConsoleInput();   bool exitWasRequested = service.ExitWasRequested();   Assert.True(exitWasRequested);  } }// snip]]&gt;&lt;/script&gt;The &lt;span class="inlineCode"&gt;ConsoleInputMock&lt;/span&gt; class below is a simple test helper which simulates a given string coming from the console as input.  Often to isolate a single class to test its methods, you have to do sneaky things like this.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class ConsoleInputMock {  private StringReader consoleInputReader;  // sets up the Console with a given input string  public void GivenConsoleInputOf(string consoleInput) {   if (consoleInputReader != null)    CloseConsoleInput();   consoleInputReader = new StringReader(consoleInput);   Console.SetIn(consoleInputReader);  }  public void CloseConsoleInput() {   consoleInputReader.Close();   consoleInputReader.Dispose();   consoleInputReader = null;  } }// snip]]&gt;&lt;/script&gt;As a general guideline, unit tests should be self-contained, clearly named, and as simple as possible.  They should also only test one thing.  This is important for when a test fails, its helpful to know exactly what broke. Have another look at the methods in &lt;span class="inlineCode"&gt;ConsoleInputServiceTests&lt;/span&gt; above, you should be able to see what they're testing based on their names (e.g. &lt;span class="inlineCode"&gt;ExitIsRequestedOnEmptyInput&lt;/span&gt;).  Speaking of testing, here's a screen-shot of the output of the tests in &lt;span class="inlineCode"&gt;ConsoleInputServiceTests&lt;/span&gt;, see the failure?&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/ConsoleInputServiceTestResults.png" /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;span class="inlineCode"&gt;ExitIsRequestedOnWhitespaceInput&lt;/span&gt; failed.  When &lt;span class="inlineCode"&gt;ConsoleInputService.ExitWasRequested()&lt;/span&gt; encountered input that was just spaces, we expected it to return true, signalling that the user wished to exit. It evidently sees that as valid input. Let's fix &lt;span class="inlineCode"&gt;ConsoleInputService.GetInput()&lt;/span&gt; to trim all input and re-run &lt;span class="inlineCode"&gt;ConsoleInputServiceTests&lt;/span&gt;.&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip  public string GetInput() {   input = Console.ReadLine();   if (!String.IsNullOrEmpty(input))    input = input.Trim();      return input;  }// snip]]&gt;&lt;/script&gt;We can now re-run our &lt;span class="inlineCode"&gt;ConsoleInputServiceTests&lt;/span&gt; and we pass.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/ConsoleInputServiceTestResultsPass.png" /&gt;&lt;/div&gt;Here are a few tests of the &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/AFewMoreTests.html" target="_blank"&gt;&lt;span class="inlineCode"&gt;MessageService&lt;/span&gt; and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; classes&lt;/a&gt;.  Here's the &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass4.zip" target="_blank"&gt;source code for this post&lt;/a&gt; as well, to run it, make sure you grab a copy of &lt;a href="http://xunit.codeplex.com/" target="_blank"&gt;xunit&lt;/a&gt;, unpack it somewhere meaningful like &lt;span class="inlineCode"&gt;C:\Program Files\xunit-1.6.1&lt;/span&gt; then goto the &lt;span class="inlineCode"&gt;Properties&lt;/span&gt; of the &lt;span class="inlineCode"&gt;Pass4Tests&lt;/span&gt; project and in the &lt;span class="inlineCode"&gt;Debug&lt;/span&gt; section set &lt;span class="inlineCode"&gt;Start external program:&lt;/span&gt; to point to one of the &lt;span class="inlineCode"&gt;xunit.console&lt;/span&gt; executables. Your setup should look similar to this:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/HowToRunTests.png" /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Up Next&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://flexamusements.blogspot.com/2010/10/dependency-injection-part-5-mocking-and.html"&gt;In the next post&lt;/a&gt; we'll tackle unit testing the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class which is more interesting since it contains 3 dependencies.&lt;br /&gt;&lt;br /&gt;Happy testing.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-575331231482960923?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/575331231482960923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=575331231482960923' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/575331231482960923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/575331231482960923'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/10/dependency-injection-part-4-tests.html' title='Dependency Injection Part 4: Tests'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-267115615415076024</id><published>2010-09-29T01:20:00.010-07:00</published><updated>2011-12-08T23:40:49.826-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><title type='text'>Dependency Injection Part 3: Making our lives easier</title><content type='html'>&lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-2-loose.html" target="_blank"&gt;In my last post&lt;/a&gt;, we implemented a set of interfaces in our service-classes and injected those interfaces into the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class. This de-coupling of &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; and its dependencies, allowed us to radically change how we output text in our application without breaking it. Here's the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; constructor we ended up with last time, it's nice and de-coupled:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[  // snip  // we're now injecting Interfaces, this loosens our coupling to our "injected" dependencies  public Magic8BallSimulator(IMessageService messageService, IInputService inputService,   IOutputService outputService) {   _messageService = messageService;   _inputService = inputService;   _outputService = outputService;  }  // snip]]&gt;&lt;/script&gt;Here's our previous implementation of the the &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; program (I dropped our crazy &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; class in favor of &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; for the moment):&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip public class SimulationRunner {  static void Main(string[] args) {   Magic8BallSimulator simulator = new Magic8BallSimulator(    new MessageService(),    new ConsoleInputService(),    new ConsoleOutputService()   );   simulator.Run();  } }// snip]]&gt;&lt;/script&gt;&lt;p&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Wait a moment&lt;/span&gt;&lt;/p&gt;Have a look at the &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; class above one more time. &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; is closely coupled to several specific classes, namely &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;, &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt;. There's also a more subtle problem with &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt;, &lt;i&gt;it has more than 1 responsibility&lt;/i&gt;. &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; is currently:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Setting up our application, in fact it creates a &lt;span class="inlineCode"&gt;new&lt;/span&gt; instance of every class in the application&lt;/li&gt;&lt;li&gt;Running our application (via &lt;span class="inlineCode"&gt;simulator.Run()&lt;/span&gt;)&lt;/li&gt;&lt;/ul&gt;Certainly &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; ought to have the responsibility of &lt;i&gt;running&lt;/i&gt; our application, but how can solve our two remaining problems of:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Removing our application's setup responsibility from &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt;, and &lt;/li&gt;&lt;li&gt;de-coupling &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; from specific instances of our service classes?&lt;/li&gt;&lt;/ol&gt;Our attack will involve using a Dependency Injection framework and a new class whose sole responsibility is setting up our application.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Autofac&lt;/span&gt; (&lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;http://code.google.com/p/autofac/&lt;/a&gt;) - a badass Dependency Injection framework for .NET.&lt;br /&gt;&lt;br /&gt;Here's an absurdly brief overview of what Autofac can do for us (if you want more information, check-out the above link to Autofac's google-code site or &lt;a href="http://www.codeproject.com/KB/architecture/di-with-autofac.aspx" target="_blank"&gt;this article on CodeProject&lt;/a&gt;).  Autofac allows us to register classes like &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt; as implementations of specific interfaces (like &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;). Here's the best part, if we register all our dependencies for &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; as well as &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; itself with Autofac, Autofac will also examine &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;'s constructor parameters and &lt;i&gt;inject the registered dependent classes for us&lt;/i&gt;. That kicks ass!&lt;br /&gt;&lt;br /&gt;Here's our new &lt;span class="inlineCode"&gt;ContainerSetup&lt;/span&gt; class, it registers our application classes with Autofac and does our application setup work.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class ContainerSetup {  private ContainerBuilder builder;  public IContainer BuildContainer() {   builder = new ContainerBuilder();   // in English this reads, setup ConsoleInputService as the implementation of    // IInputService   builder.RegisterType&lt;consoleinputservice&gt;().As&lt;iinputservice&gt;();   // Magic8BallSimulator doesn't implement an interface, its registered as-is   builder.RegisterType&lt;magic8ballsimulator&gt;();   builder.RegisterType&lt;messageservice&gt;().As&lt;imessageservice&gt;();   builder.RegisterType&lt;consoleoutputservice&gt;().As&lt;ioutputservice&gt;();   return builder.Build();  }}//snip]]&gt;&lt;/script&gt;Inside &lt;span class="inlineCode"&gt;BuildContainer()&lt;/span&gt; we're registering the classes that Autofac will provide when their relevant interfaces are requested. For example, on line 10, we're registering &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt; as the class to use when one of our classes has a dependency on the &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt; interface (hey, &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; depends on that interface!). Notice that we didn't map &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; to anything, it doesn't implement an interface.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Problem Solved!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here's our new &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; class.  It's de-coupled from the specific classes in our application and is free of all setup responsibilities.  It's asking Autofac for the class to run (that's the call to &lt;span class="inlineCode"&gt;Resolve()&lt;/span&gt; on line 6):&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class SimulationRunner {  static void Main(string[] args) {   ContainerSetup containerSetup = new ContainerSetup();   IContainer container = containerSetup.BuildContainer();   container.Resolve&lt;magic8ballsimulator&gt;().Run();  } }//snip]]&gt;&lt;/script&gt;As I mentioned before, since we've registered our &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class with Autofac as well as its dependencies (&lt;span class="inlineCode"&gt;MessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt;), Autofac will create and inject instances of those dependent classes into &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; for us.  Scroll up and look at the old &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; constructor, it hasn't changed at all :)&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;For the curious&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To keep this post relatively simple, I dropped our crazy &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; class in favor of &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt;. Remember &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt;? We used that class to show how de-coupling with interfaces allowed us to radically change a whole section of our application (and even its behavior) without breaking anything.  It's also worth noting that &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt;'s constructor takes a file-path string as it's argument.  &lt;br /&gt;&lt;br /&gt;If we wanted to use &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; as our implementation of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; we'd make the following change to our &lt;span class="inlineCode"&gt;ContainerSetup&lt;/span&gt; class:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip   //builder.RegisterType&lt;consoleoutputservice&gt;().As&lt;ioutputservice&gt;();   string outputFilePath = Path.Combine(Path.GetTempPath(), "magic8BallOutput.txt");   builder.Register(c =&gt; new MultipleOutputService(outputFilePath)).As&lt;ioutputservice&gt;();//snip]]&gt;&lt;/script&gt;In the code above, we're still specifying &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; as our implementation of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;, but we're also providing Autofac with a simple lambda expression to use when creating &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt;.  The good news is, if we keep the code above, nothing else has to change (we'll still get radically different output behavior).&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass3.zip" target="_blank"&gt;Source code for this post&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;but wait, there's more&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://artjumble.com/blog" target="_blank"&gt;Steve&lt;/a&gt; asked a great question: &lt;span class="Apple-style-span" style="color: #0b5394;"&gt;But what if I want to use both MultipleOutputService and ConsoleOutputService? Say there is a user option to select which method to output with, can a DI framework handle this for me?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Since it's trivial to register more than one implementation of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; with the dependency injection framework, the framework can still help us here.  We'll have to write the user-activated output switch option though :)  Here's the approach I see:&lt;ol&gt;&lt;li&gt;Remove &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; and add &lt;span class="inlineCode"&gt;PopupOutputService&lt;/span&gt; and &lt;span class="inlineCode"&gt;FileOutputService&lt;/span&gt; to express our new modes of output.  We'll also keep  &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; around since one of our options is to print to the console.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Since this silly example currently runs in the console, I'm going to let users pass a few command-line parameters specifying how they'd like their output delivered. Legal parameter values will be: "popup","file","console", and "all".  If we get odd combinations like "console all" we'll assume "all".&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;We'll also need a class to hold our configuration. That class we'll be constructed by parsing our command-line arguments and will be passed into our &lt;span class="inlineCode"&gt;ContainerSetup.BuildContainer&lt;/span&gt; method so we'll know which classes to regsiter as implementations of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;.&lt;/li&gt;&lt;/ol&gt;Here's our configuration class, aptly named &lt;span class="inlineCode"&gt;Config&lt;/span&gt;:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip // an enumeration expressing our different output modes public enum OutputModes { console, popup, file, all }; // holds our configuration, this class will be easy to expand later on // if we need to public class Config {  public List&lt;outputmodes&gt; OutputModes { get; private set; }  public Config(List&lt;outputmodes&gt; outputPreferences) {   OutputModes = outputPreferences;  } }//snip]]&gt;&lt;/script&gt;Here's &lt;span class="inlineCode"&gt;ArgumentsParser&lt;/span&gt; its whole point in life is to sanity-check and parse our command-line arguments: &lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class ArgumentsParser {  private string[] arguments;  private string requestedOutputMode = string.Empty;  // default output mode when no args specified  private List&lt;outputmodes&gt; outputModes = new List&lt;outputmodes&gt;(){ OutputModes.console };  public ArgumentsParser(string[] args) {   arguments = args;  }  public Config GetConfig() {   SanityCheckArgs();   return new Config(outputModes);  }  private void SanityCheckArgs() {   // no-args to check   if (arguments.Length == 0)    return;   // too many args   if (arguments.Length &gt; 3)    throw new ArgumentException(     "Too many arguments were specified, expected 'console','popup','file', or 'all'");   SetupOutputModes();  }  private void SetupOutputModes() {   outputModes = new List&lt;outputmodes&gt;();   // see if each requested output-mode exists in our struct   foreach (string outputModeRequested in arguments) {    try {     outputModes.Add(      (OutputModes)Enum.Parse(typeof(OutputModes), outputModeRequested, true)     );    } catch {     throw new ArgumentException(String.Format(      "Illegal output mode '{0}' requested, expected 'console','popup','file', or 'all'",      outputModeRequested));    }   }   // handles odd input like "console all"   if( outputModes.Contains(OutputModes.all) )    outputModes = new List&lt;outputmodes&gt;() { OutputModes.all };  } }//snip]]&gt;&lt;/script&gt;Here are our change to &lt;span class="inlineCode"&gt;ContainerSetup.BuildContainer&lt;/span&gt;, notice the new &lt;span class="inlineCode"&gt;config&lt;/span&gt; parameter:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  public IContainer BuildContainer(Config config) {   _config = config;   // snip - previous code is still valid   // we're now registering our IOutputService based on our pass config configuration class   if (_config.OutputModes.Contains(OutputModes.console) || _config.OutputModes.Contains(OutputModes.all))    builder.RegisterType&lt;consoleoutputservice&gt;().As&lt;ioutputservice&gt;();   if (_config.OutputModes.Contains(OutputModes.popup) || _config.OutputModes.Contains(OutputModes.all))    builder.RegisterType&lt;popupoutputservice&gt;().As&lt;ioutputservice&gt;();   if (_config.OutputModes.Contains(OutputModes.file) || _config.OutputModes.Contains(OutputModes.all)) {    string outputFilePath = Path.Combine(Path.GetTempPath(), "magic8BallOutput.txt");    builder.Register(c =&gt; new FileOutputService(outputFilePath)).As&lt;ioutputservice&gt;();   }   return builder.Build();  }//snip]]&gt;&lt;/script&gt;We're now expecting a collection of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;'s in &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;, so we'll modify that class as follows (notice the new &lt;span class="inlineCode"&gt;IEnumerable&lt;ioutputservice&gt;&lt;/span&gt; constructor parameter and the &lt;span class="inlineCode"&gt;_outputServices&lt;/span&gt; property:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  private IEnumerable&lt;ioutputservice&gt; _outputServices;  // we're now injecting Interfaces, this loosens our coupling to our "injected" dependencies  public Magic8BallSimulator(IMessageService messageService, IInputService inputService,   IEnumerable&lt;ioutputservice&gt; outputServices) {   _messageService = messageService;   _inputService = inputService;   _outputServices = outputServices;  }  public void Run() {   PrintWelcome();   string message = string.Empty;   PrintInputPrompt();   _inputService.GetInput();   while (!_inputService.ExitWasRequested()) {    message = _messageService.GetMessage();    PrintMessage(message);    PrintInputPrompt();    _inputService.GetInput();   }   PrintExit();  }  private void PrintWelcome() {   foreach (IOutputService outputService in _outputServices)    outputService.PrintWelcome();  }  private void PrintInputPrompt() {   foreach (IOutputService outputService in _outputServices)    outputService.PrintInputPrompt();  }  private void PrintMessage(string message) {   foreach (IOutputService outputService in _outputServices)    outputService.PrintMessage(message);  }  private void PrintExit() {   foreach (IOutputService outputService in _outputServices)    outputService.PrintExit();  }//snip]]&gt;&lt;/script&gt;And finally, here's our new &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; tying it all together:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  static void Main(string[] args) {   ContainerSetup containerSetup = new ContainerSetup();   IContainer container = containerSetup.BuildContainer();   container.Resolve&lt;magic8ballsimulator&gt;().Run();  }//snip]]&gt;&lt;/script&gt;The &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/PopupFileOutputServices.html" target="_blank"&gt;&lt;span class="inlineCode"&gt;PopupOutputService&lt;/span&gt; and &lt;span class="inlineCode"&gt;FileOutputService&lt;/span&gt;&lt;/a&gt; classes were trivial, I've posted those &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/PopupFileOutputServices.html" target="_blank"&gt;here&lt;/a&gt;. Here's the new &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass3a.zip" target="_blank"&gt;source-code for this section of the post&lt;/a&gt; as well.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Unit Tests up next&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Although our new code is de-coupled, to ensure that its solid, we'll need to test it a bit.  We'll start by testing each class separately. &lt;a href="http://flexamusements.blogspot.com/2010/10/dependency-injection-part-4-tests.html"&gt;&amp;gt;&amp;gt; Next Post&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-267115615415076024?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/267115615415076024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=267115615415076024' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/267115615415076024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/267115615415076024'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html' title='Dependency Injection Part 3: Making our lives easier'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-76606408394328767</id><published>2010-09-27T23:42:00.011-07:00</published><updated>2011-12-08T23:39:35.303-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><title type='text'>Dependency Injection Part 2: Loose coupling to the rescue</title><content type='html'>&lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-1-dependency.html" target="_blank"&gt;In my last post&lt;/a&gt;, we finally injected dependencies into our &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class, but soon realized that by injecting &lt;i&gt;specific&lt;/i&gt; classes with &lt;i&gt;specific&lt;/i&gt; behaviors we were &lt;i&gt;coupling&lt;/i&gt; &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; to its dependencies.  We discussed how this kind of coupling would make our application brittle for the following reasons:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;If we want to change, say, &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; we risk breaking &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; too, since it depends directly on &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; (and changing the output on the console really shouldn't break our entire application).&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If we get a requirements change to alter how we output messages, say, to send them as Instant Messages or via Email, we'll have to rip out &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; and change &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; as well.  This makes our application less maintainable because changes to one section break and cascade to other parts of our&amp;nbsp;application&amp;nbsp;making the whole thing a pain in the ass to work on.&lt;/li&gt;&lt;/ol&gt;Here's the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; constructor we ended up with last time:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  public Magic8BallSimulator(MessageService messageService, ConsoleInputService ConsoleInputService,    ConsoleOutputService ConsoleOutputService) {   _messageService = messageService;   _ConsoleInputService = ConsoleInputService;   _ConsoleOutputService = ConsoleOutputService;  }//snip]]&gt;&lt;/script&gt;We decided that we needed to alter &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; to depend upon &lt;i&gt;abstractions&lt;/i&gt; instead of concrete &lt;i&gt;implementations&lt;/i&gt;, making &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; &lt;i&gt;loosely coupled&lt;/i&gt; to the classes it depends on.&lt;br /&gt;&lt;br /&gt;Consider our &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; class.  It's a concrete class which has a specific behavior.  It prints output &lt;i&gt;to the console&lt;/i&gt;, and it's "to the console" behavior is rather specific don't you think?  This means that when we inject &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; into &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;, &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; knows not only &lt;i&gt;that&lt;/i&gt; &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; outputs things, but &lt;i&gt;how&lt;/i&gt; it outputs them as well.  Now &lt;i&gt;how&lt;/i&gt; &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; outputs things is its own concern, and we'd like to keep our application flexible enough so we can change how we output things without without breaking &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;. The solution is simple, we need to tell &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; that we have a class capable of handling output, but not how that class does its job.  Here's the solution to our output problem:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Interfaces!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;IOutputService&lt;/b&gt; - a contract to provide output functionality&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public interface IOutputService {  void PrintExit();  void PrintInputPrompt();  void PrintMessage(string message);  void PrintWelcome(); }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;This bit is important:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; is a simple contract which says "I agree to provide the following behavior (&lt;span class="inlineCode"&gt;PrintExit, PrintInputPrompt&lt;/span&gt; etc), &lt;i&gt;but how I work inside is no one else's concern&lt;/i&gt;.  Now we only need a simple modification to &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; to complete our de-coupling.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;ConsoleOutputService&lt;/b&gt; - implements IOutputService and prints to the console&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip class ConsoleOutputService : IOutputService {  //snip - this is the only change to ConsoleOutputService , really, that's it. }//snip]]&gt;&lt;/script&gt;Let's modify &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;'s constructor to take its new de-coupled dependency (notice we're now passing in an &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt; interface instead of an instance of the specific &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; class):&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  private IOutputService _outputService;  // we're now injecting an Interface, this loosens our coupling to our "injected"   // ConsoleOutputService dependency  public Magic8BallSimulator(MessageService messageService, ConsoleInputService inputService,   IOutputService outputService) {   _messageService = messageService;   _inputService = inputService;   _outputService = outputService;  }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;2 more Interfaces&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's de-couple &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; from, the rest of its dependencies.  Here are our remaining interfaces:&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public interface IInputService {  bool ExitWasRequested();  string GetInput(); } public interface IMessageService {  string GetMessage(); }//snip]]&gt;&lt;/script&gt;I won't bore you with pasting copies of &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt; implementing &lt;span class="inlineCode"&gt;IInputService&lt;/span&gt; or of &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt; implementing &lt;span class="inlineCode"&gt;IMessageService&lt;/span&gt;.  If you'd like to have a look at those classes, &lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/ImplementingInterfaces.html" target="_blank"&gt;I've posted them here&lt;/a&gt;.  Stay with me, things are about to get interesting.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Success!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;here's our new &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; class: &lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[// snip  // our "dependencies" are now interfaces  private IMessageService _messageService;  private IInputService _inputService;  private IOutputService _outputService;  // we're now injecting Interfaces, this loosens our coupling to our "injected" dependencies  public Magic8BallSimulator(IMessageService messageService, IInputService inputService,   IOutputService outputService) {   _messageService = messageService;   _inputService = inputService;   _outputService = outputService;  }//snip]]&gt;&lt;/script&gt;&lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; still has its dependencies injected, and behaves the way it did in the last post (in other words, we didn't break anything). &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; still relies on the services provided by its dependent classes, but now it's de-coupled from the concrete details of &lt;i&gt;how&lt;/i&gt; those classes do their work.  Don't believe me?&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Check this out&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A really different implementation of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;:&lt;br /&gt;&lt;table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/newOutputService.png" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="242" src="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/newOutputService.png" width="320" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Yeah, that's certainly *different* :)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;Here's the new implementation of &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;, check out &lt;span class="inlineCode"&gt;PrintMessage()&lt;/span&gt;, it prints messages to the screen, a file, and within a Windows-forms &lt;span class="inlineCode"&gt;MessageBox&lt;/span&gt;:&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class MultipleOutputService : IOutputService {  private string _outputFilePath = String.Empty;  public MultipleOutputService(string outputFilePath) {   _outputFilePath = outputFilePath;  }  private string _message = string.Empty;  public void PrintMessage(string message) {   _message = message;   WriteMessageToConsole();   WriteMessageToFile();   ShowMessagePopup();  }  private void WriteMessageToConsole() {   Console.WriteLine(_message);  }  private void WriteMessageToFile() {   File.AppendAllText(_outputFilePath, _message);  }  // because they're not enough popups in our lives :)  private void ShowMessagePopup() {   MessageBox.Show(_message, "The 8-Ball says");  }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;What did we just do?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Well aside from writing a crazy output class, we've radically changed the output behavior of our application without breaking it! How?  Simple. All &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; knows is that it gets passed (or injected) an instance of a class which implements &lt;span class="inlineCode"&gt;IOutputService&lt;/span&gt;, and our new (slightly odd) &lt;span class="inlineCode"&gt;MultipleOutputService&lt;/span&gt; class does that.&lt;br /&gt;&lt;br /&gt;Here's our new &lt;span class="inlineCode"&gt;SimulationRunner&lt;/span&gt; class, it injects dependent classes into &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; (remember, &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; still sees these classes as &lt;i&gt;Interfaces&lt;/i&gt;).&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip  static void Main(string[] args) {   // output file for new output-service   string outputFilePath = Path.Combine(Path.GetTempPath(), "magic8BallOutput.txt");   Magic8BallSimulator simulator = new Magic8BallSimulator(    new MessageService(),    new ConsoleInputService(),    new MultipleOutputService(outputFilePath) // new odd output class   );   simulator.Run();  }//snip]]&gt;&lt;/script&gt;&lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass2.zip" target="_blank"&gt;Source code for this post&lt;/a&gt; (that really is an odd output class).&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;In the next post&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We'll have a look at a Dependency Injection framework for .NET (there are quite a few to chose from) and how it can make our lives easier.  We'll also discuss the differences between running our application and starting it up to run.  We'll also check out a better (and more interesting) way of wiring in our dependencies to &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;. &lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html"&gt;&amp;gt;&amp;gt; Next Post&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-76606408394328767?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/76606408394328767/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=76606408394328767' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/76606408394328767'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/76606408394328767'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/09/dependency-injection-part-2-loose.html' title='Dependency Injection Part 2: Loose coupling to the rescue'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-4851013244369224074</id><published>2010-09-26T20:04:00.028-07:00</published><updated>2011-12-08T23:31:29.998-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><title type='text'>Dependency Injection Part 1: Dependency Injection at last</title><content type='html'>&lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-0-tragedy.html" target="_blank"&gt;In my last post&lt;/a&gt;&amp;nbsp;(read it, its short, honestly), I created an awful&amp;nbsp;&lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;&amp;nbsp;class which violated the Single Responsibility Principle by concerning itself with:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Message retrieval&lt;/li&gt;&lt;li&gt;Reading user input&lt;/li&gt;&lt;li&gt;Printing message and program output&lt;/li&gt;&lt;li&gt;Handling user-interactions via a lame read-eval-print (aka REPL) loop&lt;/li&gt;&lt;/ul&gt;In this post, I'm going to:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Create a class per responsibility&lt;/li&gt;&lt;li&gt;Make the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;class depend on those classes&lt;/li&gt;&lt;li&gt;&lt;i&gt;Inject &lt;/i&gt;those dependent classes into &lt;span class="inlineCode"&gt;Magic8BallSimulator&amp;nbsp;&lt;/span&gt;(you'll laugh when you see how simple this turns out to be).&lt;/li&gt;&lt;li&gt;and finally, make you sad by exposing a new problem with the code&lt;/li&gt;&lt;/ol&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;The new classes broken out are:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;MessageService&lt;/b&gt; - gets messages&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class MessageService {  public MessageService() {   SetupMessages();  }  private List&lt;string&gt; messages = new List&lt;string&gt;();  private void SetupMessages() {   messages = new List&lt;string&gt;() {     "Signs point to yes.",     "Yes."    //snip   };  }  public string GetMessage() {   Random random = new Random();   int index = random.Next(0, messages.Count - 1);   return messages[index];  } }//snip]]&gt;&lt;/script&gt;&lt;b&gt;ConsoleInputService&lt;/b&gt; - reads user input from the console.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class ConsoleInputService {  private string input = "input-initializer";  public string GetInput() {   input = Console.ReadLine();   return input;  }  public bool ExitWasRequested() {   return String.IsNullOrEmpty(input);  }}//snip]]&gt;&lt;/script&gt;&lt;b&gt;ConsoleOutputService&lt;/b&gt; - prints output to the console.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class ConsoleOutputService {  public void PrintMessage(string message) {   Console.WriteLine(message);   Console.WriteLine();  }  public void PrintWelcome() {   Console.WriteLine("Welcome to the Magic 8 Ball simulator");  }  public void PrintInputPrompt() {   Console.Write("Ask a Question, or press [Enter] to exit &gt;&gt; ");  }  public void PrintExit() {   Console.WriteLine("Goodbye");  } }//snip]]&gt;&lt;/script&gt;&lt;b&gt;Magic8BallSimulator&lt;/b&gt; - Simulates a Magic 8 Ball via a lame read-eval-print (aka REPL) loop.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Oh &lt;i&gt;there's&lt;/i&gt; a bit of Dependency Injection&lt;/span&gt; - &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; are &lt;i&gt;dependencies&lt;/i&gt; of &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;, which is to say that &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; cannot do its job of simulating a Magic 8 Ball without them.  These dependencies are given to this class (or &lt;i&gt;injected&lt;/i&gt;) as simple constructor parameters.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class Magic8BallSimulator {  // these 3 classes are "dependencies" of this class, in that this class cannot  // do its job without their services.  private MessageService _messageService;  private ConsoleInputService _inputService;  private ConsoleOutputService _outputService;  // here our dependencies are "injected" into this class, that's depedency injection,   // really that's it!  public Magic8BallSimulator(MessageService messageService, ConsoleInputService inputService,    ConsoleOutputService outputService) {   _messageService = messageService;   _inputService = inputService;   _outputService = outputService;  }  public void Run() {   _outputService.PrintWelcome();   string message = string.Empty;   _outputService.PrintInputPrompt();   _inputService.GetInput();   while (!_inputService.ExitWasRequested()) {    message = _messageService.GetMessage();    _outputService.PrintMessage(message);    _outputService.PrintInputPrompt();    _inputService.GetInput();   }   _outputService.PrintExit();  } }//snip]]&gt;&lt;/script&gt;&lt;b&gt;SimulationRunner&lt;/b&gt; - Runs the simulator class, and &lt;i&gt;injects&lt;/i&gt; new instances of the&amp;nbsp;dependent&amp;nbsp;classes into&amp;nbsp;&lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt;.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class SimulationRunner {  static void Main(string[] args) {   // watch me now, I'm injecting dependent classes   Magic8BallSimulator simulator = new Magic8BallSimulator(    new MessageService(),    new ConsoleInputService(),    new ConsoleOutputService()   );   simulator.Run();  } }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Hmm, there's still a problem with this code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's have one more look at the &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; constructor.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[  // snip  public Magic8BallSimulator(MessageService messageService, ConsoleInputService ConsoleInputService,    ConsoleOutputService ConsoleOutputService) {   _messageService = messageService;   _ConsoleInputService = ConsoleInputService;   _ConsoleOutputService = ConsoleOutputService;  }  // snip]]&gt;&lt;/script&gt;Notice how we're injecting instances of &lt;span class="inlineCode"&gt;MessageService&lt;/span&gt;, &lt;span class="inlineCode"&gt;ConsoleInputService&lt;/span&gt;, and &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt;? What this does is, for example, &lt;i&gt;couple&lt;/i&gt; &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; to &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; which is a &lt;i&gt;specific&lt;/i&gt; class printing output in a &lt;i&gt;specific&lt;/i&gt; way (to the console).  This &lt;i&gt;coupling&lt;/i&gt; exposes us to 2 threats:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;If we want to change, say, &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; we risk breaking &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; too, since it depends directly on &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; (and changing the output on the console really shouldn't break our entire application).&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If we get a requirements change to alter how we output messages, say, to send them as Instant Messages or via Email, we'll have to rip out &lt;span class="inlineCode"&gt;ConsoleOutputService&lt;/span&gt; and change &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; as well.  This makes our application less maintainable because changes to one section break and cascade to other parts of our&amp;nbsp;application&amp;nbsp;making the whole thing a pain in the ass to work on.&lt;/li&gt;&lt;/ol&gt;You might be tempted to try something like the code below, &lt;i&gt;don't do it&lt;/i&gt;. &lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[  // snip  public Magic8BallSimulatorEvenWorse() {   _messageService = new MessageService();   _inputService = new ConsoleInputService();   _outputService = new ConsoleOutputService();  }  // snip]]&gt;&lt;/script&gt;This is actually &lt;i&gt;worse&lt;/i&gt; because it adds another responsibility to &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; namely that of &lt;i&gt;creating its own dependencies&lt;/i&gt;.  This actually couples us even tighter to these dependent classes since now we're not only using their specific service-implementations, but instantiating them as well.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Luckily there's a simple way out.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We need to alter &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; to depend upon &lt;i&gt;abstractions&lt;/i&gt; instead of concrete &lt;i&gt;implementations&lt;/i&gt;, making &lt;span class="inlineCode"&gt;Magic8BallSimulator&lt;/span&gt; &lt;i&gt;loosely coupled&lt;/i&gt; to the classes it depends on.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass1.zip" target="_blank"&gt;(Awfully coupled) source code for this post&lt;/a&gt;, witness the bad design first hand!&lt;br /&gt;&lt;br /&gt;We'll explore this way out in the &lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-2-loose.html"&gt;next post&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-4851013244369224074?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/4851013244369224074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=4851013244369224074' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4851013244369224074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4851013244369224074'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/09/dependency-injection-part-1-dependency.html' title='Dependency Injection Part 1: Dependency Injection at last'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-4361536365759401918</id><published>2010-09-26T11:02:00.024-07:00</published><updated>2011-12-08T23:31:12.725-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Introduction'/><title type='text'>Dependency Injection Part 0: The tragedy</title><content type='html'>Consider this simple &lt;a href="http://en.wikipedia.org/wiki/Magic_8-Ball" target="_blank"&gt;Magic 8-Ball&lt;/a&gt; Simulator class, here's the UI:&lt;br /&gt;&lt;br /&gt;&lt;img border="0" src="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/images/8ballOutput.png" /&gt;&lt;br /&gt;&lt;br /&gt;and here is the code, reading it should take perhaps a minute. &amp;nbsp;Heck just read the &lt;span class="inlineCode"&gt;Run()&lt;/span&gt; method.&lt;br /&gt;&lt;script class="brush: c#" type="syntaxhighlighter"&gt;&lt;![CDATA[//snip public class Magic8BallSimulator {  public Magic8BallSimulator() {   SetupMessages();  }  public void Run() {   PrintWelcome();   PrintInputPrompt();   GetInput();   while (!ExitWasRequested()) {    GetMessage();    PrintMessage();    PrintInputPrompt();    GetInput();   }   PrintExit();  }  private List&lt;string&gt; messages = new List&lt;string&gt;();  private void SetupMessages() {   messages = new List&lt;string&gt;() {     "Signs point to yes.",     "Yes."    //snip   };  }  private string message = String.Empty;  private void GetMessage() {   Random random = new Random();   int index = random.Next(0, messages.Count - 1);   message = messages[index];  }  private void PrintMessage() {   Console.WriteLine(message);   Console.WriteLine();  }  private void PrintInputPrompt() {   Console.Write("Ask a Question, or press [Enter] to exit &gt;&gt; ");  }  private string input = "place-holder input";  private void GetInput() {   input = Console.ReadLine();  }  private void PrintWelcome() {   Console.WriteLine("Welcome to the Magic 8 Ball simulator");  }  private void PrintExit() {   Console.WriteLine("Goodbye");  }  private bool ExitWasRequested() {   return String.IsNullOrEmpty(input);  } }//snip]]&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Why does this class suck?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Simple, &lt;i&gt;it is doing way too much work&lt;/i&gt;. &amp;nbsp;As our requirements change and we work on this class, we'll start to introduce bugs in unexpected places.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3366ff;"&gt;[Requirements Change]: We'd like to store the 8-ball messages in a flat-file database.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Can you honestly do that without the risk of breaking unrelated code like&amp;nbsp;message&amp;nbsp;output? &amp;nbsp;Imagine the following scenario:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;You:&lt;/b&gt; "Yeah, so I was making changes to how the messages are stored and fetched and I broke the way they print to the screen."&lt;br /&gt;&lt;b&gt;Your teammates:&lt;/b&gt; "Hmm, OK". &lt;i&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;sheesh this guy cannot work on one feature without breaking something completely unrelated&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Hey those two&amp;nbsp;responsibilities&amp;nbsp;are not related! So put them in different classes. In fact the code above has lots of&amp;nbsp;responsibilities:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Message retrieval&lt;/li&gt;&lt;li&gt;Reading user input&lt;/li&gt;&lt;li&gt;Printing message and program output&lt;/li&gt;&lt;li&gt;Handling user-interactions via a lame read-eval-print (aka REPL) loop&lt;/li&gt;&lt;/ul&gt;Separate responsibilities,&amp;nbsp;separate&amp;nbsp;classes,&amp;nbsp;separate&amp;nbsp;reasons for changing code. This is the&amp;nbsp;&lt;b&gt;Single-Responsibility Principle&lt;/b&gt;, a grand and simple assertion that:&lt;b&gt;&amp;nbsp;There should never be more than one reason for a class to change. &lt;/b&gt;&amp;nbsp;In my experience, most programmers dismiss this principle as &lt;i&gt;obvious&lt;/i&gt;, and yet &lt;i&gt;cannot seem to follow it when programming&lt;/i&gt;. &amp;nbsp;In fact this kind of code is present in project's which I've otherwise been damn proud to work on.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/26355144/miscExamples/dotNetDiTalk/Pass0.zip" target="_blank"&gt;Source code for this post&lt;/a&gt;, please only download it to shun it, and never speak of it again.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Let's fix this code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In the &lt;a href="http://flexamusements.blogspot.com/2010/09/dependency-injection-part-1-dependency.html"&gt;next post&lt;/a&gt; we'll see what breaking this class up entails.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-4361536365759401918?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/4361536365759401918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=4361536365759401918' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4361536365759401918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4361536365759401918'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/09/dependency-injection-part-0-tragedy.html' title='Dependency Injection Part 0: The tragedy'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-4109036198322078450</id><published>2010-06-11T23:44:00.003-07:00</published><updated>2011-12-08T23:37:30.984-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AIR'/><category scheme='http://www.blogger.com/atom/ns#' term='Threads'/><title type='text'>AIR Ersatz Thread Example</title><content type='html'>Citing the lack of threading as an example, I &lt;a href="http://flexamusements.blogspot.com/2010/06/air-has-ways-to-go-as-desktop.html"&gt;recently ranted&lt;/a&gt; that Air had a ways to go as a desktop application platform. &amp;nbsp;In the context of such rich platforms like Winforms and WPF, I think that's a fair assertion (the API's are all public, judge for yourself).&amp;nbsp;At any rate, leaving aside dramatic pronouncements, I decided to create an example AIR application which would both search the file-system and keep the UI responsive. &amp;nbsp;This silly example simulates threading by doing small bits of searching on &lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ENTER_FRAME&lt;/span&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Have a look &lt;a href="http://dl.dropbox.com/u/26355144/flexExamples/ersatzAirThreads/srcview/index.html"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-4109036198322078450?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/4109036198322078450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=4109036198322078450' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4109036198322078450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4109036198322078450'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/06/air-ersatz-thread-example.html' title='AIR Ersatz Thread Example'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-627064317428220596</id><published>2010-06-09T18:35:00.032-07:00</published><updated>2010-10-26T23:37:56.977-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AIR'/><category scheme='http://www.blogger.com/atom/ns#' term='Threads'/><title type='text'>AIR has a ways to go as a desktop application platform</title><content type='html'>&lt;div&gt;&lt;div class="separator" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em; text-align: center;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/__7eX1ilwql8/TBEY8GaQiOI/AAAAAAAAAEE/SiuaO5Jiv_4/s320/sillySearch.png" /&gt;&lt;/div&gt;&lt;br /&gt;Imagine a simple file-search desktop application. &amp;nbsp;A user could type in a search string and press a button to start the search. &amp;nbsp;The application would then recurse the directories on the user's computer searching for files whose names contained that string. &amp;nbsp;Nothing complex, not even a cancel-search button or progress bar.&lt;br /&gt;&lt;br /&gt;Now while the application recurses sub-directories, we want the user-interface to be responsive so the user (and the operating system) don't think the application has crashed. &amp;nbsp;In .NET and JAVA, this is as simple as putting the file-search and user-interface on different threads. &lt;br /&gt;&lt;br /&gt;In AIR this is simply not possible.&amp;nbsp;Using the file-system search as an example, Adobe provides a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;getDirectoryListingAsync()&lt;/span&gt;&lt;/span&gt; function, you call it and then listen for a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;FileListEvent&lt;/span&gt;&lt;/span&gt;. &amp;nbsp;Since our simple application needs to recurse sub-directories in search of matching files, it will need to parse the returned file-system items and venture into the returned directories calling &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;getDirectoryListingAsync&lt;/span&gt;&lt;/span&gt; on those as well and so on. &amp;nbsp;Adobe also provides the &lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;getDirectoryListing()&lt;/span&gt;&lt;/span&gt; function which if used will yield much the same outcome through synchronously and without the need for an event listener.&lt;br /&gt;&lt;br /&gt;A few folks have asserted that&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;getDirectoryListingAsync()&lt;/span&gt;&lt;/span&gt;&amp;nbsp;does in fact run on a&amp;nbsp;separate&amp;nbsp;thread. &amp;nbsp;If that's true then great, the problem is the&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;FileListEvent&lt;/span&gt;&lt;/span&gt;s are still handled on the UI thread, and when recursing through lower-level directories, there are quite a few&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;FileListEvent&lt;/span&gt;&lt;/span&gt;s. &amp;nbsp;So&amp;nbsp;sadly, when these methods are used to access the file-system in AIR,&amp;nbsp;the UI becomes sluggish and unresponsive, especially when recursing from lower directories. &amp;nbsp;AIR is simply doing too much work on one thread and doesn't have the resources to paint the UI. &lt;br /&gt;&lt;br /&gt;There are a lot of &lt;a href="http://blog.generalrelativity.org/actionscript-30/green-threads/"&gt;smart folks&lt;/a&gt;&amp;nbsp;(&lt;a href="http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html"&gt;another smart person&lt;/a&gt;) who've come up with &lt;a href="http://code.google.com/p/greenthreads/"&gt;work-arounds for this limitation&lt;/a&gt;, most attacks boil down to this:&lt;br /&gt;&lt;br /&gt;Split up your process in some way and call sections of your newly split-up process in chunks on each frame, allowing AIR to paint the UI.&lt;br /&gt;&lt;br /&gt;That sure is a lot of work.&lt;br /&gt;&lt;br /&gt;If we had threads, we could write this application via two simple concurrent processes. &amp;nbsp;Recursing sub-directories is easy to do, and so is responding to user gestures on the UI. &amp;nbsp;The sub-directory searching process could even raise events that we'd listen for on the UI thread letting the user know interesting bits like: progress, files found, directories searched, etc. &amp;nbsp;The directory search could even be a simple, synchronous process (hey, its running on its own thread).&lt;br /&gt;&lt;br /&gt;It is certainly worth noting that concurrent programming is hard (if you don't think so, you're either a badass or are new to it) and introduces subtle and confounding bugs. &amp;nbsp;The pay-offs are huge but you run a risk as well (Microsoft charted a great middle course with &lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;BackgroundWorker&lt;/span&gt;&lt;/span&gt;). &amp;nbsp;People are always clamoring for features in AIR, so here's my vote. &amp;nbsp;Give me threads.&lt;br /&gt;&lt;br /&gt;Edit: I've put up an &lt;a href="http://flexamusements.blogspot.com/2010/06/air-ersatz-thread-example.html"&gt;Ersatz threading example&lt;/a&gt; which attempts the attack outlined above.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-627064317428220596?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/627064317428220596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=627064317428220596' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/627064317428220596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/627064317428220596'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/06/air-has-ways-to-go-as-desktop.html' title='AIR has a ways to go as a desktop application platform'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/__7eX1ilwql8/TBEY8GaQiOI/AAAAAAAAAEE/SiuaO5Jiv_4/s72-c/sillySearch.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-7379797634993885907</id><published>2010-03-24T14:42:00.005-07:00</published><updated>2010-03-24T14:48:16.581-07:00</updated><title type='text'>A Funny Skype chat</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I'm lucky enough to work with some smart (and funny) folks.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;[12:59:18 PM] &lt;a href="http://joelhooks.com/"&gt;Joel Hooks&lt;/a&gt;: I'm not a geek&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;[12:59:52 PM] Max Nachlinger: yeah a certain degree of self-deception is necessary for one's sanity :)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;[ 1:00:04 PM] Max Nachlinger: I'm not a geek either&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;[ 1:00:05 PM] &lt;a href="http://flexiblegorilla.com/"&gt;Glen Whitbeck&lt;/a&gt;: ... admitting it is the first step towards recovery&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;[ 1:00:07 PM] &lt;a href="http://joelhooks.com/"&gt;Joel Hooks&lt;/a&gt;: fu Nachlinger!&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-7379797634993885907?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/7379797634993885907/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=7379797634993885907' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7379797634993885907'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7379797634993885907'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/03/funny-skype-chat.html' title='A Funny Skype chat'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-8208291353070759779</id><published>2010-02-14T09:52:00.006-08:00</published><updated>2011-12-08T23:36:21.724-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='List'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>Simple Horizontal Scrolling List</title><content type='html'>I recently needed a horizontal scrolling list which would display 1 item at a time (2 items while the new item is sliding in) and have 1/32nd the functionality of &lt;a href="http://livedocs.adobe.com/flex/3/langref/mx/controls/HorizontalList.html"&gt;Flex's HorizontalList&lt;/a&gt;. Here's a modified&amp;nbsp;&lt;a href="http://dl.dropbox.com/u/26355144/flexExamples/singleItemHList/index.html"&gt;example&lt;/a&gt;&amp;nbsp;(right-click to view source).&lt;br /&gt;&lt;br /&gt;Edit: I noticed an annoying flicker in this control and have fixed that.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-8208291353070759779?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/8208291353070759779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=8208291353070759779' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/8208291353070759779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/8208291353070759779'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/02/simple-horizontal-scrolling-list.html' title='Simple Horizontal Scrolling List'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-3009319747859686161</id><published>2010-02-09T20:50:00.011-08:00</published><updated>2010-09-28T00:22:12.261-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Itemrenderer'/><category scheme='http://www.blogger.com/atom/ns#' term='List'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>Flex ItemRenderers ought to be home-schooled</title><content type='html'>ItemRenderers ought to be considered private children of their parent control and get their data from their parent only. &amp;nbsp;Of course sometimes itemRenderers need data beyond what's supplied to them via &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;IDataRenderer&lt;/span&gt;&lt;/span&gt;. &amp;nbsp;Imagine that we've a List-based control and want our itemRenderer children to know when some bit of data has changed perhaps&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;isTheWorldEnding:Boolean&lt;/span&gt;&lt;/span&gt;. &amp;nbsp;The itemRenderers can then prepare from the upcoming conflagration by changing their states accordingly. &amp;nbsp;I've seen lots of bad solutions to this problem, two common ones are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Injecting a Presentation-Model into the itemRenderers (bad)&lt;/li&gt;&lt;li&gt;referencing an awful Singleton with the desired data (&lt;i&gt;real &lt;/i&gt;bad)&lt;/li&gt;&lt;/ul&gt;Here's a better way. Sub-class the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;List &lt;/span&gt;&lt;/span&gt;control (make a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;MyList &lt;/span&gt;&lt;/span&gt;control) and add a publicly accessible property of &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;isTheWorldEnding&lt;/span&gt;&lt;/span&gt;. &amp;nbsp;In &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;MyList &lt;/span&gt;&lt;/span&gt;when &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;isTheWorldEnding &lt;/span&gt;&lt;/span&gt;is set to a new value, call &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;invalidateList()&lt;/span&gt;&lt;/span&gt; to refresh the renderers. Meanwhile, implement &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;IDropInListItemRenderer &lt;/span&gt;&lt;/span&gt;in the renderers and get &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;isTheWorldEnding &lt;/span&gt;&lt;/span&gt;by casting &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;_listData.owner as MyList&lt;/span&gt;&lt;/span&gt;. &amp;nbsp;Here's the code to be put in &lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;MyList&lt;/span&gt;&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;script class="brush: as3" type="syntaxhighlighter"&gt;&lt;![CDATA[protected var _isTheWorldEnding:Boolean;[Bindable]public function get isTheWorldEnding():Boolean{ return _isTheWorldEnding;}public function set isTheWorldEnding(value:Boolean):void{ if (_isTheWorldEnding != value) {  _isTheWorldEnding = value;  invalidateList(); // things have changed, update all the child renderers }}]]&gt;&lt;/script&gt;&lt;br /&gt;and the code to be placed in our renderer:&lt;br /&gt;Edit: &lt;a href="http://joelhooks.com/"&gt;Joel&lt;/a&gt;&amp;nbsp;pointed out that it would be nice to have a convenient method to cast the list, here it is:&lt;br /&gt;&lt;script class="brush: as3" type="syntaxhighlighter"&gt;&lt;![CDATA[protected function get myList():MyList { return _listData.owner as MyList }protected var _listData:BaseListData;protected var isTheWorldEnding:Boolean;protected var isTheWorldEndingChanged:Boolean;public function set listData(value:BaseListData):void{ if (!value)  return; _listData = value; if( isTheWorldEnding != myList.isTheWorldEnding ) {  isTheWorldEnding = myList.isTheWorldEnding;  isTheWorldEndingChanged = true;  invalidateProperties(); // figure out how to respond in commitProperties }}]]&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-3009319747859686161?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/3009319747859686161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=3009319747859686161' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/3009319747859686161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/3009319747859686161'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/02/flex-itemrenderers-ought-to-be-home.html' title='Flex ItemRenderers ought to be home-schooled'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-1666071893982522588</id><published>2010-01-16T16:53:00.024-08:00</published><updated>2010-02-05T21:38:10.642-08:00</updated><title type='text'>Server Errors</title><content type='html'>When writing a client app, we can lump server-errors into two broad categories:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exceptions&lt;/b&gt; - the code running on the server has failed in an unexpected way.  Examples:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The server-code cannot write to or read from disk.&lt;/li&gt;&lt;li&gt;The server operation violated a constraint in the database.&lt;/li&gt;&lt;li&gt;The server cannot find a matching method signature for your request.&lt;/li&gt;&lt;li&gt;and so on.&lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Business Errors&lt;/b&gt; - The request violated a business rule in our application.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;An account with that email address already exists.&lt;/li&gt;&lt;li&gt;The submitted data is not valid.&lt;/li&gt;&lt;li&gt;We cannot lend you "Breakin 2 Electric Boogaloo", you already have that video checked out.&lt;/li&gt;&lt;/ul&gt;In my opinion a server exception should never be pitched to the client for at least two reasons:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;exceptions often expose sensitive information, and &lt;/li&gt;&lt;li&gt;the server-code ought to handle the exceptions in question; not just re-throw them.&lt;/li&gt;&lt;/ol&gt;Now the server certainly needs to let the client know something went wrong, but that response need only indicate the severity of the exception and whether or not the client should try again.&lt;br /&gt;&lt;br /&gt;Consider the case where a user uploads a corrupt file and the server throws an exception while trying to read it.  In this case the client ought to inform the user that their file was malformed and to try again.  If user-input from the client causes a violation of a database constraint however, then the software has failed and there's no point in allowing the user to retry their operation.&lt;br /&gt;&lt;br /&gt;Back to the two categories of errors, Exceptions and Business Errors, which do you think the client should handle?&lt;br /&gt;&lt;br /&gt;Both? &lt;i&gt;Hell yes both!&lt;/i&gt;  Here are a few ways to recover:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Recovering from Exceptions&lt;/b&gt;&lt;br /&gt;If the exception is fatal, the server should send a text message to the poor bastard(s) on call and return the fatal-status to the client.  The client could then display a standardized alert, much like the following:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="color: red;"&gt;We're sorry but we've encountered an error &lt;/span&gt;&lt;insert-action-here&gt;&lt;span style="color: red;"&gt;,  please try again later.  We apologize for the inconvenience, the support staff has been notified and is working to resolve this issue.  If you have questions, please contact our support staff at support@domain.com&lt;/span&gt;&lt;/insert-action-here&gt;&lt;/blockquote&gt;The user would then be logged-out for the application is no longer stable.  This of course is a severity 1, high-priority, wake-your-ass-up-and-fix-it issue.  Have I mentioned how much I hate maintenance?  At any rate, say a user uploaded a corrupt file causing a server exception and we'd like for the user to try again.  Our Alert might read:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="color: red;"&gt;We're sorry but we've encountered an error processing the file you uploaded.  Please fix the file, or try uploading a different file.  We apologize for the inconvenience.  If you have questions, please contact our support staff at support@domain.com&lt;/span&gt;&lt;/blockquote&gt;&lt;b&gt;Recovering from Business Errors&lt;/b&gt;&lt;br /&gt;This actually varies depending on the error, but it is worth noting that the server ought to return a code indicating the error in question so the client can proceed accordingly.&lt;br /&gt;&lt;br /&gt;I've used variants of the following server-response object on lots of projects with success:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;A useful result object&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;script class="brush: csharp" type="syntaxhighlighter"&gt;&lt;![CDATA[/* Wraps all server responses */public class OperationResult{ /* Status-response: options are "error", "business-error", or "ok". You could make this an enum. */ public string operationStatus; /* Should the client try the operation again? */ public bool tryAgain; /* For server-faults: pick some rational fault-codes, e.g. "corrupt-file", "db-connection", "db-constraint" etc */ public string faultCode; /* Business response, such as "account-not-found" etc */ public string businessStatus; /* Response pay-load, could be anything. */ public object result;}]]&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;When I get one of these back from the server, I check the operationStatus first and if its OK, I parse the result property.  On "error" I then see if I need to try again (and often keep a count of how many times I've retried) and use the "faultCode" to drive my message to the user.  On business errors, I check the businessStatus and notify the user accordingly.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-1666071893982522588?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/1666071893982522588/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=1666071893982522588' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/1666071893982522588'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/1666071893982522588'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2010/01/server-errors.html' title='Server Errors'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-4678973137028442825</id><published>2009-10-10T18:53:00.013-07:00</published><updated>2011-12-08T23:28:56.303-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Database design'/><title type='text'>NULLs in SQL</title><content type='html'>In SQL, NULL is an indicator of the absence of value;  consequently, the assertion that NULL = NULL cannot be parsed.  To be evaluated, something must first have a value.  I've certainly not explained anything here, I've merely proven that I understand the meaning and use of the English word "evaluate".&lt;br /&gt;&lt;br /&gt;Imagination can be fairly described as visual thinking, and often when folks imagine NULL, they conjure images of empty strings or zeros; but both of these are values.  Try imagining nothing.  My favorite (though over-used) example of imagination versus non-visual thought is a Hectogon (a hundred-sided polygon).  Try as you might, you cannot form a mental-image of this shape, but you can certainly think about it all the same.&lt;br /&gt;&lt;br /&gt;Back to SQL.  Consider the following simple table of Colors:&lt;p class="MsoNormal"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/__7eX1ilwql8/StE7nAbXkiI/AAAAAAAAACQ/3HhdgRc5sBc/s1600-h/colorTable.png"&gt;&lt;img style="cursor: pointer; width: 320px; height: 154px;" src="http://3.bp.blogspot.com/__7eX1ilwql8/StE7nAbXkiI/AAAAAAAAACQ/3HhdgRc5sBc/s320/colorTable.png" alt="" id="BLOGGER_PHOTO_ID_5391155770254922274" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;We allow the name column to be NULL, for 2 reasons:&lt;br /&gt;&lt;br /&gt;1)    we might not know the name of a given color or&lt;br /&gt;2)    perhaps this combination of red, green, and blue lacks a known name altogether&lt;br /&gt;&lt;br /&gt;We cannot, however, allow the red, green, or blue columns to be NULL, for each of these is an essential bit of information used to define a Color.  So we've tables which represent Entities, and those Entities have attributes some of which are necessary to define and describe the Entity in question.  Basic database concepts.  Consider this table of Tools:&lt;p class="MsoNormal"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/StE7uBtiWEI/AAAAAAAAACY/OpHVhHKrlEk/s1600-h/toolTable.png"&gt;&lt;img style="cursor: pointer; width: 272px; height: 110px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/StE7uBtiWEI/AAAAAAAAACY/OpHVhHKrlEk/s320/toolTable.png" alt="" id="BLOGGER_PHOTO_ID_5391155890858645570" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;Can you spot the error?  Here's a hint: "Could you hand me a tool?" , "Sure, which tool?",  "I don't know."  "Well how about this one?"  "I really don't know" and so it goes.  The problem with this table is that we've allowed an essential attribute (the name of the Tool in question) to be NULL.  I've seen lots of these awful tables in production we're-so-proud-of-our-flagship-product applications.&lt;br /&gt;&lt;/p&gt;NULL and NOT NULL are important considerations, and the horrors continue when we find Tools with empty strings for names and NULL as well.  This means that:&lt;br /&gt;&lt;br /&gt;1)    the client failed to require the obvious data&lt;br /&gt;2)    the middle-tier failed to check for the required data&lt;br /&gt;3)    the database allowed the bad data right in as well.&lt;br /&gt;&lt;br /&gt;There are lots of defenses against bad data, foreign keys are great for this as well.  Perhaps I'll add another crabby post about those.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-4678973137028442825?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/4678973137028442825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=4678973137028442825' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4678973137028442825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/4678973137028442825'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2009/10/nulls-in-sql.html' title='NULLs in SQL'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/__7eX1ilwql8/StE7nAbXkiI/AAAAAAAAACQ/3HhdgRc5sBc/s72-c/colorTable.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-976645491600133865</id><published>2009-08-05T09:34:00.006-07:00</published><updated>2011-12-08T23:32:45.061-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Calendar'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>Month Calendar control (with a MATE test-app and Degrafa skins)</title><content type='html'>Here's a &lt;a href="http://dl.dropbox.com/u/26355144/flexExamples/monthCalendar/index.html"&gt;month-calendar&lt;/a&gt; (right-click to view source) I recently built.  The skins are done with Degrafa and the test-app leverages MATE.&lt;br /&gt;&lt;br /&gt;I'm honestly ambivalent about doing skins via Degrafa, for tools like Illustrator and Flash seem better suited for that kind of work (and you don't have to recompile to check simple things like colors, alphas and ratios).  Every team has its own workflow it seems.&lt;br /&gt;&lt;br /&gt;-- Edit&lt;br /&gt;I've implemented the following changes suggested by a few fellow programmers on flexcoders:&lt;br /&gt;&lt;ol&gt;&lt;li&gt; You can now  add events via a day-cell double-click.&lt;/li&gt;&lt;li&gt; The description-text is now printed on the Event.&lt;/li&gt;&lt;li&gt; Changed the Add/Edit Event Popup's title to match the Add Button text.&lt;/li&gt;&lt;/ol&gt;Thanks Kevin and Pedro!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-976645491600133865?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/976645491600133865/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=976645491600133865' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/976645491600133865'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/976645491600133865'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2009/08/month-calendar-control-with-mate-test.html' title='Month Calendar control (with a MATE test-app and Degrafa skins)'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-7225628565930031001</id><published>2009-06-27T08:43:00.011-07:00</published><updated>2010-09-28T00:23:56.971-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Database design'/><title type='text'>Boolean columns suck as statuses or types</title><content type='html'>A boolean column in a database table can usually have two possible values, TRUE or FALSE (I say &lt;span style="font-style: italic;"&gt;usually&lt;/span&gt;, for one could make the column in question NULLable, allowing for the absence of a value).  The problem with boolean columns is that in any non-trivial database, they're not expressive enough to describe the status or type of an entity.  Consider the following simple requirement.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Users may access the system by typing in their username and password. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Easy enough; here's the table (we might even imagine an index on username and password to speed up logins):&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/__7eX1ilwql8/SkY-ijvRyiI/AAAAAAAAABY/fJMYxmIOP80/s1600-h/bools_1.png"&gt;&lt;img style="cursor: pointer; width: 200px; height: 75px;" src="http://4.bp.blogspot.com/__7eX1ilwql8/SkY-ijvRyiI/AAAAAAAAABY/fJMYxmIOP80/s200/bools_1.png" alt="" id="BLOGGER_PHOTO_ID_5352033970606426658" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As they often do, the requirements changed:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;We'll also need the ability to disable certain users' access.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s1600-h/new_developer.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 84px; height: 93px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s200/new_developer.png" alt="" id="BLOGGER_PHOTO_ID_5352034474884768450" border="0" /&gt;&lt;/a&gt;"That's easy enough", thinks the new developer, "I'll add a boolean field indicating whether or not the User is active. After all they're either active or they're not."  Here's his design.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/SkY_M0Q6sVI/AAAAAAAAABo/j5bTd9iOpII/s1600-h/bools_2.png"&gt;&lt;img style="cursor: pointer; width: 200px; height: 92px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/SkY_M0Q6sVI/AAAAAAAAABo/j5bTd9iOpII/s200/bools_2.png" alt="" id="BLOGGER_PHOTO_ID_5352034696596992338" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;We can imagine that an inactive user attempts to login, and the system politely informs them that:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;We're sorry but your account is no longer active.  If you feel this is an error, please contact support at: support@domain.com.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Requirements changed again (at least in this example the requirements are still reasonable):&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;We're worried about spammers, so new users will have to verify their accounts before we allow them to login.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is a standard process for lots of websites, the system sends you an email with a link containing a code which verifies your account.  I am an English speaker and have learnt the meaning of the terms "active" and "inactive".  Certainly un-verified new users are not active as they cannot log-in, but they're not really inactive either for we've not explicitly disabled them.  Imagine showing an unverified new user the no-longer-active message above, their response would be some variation of "but I just registered!" (shortly thereafter the user stops attempting to use your crappy amateur-hour application).&lt;br /&gt;&lt;br /&gt;Unfortunately there are lots of bad solutions to this problem and only a few good ones .  Here's a bad solution:&lt;br /&gt;&lt;br /&gt;Bad Solution 1 - another fun boolean field&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s1600-h/new_developer.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 84px; height: 93px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s200/new_developer.png" alt="" id="BLOGGER_PHOTO_ID_5352034474884768450" border="0" /&gt;&lt;/a&gt;"That's just another field, isVerified."  Here's his design.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/SkY_0ENJj1I/AAAAAAAAABw/xUnv8NwAJno/s1600-h/bools_3.png"&gt;&lt;img style="cursor: pointer; width: 200px; height: 111px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/SkY_0ENJj1I/AAAAAAAAABw/xUnv8NwAJno/s200/bools_3.png" alt="" id="BLOGGER_PHOTO_ID_5352035370891054930" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Look at the complexity we've caused.&lt;br /&gt;&lt;br /&gt;Active users can be defined as isActive = TRUE AND isVerified = TRUE, New un-verified users can be defined as isActive = TRUE AND isVerfieid = FALSE, disabled users have the status of isActive = FALSE AND isVerified = FALSE.   This is not maintainable.&lt;br /&gt;&lt;br /&gt;Bad Solution 2 - NULLable boolean field&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s1600-h/new_developer.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 84px; height: 93px;" src="http://2.bp.blogspot.com/__7eX1ilwql8/SkY-_6UmosI/AAAAAAAAABg/pSEcH7wQuoI/s200/new_developer.png" alt="" id="BLOGGER_PHOTO_ID_5352034474884768450" border="0" /&gt;&lt;/a&gt;"Let's set isActive NULL for un-verified users"  I'm not including the table graphic for this design, as I recently ate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Great now our data-model says you're either inactive, active or we have no idea what your status is.  I'd say the isActive column no longer really means "is-active".  This is not maintainable either.&lt;br /&gt;&lt;br /&gt;If you've read this far and are relatively sober, you'll realize that I've been building an argument to support my earlier assertion that in any non-trivial database, boolean columns are not expressive enough to describe the status or type of an entity.  If you're not already convinced of this fact, imagine handling the further requirement via boolean columns:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;We'd like to expire the accounts of users who've not paid within the last thirty-days.  We also need to know the users who've expired so we can notify them.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What we need is a design which corresponds to how we think about the data.  &lt;span style="font-weight: bold;"&gt;Most English speakers do not, for example, describe someone standing as isWalking = FALSE AND isSitting = FALSE, so why should your data-model act that way?&lt;/span&gt;  Consider this simple design:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/__7eX1ilwql8/SkZAZCsXGbI/AAAAAAAAAB4/6F43QbGnp_Q/s1600-h/bools_4.png"&gt;&lt;img style="cursor: pointer; width: 162px; height: 200px;" src="http://4.bp.blogspot.com/__7eX1ilwql8/SkZAZCsXGbI/AAAAAAAAAB4/6F43QbGnp_Q/s200/bools_4.png" alt="" id="BLOGGER_PHOTO_ID_5352036006140254642" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This design handles all the above requirements.  Possible values in UserStatus are:&lt;br /&gt;&lt;br /&gt;1 - Active&lt;br /&gt;2 - Inactive&lt;br /&gt;3 - Unverified&lt;br /&gt;4 - Expired for non-payment&lt;br /&gt;5 - Mocked for bad db design&lt;br /&gt;6 - sent to an Intro. to Databases class&lt;br /&gt;&lt;br /&gt;This design is flexible for if requirements change and we realize we need to account for a new status, just add a row to UserStatus.  If it turns out that its possible for a user to be in more than one UserStatus (I can see a few un-named developers as being in statuses 5 and 6), we only need to add that as a new UserStatus ("7 - Mocked and re-educated" or perhaps "7 - Lucky to be working as a Developer").&lt;br /&gt;&lt;br /&gt;If you keep things simple and model your data in an expressive way you'll be fine.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-7225628565930031001?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/7225628565930031001/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=7225628565930031001' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7225628565930031001'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/7225628565930031001'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2009/06/boolean-columns-open-mic-night-covers.html' title='Boolean columns suck as statuses or types'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/__7eX1ilwql8/SkY-ijvRyiI/AAAAAAAAABY/fJMYxmIOP80/s72-c/bools_1.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-6550377632730523378</id><published>2009-06-21T15:10:00.000-07:00</published><updated>2009-06-21T15:11:24.486-07:00</updated><title type='text'>COMMIT</title><content type='html'>Were you ever been forced to read something in school that, only after a period of time - having forgotten all about it, and on the 3rd glass of wine, you recalled- made sense?  I was forced to read Freud in school, and (if you take a superficial glance at his ideas) he seems easy enough to  dismiss.  The insight I took away from Freud was this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;people are neither motivated nor governed by reason&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Great.  Given the leaking sack of fear, hope, un-fulfillment and the precious things we all carry around, it's easy to understand this insight.  It is in this context that I hope to understand why several of my team-mates are not using transactions when performing multiple INSERTs, UPDATEs and DELETEs. &lt;br /&gt;&lt;br /&gt;The utility of transactions is easy enough to prove, do a set of INSERTs, say, followed by an UPDATE referencing a column that doesn't exist.  You've now fucked your database.  I've actually run this experiment for junior developers at a few jobs; the response is always a variation of "it (meaning the DBMS) doesn't handle that for you?".&lt;br /&gt;&lt;br /&gt;What's interesting is that, even after proving multiple SQL statements that modify data ought to be wrapped in a transaction, a few of my team-mates continue to piss in their code, firing off batches of these commands without a single transaction.  Why?  I've no answer.  I've honestly never called on these folks socially and am not able to confirm the messy rooms and dirty dishes one might expect to find in the dwellings of people unable to COMMIT or ROLLBACK.&lt;br /&gt;&lt;br /&gt;Please understand, I like the fact that these people are running around, it makes the world more interesting and they've something to offer.  I just don't want these people on my team if they're allowed to INSERT, UPDATE, or DELETE. &lt;br /&gt;&lt;br /&gt;In Protagoras (something else I was forced to read) Socrates asks Protagoras if he agrees with the rest of the world which thinks that one can have knowledge:&lt;br /&gt;&lt;blockquote&gt;and yet that the knowledge which is in him may be overmastered by anger, or pleasure, or pain, or love, or perhaps by fear,-just as if knowledge were a slave, and might be dragged about anyhow.&lt;/blockquote&gt;&lt;br /&gt;I think I agree with the rest of the world too.  If you have doubts, I've got some source-code to show you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-6550377632730523378?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/6550377632730523378/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=6550377632730523378' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6550377632730523378'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6550377632730523378'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2009/06/commit.html' title='COMMIT'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3247085757012917195.post-6003927312227005938</id><published>2009-04-04T11:53:00.015-07:00</published><updated>2010-11-27T09:44:22.641-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SWC'/><category scheme='http://www.blogger.com/atom/ns#' term='Flex'/><title type='text'>How to make a SWC into a (much smaller) RSL</title><content type='html'>This of course only works if your SWC isn't already compressed.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Extract library.swf from the SWC&lt;/span&gt;&lt;br /&gt;0. Let's refer to our SWC as {SWC} and its location on the file-system as {PATH}&lt;br /&gt;1. Open {SWC} in your favorite unzip utility (7-zip works great and is free)&lt;br /&gt;2. Extract library.swf to {PATH}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Run the Optimizer Tool against library.swf&lt;/span&gt;&lt;br /&gt;3. Within /bin in your Flex SDK directory, you'll see optimized.exe, run that and point it at {PATH}:&lt;br /&gt;&lt;br /&gt;Example on my machine: {FLEX_SDK} = location of Flex SDK&lt;br /&gt;&lt;tt&gt;{FLEX_SDK}\bin\optimizer -keep-as3-metadata="Bindable,Managed,ChangeEvent,NonCommittingChangeEvent,Transient"&lt;br /&gt;-input {PATH}\library.swf -output {PATH}\output.swf&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;(the output SWF is quite a bit smaller now)&lt;br /&gt;&lt;br /&gt;4. Delete DIR/library.swf and rename output.swf to library.swf&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Run the Digest Tool&lt;/span&gt;&lt;br /&gt;5. Since our SWF file changed, we need to update the digest info in our SWC&lt;br /&gt;&lt;br /&gt;Example on my machine {FLEX_SDK} = location of Flex SDK&lt;br /&gt;&lt;tt&gt;{FLEX_SDK}\bin\digest --digest.rsl-file {PATH}\library.swf --digest.swc-path  {PATH}\{SWC}&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;6. Modify your FlexBuilder projects and mxmlc ANT tasks to point to {SWC} as an RSL with a URL of library.swf&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3247085757012917195-6003927312227005938?l=flexamusements.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://flexamusements.blogspot.com/feeds/6003927312227005938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3247085757012917195&amp;postID=6003927312227005938' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6003927312227005938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3247085757012917195/posts/default/6003927312227005938'/><link rel='alternate' type='text/html' href='http://flexamusements.blogspot.com/2009/04/how-to-make-swc-into-much-smaller-rsl.html' title='How to make a SWC into a (much smaller) RSL'/><author><name>Max</name><uri>http://www.blogger.com/profile/07893255941326636217</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://2.bp.blogspot.com/__7eX1ilwql8/TPSbzT6uT5I/AAAAAAAAAEk/-DLmZbL6gak/S220/IMG_0931.JPG'/></author><thr:total>5</thr:total></entry></feed>
