Funk IoC – A New Dependency Injection Framework

Thursday, April 8th, 2010

Twitter can be a funny beast, what makes it great can also make it poor. I use Twhirl which keeps me updated any time one of the people I follow tweets about something, the only problem is that so many people tweet that if I dont happen to see it within about and hour or so of the Tweet, ill miss it. This time however I was lucky enough to catch a tweet by @Joa about his new Inversion of Control and functional-programming-like library, Funk AS3.

As I have been getting well into RobotLegs (a Dependency Injection MVCS framework) recently I was extremely interested to hear about this new project by Joa who I respect very much as a brilliant coder not least because of his excellent work on low-level Flash byte-code optimisation (see Apparat).

Joa has taken a different approach to doing dependency injection. The approach most frequently used (and the one used in SwiftSuspenders / RobotLegs) is to use meta-data to declare to a number of variables for injection. You then map a class to be injected and instantiate it using the injector.

As an example, with Swift Suspenders you would define a class for injection with something like the following:

  1. class MyInjectedClass
  2. {
  3. public function sayHello(toSay:String)
  4. {
  5. trace("Hello "+toStay);
  6. }
  7. }
  8.  
  9. var injector : Injector = new Injector();
  10. injector.mapSingleton(MyInjectedClass);

Here we are telling the Injector to make a single instance of our class and hold it internally ready for when it is next requested, such as:

  1. class MyDependantClass
  2. {
  3. [Inject] public var myClass : MyInjectedClass;
  4.  
  5. public function performAction()
  6. {
  7. myClass.sayHello("World");
  8. }
  9. }

Here the [Inject] meta-data indicates that we want the framework to supply the class with an instance of type MyInjectedClass. The final part is to make an instance of the dendant class and inject into it:

  1. var dependant : MyDependantClass = new MyDependantClass();
  2. injector.injectInto(dependant);
  3. dependant.performAction();

As you can see this is a nice way of handling inter-module dependencies in your code, when coupled with a MVC framework such as RobotLegs it becomes and extremely powerful yet elegant way of coding.

It however isnt perfect and Joa, on his google code page mentions three drawbacks of this method:

  • Your injected properties are publicly exposed and mutable.
  • describeType is very expensive.
  • Steep learning curve.

This is where he suggests his alternative method, which is quite ingenious. Using the same example as above you would see something like the following:

  1. class MyInjectedClass
  2. {
  3. public function sayHello(toSay:String)
  4. {
  5. trace("Hello "+toStay);
  6. }
  7. }
  8.  
  9. bind(MyInjectedClass).asSingleton();

Then the dependant class would look like the following:

  1. class MyInjectedClass
  2. {
  3. protected var myClass : MyInjectedClass = inject(MyInjectedClass);
  4.  
  5. public function performAction()
  6. {
  7. myClass.sayHello("World");
  8. }
  9. }

And making an instance of it could be as simple as:

  1. var dependant : MyDependantClass = new MyDependantClass();
  2. dependant.performAction();

As can be seen there are some benefits to this method, the biggest one in my opinion is that injected properties dont have to be public as they are provided by the call from within the class scope rather than from outside.

So how does Joa perform this magic? By abusing a little used ability of the Actionscript programming language known as package-level-functions. These are throwbacks from the old AS1 & AS2 days of global functions. There are actually a couple of common examples in AS3 still such as getTimer() and getQualifiedClassName() still used. What Joa has done is to use these package level functions as a method of generating concise looking code reminiscent of functional programming.

Performance wise, im not entirely sure whether by using package-level functions instead of describeType() calls used in meta-data driven IoC frameworks is any faster as Till Schneidereit of Swift Suspenders suggests:

I don’t think that Funk’s approach is any faster than an optimized
metadata-based IoC container: describeType may be slow (as in “takes a
few dozen microseconds to run”), but is only ever called once for each
class an instance of which is injected into. After that, it’s just a
straight iteration over an array for all injection points instead of
Funk’s multiple method calls for each injection point.

So the next step for me is to run some tests to see how things pan out. Either way im very impressed with both approaches and cant wait to see what kind of exciting advances will be developed in the coming months.

Tags: , , , , , , , , ,

Trance Around The World, Show Downloader

Friday, March 5th, 2010

Had this little idea a while back, thought I would spend an hour tonight and bash it out.

