Swiz and MockAsyncToken

I've been taking a look at the applicability of Chris Scott's Swiz Framework for use here at Broadchoice. So far, it's exactly what I'd expect - a fairly noninvasive way of wiring dependencies inside a Flex / AIR application that allows use of inversion-of-control.

One thing that threw me, though, was that Swiz approaches business delegates (wrappers for business services like RemoteObjects) from a total RPC standpoint. Instead of passing an IResponder to a new delegate instance each time you wish to use it like most Flex frameworks encourage, you simply write a delegate, instantiate it once in your BeanLoader, and use it as a singleton. Each of its methods that make server-side calls just return AsyncToken instances.

I really like this, as it makes the difference between a simple RemoteObject and a delegate wrapper completely irrelevant - your controller doesn't have a clue if it's talking to a delegate or not.

It did, however, throw me off from something I typically do. I normally write a "mock" delegate for any business service that "pretends" the server is there. Its useful in both unit tests and day to day coding: server may be down, backend code not written, etc.

In order to get this to work with Swiz, however, I had to simulate the presense of an AsyncToken and its related RPC events. Hence, I've cobbled together a MockAsyncToken and MockResultEvent that let you quickly set what you'd like a mock delegate to return:



// Mock'd RPC function
public function getUser() : AsyncToken
{
// just pass the desired server response to the MockAsyncToken's constructor
return new MockAsyncToken(new User());
}

...bam. Instant mock delegate. Thanks to Swiz, the rest of the app doesn't care that it's even there, and if I can use a plain RemoteObject, I never even have to write the "real" delegate.

Code for MockAsyncToken and MockResultEvent (dependency) is attached. Use at your own peril.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Roland Ringgenberg's Gravatar Perfect!
This is exactly what I was looking for, thx!
# Posted By Roland Ringgenberg | 10/10/08 5:07 AM
Gareth Arch's Gravatar So, 4 months on, and still liking the Swiz framework?

I only ask as my current company is thinking about Flex frameworks, and, on a very brief glance at Swiz, it seems to cut down on the repetitive code issues that occur in Cairngorm, but still make it simple enough for others to step in and get up to speed quickly.
# Posted By Gareth Arch | 12/30/08 1:17 AM
Brian Kotek's Gravatar Yes, still loving it, in fact, loving it more! They keep adding really cool features like Dynamic Mediators, testing utilities, and now they're talking about auto-generation of mediators from normal Flex Event metadata. Which would mean that you can dispatch events using Flex's normal dispatchEvent() method instead of needing to use the CentralDispatcher!

I'm writing a personal series on Swiz over at my blog (briankotek.com/blog) if you care to follow along. It just got underway.
# Posted By Brian Kotek | 1/9/09 1:54 PM
Gareth Arch's Gravatar Wow! I've been playing with the asyncToken calls, autowiring everything up, and this stuff is fantastic! :)

I've never really played with the IoC or DI stuff before, so it took me a couple of days to wrap my head around the examples ( I followed along at insideRIA first http://www.insideria.com/2008/12/frameworkquest-20... ), but everything seems to be starting to click now. For the small fact alone that I don't have to add eventListeners for anything (unless I really want to) from my service calls, delegates, etc. is just an amazing timesaver. My company's wanting to move more towards unit testing (we've done it for CF so far), and I'm guessing they'll be wanting to use FlexUnit or Fluint at some point, so I'll be interested to see what's involved with that integration.

I'll be following along on your blog posts just to pick up on some tips for anything I'm missing out on, but I'm actually looking forward to playing with this framework a bit more now.
# Posted By Gareth Arch | 1/19/09 4:16 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002. Contact Blog Owner