Inversion of Control on PocketPC

Something one should really strive for (in my opinion) is writing code in the spirit of the SOLID principles . One of the principles talks about DI( Dependency Injection) and even if it doesn’t refer strictly an Inversion of Control container – this is the easiest example of DI.

A simple example of DI would be an `OrderProcessor` class that needs to calculate on an order the tax that should be applied. That calculation would best be accomplished by a separate class, and because there might be more than one way to calculate taxes (different states from where the order is placed for example) `OrderProcessor` shouldn’t depend on a specific implementation of the tax calculator.

So an interface appears and instead of :

   1: class OrderProcessor{
   2:     ITaxCalculator calculator1 = new SomeStateTaxCalculator();
   3:     ITaxCalculator calculator2 = new SomeOtherTaxCalculator();
   4:     
   5:     // do some logic and decide which calculator to use ..
   6: }

you would have :

   1: class OrderProcessor{
   2:     ITaxCalculator _calculator;
   3:     
   4:     public OrderProcessor(ITaxCalculator calculator){
   5:         _calculator = calculator;
   6:     }
   7:  
   8:     // now you have the calculator you need
   9:     // the responsibility of which calculator to use is no longer in this class.
  10: }

To the PocketPC

And now after a short introduction to what Inversion of Control is about, I was surprised to find out that on the .Net Compact Framework is quite hard to find an IoC library.

Luckily, Germán Schuager, has put in the effort of creating a small version of an IoC container. That can be found at : http://code.google.com/p/compactcontainer/ . You’ll also find on that page a few links to short example of how to use the library.

For personal use I just made a small class to register types needed at startup, and be able to resolve dependencies at runtime.

   1: static class IoC
   2:     {
   3:         private static CompactContainer _container;
   4:         private static bool _configured;
   5:         public static void Configure()
   6:         {
   7:             _container = new CompactContainer { DefaultLifestyle = LifestyleType.Transient };
   8:  
   9:             _container.AddComponent(typeof(ISettingsManager), typeof(SettingsManager), LifestyleType.Singleton);
  10:             _container.AddComponent(typeof(IRatesDownloader), typeof(RatesDownloader));
  11:             _container.AddComponent(typeof(IRatesParser), typeof(RatesParser));
  12:             _container.AddComponent(typeof(IWebUpdater), typeof(WebUpdater));
  13:             _container.AddComponent(typeof(IContextPersister), typeof(ContextPersister));
  14:  
  15:             _configured = true;
  16:         }
  17:  
  18:         public static T Resolve<T>()
  19:         {
  20:             if (!_configured)
  21:                 throw new Exception("The IoC container was not configured before use. Call Configure() on the IoC class before the Application.Run");
  22:             return _container.Resolve<T>();
  23:         }
  24:     }

Tags: , ,

Currency Calculator for PocketPC

This summer I'm taking a vacation to Croatia and I wanted a simple PocketPC app that will convert between the Euro, the Croatian kuna , and my national currency the Romanian Leu.
Obviously I first searched for such an app and I did found one - unfortunately id had everything I wanted BUT the Croatian kuna :). So , without too much thinking I decided to write the app myself (small syndrome of not invented here :) ). So, I set about creating my own application.

 The most important part(at least from a programmers point of view) , and the one that I really wanted to get right was the CurrencyCalculator that would convert from a currency to another currency and the Money class that would represent a specific Currency.
I'm not exactly proficient with TestDrivenDevelopment and thought this would make a grate candidate for TDD. You'll find 2 (yes two :) ) tests in the CurrencyCalculator.Tests project. 

These are the only tests in the application for the moment , I'll write a few more for some other stuff (like downloading an xml with exchange rates ) but the rest is pretty much UI stuff which I'm not gonna test.

Another interesting part of the project is the Dependency Injection part - where I only wanted to be able to do some constructor injection and be able to specify LifeStyle (Transient or Singleton) for different classes . It's surprising how little choice you have for the mobile platform, even for simple stuff.

But I'll talk more about this in another post.

The project is Open Source, and you can grab the code from google code : Currency Calculator for PocketPC.

Tags: , ,