I love the Trance Around The World radio show by Above and Beyond, downloading  it for listening later is very annoying however and involves some fiddling with browser source and other things, so to make life easier I built this little tool. What it does is grab the HTML from the above and beyond page then parse it for the MP3 file, then it uses the FileReference class in flash to download it to your HD.

Anyways, enough jibber jabber. Source is enabled:

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.

Hope you enjoy!

Tags: , , , , , , , ,

AudioBook Organiser v1.3.0 – Drag'n'Drop

Tuesday, January 12th, 2010

Well I was just doing some audio book organising and realised that it would be great if I could drag and drop a folder straight from my AIR into iTunes ready for upload to my iPhone.

Anyways after a little searching through the docs I came up with this little ditty:

var cp : Clipboard = new Clipboard();
cp.setData(ClipboardFormats.FILE_LIST_FORMAT, [new File(book.url)], false);
NativeDragManager.doDrag(null,cp);
Which gets fired by my DataGrid in the view:
<mx:DataGrid width="100%" height="100%" dataProvider="{books}" editable="true"
itemEditEnd="{dispatchEvent(new BooksEvent(BooksEvent.PROPERTY_CHANGED));}"
dragEnabled="true"
dragStart="{dispatchEvent(new BooksEvent(BooksEvent.BOOK_BEGIN_DRAG, AudioBookModel(event.currentTarget.selectedItem)))}">
Its pretty cool.
Anyways, the latest version and the source is below:

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.

Source: http://www.mikecann.co.uk/flash/AudioBookOrganiser/AudioBookOrganiser_v130_source.zip

Tags: , , , , , , ,

Quasimondo's Galactose

Sunday, January 10th, 2010

ScreenHunter_02 Jan. 10 20.13

I have immense respect for some of the flash developers out there and Quasimondo is one of them. He has just released a little particle related experiment.

Whenever I see these kind of things it inspires me to bash out my own little experiments.

I wont talk too much about it but check it out on this lab page: http://incubator.quasimondo.com/flash/galactose.php

Tags: , , , , ,

Audio Book Organiser v1.2.0

Saturday, January 9th, 2010

ScreenHunter_01 Jan. 09 18.34

Just did a quick update to the audio book organiser. Added the ability to move the storage database file. This was so that I can put my storage file on Dropbox and it will then be backedup and synced between machines.

New version and sources:

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.

Source: http://www.mikecann.co.uk/flash/AudioBookOrganiser/AudioBookOrganiser_v120_source.zip

Tags: , , , , , , , ,

Audio Book Organiser (AIR, Mate, Flex 4)

Wednesday, December 30th, 2009

Well its been a fun Christmas, I have eaten and drunk to the point that im going to be running it off in the gym till next christmas.

Although there has been merryment abound, the keyboard couldn’t keep me away. Its probably okay to say this now as im not under any secrecy act; I have decided to leave Massively Multimedia in Manchester to join a new startup called Ideas Pad in Wilmslow (just south of Manchester).

I cant say too much about exactly what I will be working on just yet but I can say that it will involve my experience in the Flash world. Specifically, I will be working in Adobe Flex again on some fairly sizeable projects. For this reason I wanted to brush up on my Flex as there has been a new release of the IDE (now named Flash Builder) and the SDK.

In addition I wanted to look at the various frameworks in use for Flex these days. I have used the usual Cairngorm, PureMVC before however I stumbled accross a new one called Mate (pronounced mah-tay, like the drink). Mate looked very interesting to me so before I jumped two feet in and used it in a commercial project I wanted to give it a spin in a simple project first.

Finally we get to the point of this post. I have developed a simple Adobe AIR application that allows you to organise audio books. The basic idea is simple you give the application a selection of ‘source’ directories where your audiobooks belong then you can tick off whether you have listened to each one, and what rating you would give them.

ScreenHunter_02 Dec. 30 12.32ScreenHunter_03 Dec. 30 12.32

The data is persisted to a file that is saved to your hard drive, so when you open the application up again next time it remembers which audio books you have listened to and what ratings you gave them.

I havent tested it very much atall so there is a very high likelyhood of being some strange bugs in there. I am also releasing all the source code for this project for all to see, use and study if they so wish.

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.

Source: http://www.mikecann.co.uk/flash/AudioBookOrganiser/AudioBookOrganiser_v101_source.zip

Tags: , , , , , , , ,