Friday, October 23, 2009

Swiz and the ESRI API, we meet again.

So, I spent quite a bit of time this week really trying to wrap my head around using the Swiz framework with the ESRI Flex API. I found the actual Swiz website, which somehow never came up in all my earlier research. It gave me some details on things I wasn't clear on before.

I also found this video by Joe Rinehart, Swiz in 20 minutes.
Videos like this are great, because it just helps to hear and see someone go through it from start to finish. With my new found information, I was ready to tackle a new version of the application I posted earlier. What I have found, is the Swiz framework really gives you a lot of freedom in how you actually want to write your code. How I use it in my style, may not work for someone else's style. I really like that. You can also get a good explanation of the framework and metadata here.

So, rewrote my previous example, to incorporate a tighter distinction between views and controllers. Found here (source available).
I now make liberal use of the Autowire metadata, which links all my functions and variables through my beans. I'm also making good use of the Mediate tag, that will capture events.

For example, in my Controller, I have this.
public static const QUERY_LAYER : String = "queryLayer";

Then, from any other view or controller, I can use this.
Swiz.dispatchEvent( new Event( LayerController.QUERY_LAYER ) );

In my Controller, I have this.
[Mediate( event="queryLayer" )]
public function queryStateLayer() : void {
trace( "begin queryStateLayer" );
Swiz.dispatchEvent( new Event( LayerController.LOAD_STARTED ) );
var query:Query = new Query();
query.returnGeometry = false;
query.outFields = [ "STATE_NAME", "AREA" ]
query.where = "STATE_NAME <> ''";
AsyncToken( stateDelegate.execute( query ).addResponder( new Responder( state_results, state_fault ) ) );
}

Using the Mediate metadata, telling it to watch for the "queryLayer" event, it will fire this function whenever that occurs.

As can be seen above, I am using the AsynToken function to call a delegate that will execute my query task. You don't need to use a delegate. You could execute your task directly in the controller. I like to keep my delegates separate only because it makes sense to me, as my first framework was Cairngorm and that was pretty standard. I also think it keeps the controller a little cleaner.

My biggest hurdles were trying to interpret many of the Swiz tutorials to fit into an application with an ESRI Map control, where you want interaction between the map, the layers and your controls. Swiz now supports adding Views to your beans, although from what I have read, you may want to do so only in special circumstances. But that could be another option, is to add the Map into your beans and open it up to all controllers and functions in your application.

One big hurdle that I have not been able to figure out is Unit Testing with the Swiz framework. I have tried with Fluint and Flex Unit to no avail, although I have heard that the Autowire and separation of controllers should really make this a simple task, especially in Fluint. When I try in Fluint, I load my beans in override setUp. I Autowire my controller and models as I think I should. I can dispatch an event to my controller and my trace show that my app runs just as it should, but when I check an Autowired value such as an ArrayCollection, it's null, even though it should not be. It's driving me nuts. I'm having the same issue with Flex Unit, my application traces show everything runs, but at the end, my Autowire values are still null.

If someone could look at my example and maybe write a quick Fluint or Flex Unit test for say the StateListCollection, I would be eternally grateful as it would really just push me in the right direction. I was hoping to incorporate unit testing from the very beginning of all my future applications, but this is really just killing me now.




Sunday, October 18, 2009

Mapping apps with Flex, Swiz and Fluint

I have been developing with Flex for about a year now and so far it has been a really fun experience. I am by no means a trained developer, I just happened to have taken some some C++ and VB6 courses in school and fell into it.

That being said, I had very little clue how to use a framework when developing, much less how (or why for that matter) to do unit testing. I saw a great presentation at the ESRI 2009 Developer Summit last year on Flex Best Practices. It was in that session that I learned how much I had been doing wrong. I guess, I shouldn't say wrong, after all, the application I was working on worked. Problem was, when someone wanted to add a new feature or change the way something worked, I found myself spending a lot of time trying to fix things I had broken by doing so.

That's when I first started using Cairngorm. This site has some great articles and videos on Cairngorm. That's where I learned how to use it. Cairngorm seemed like a great way to organize my code and in the end it just made sense. It can be a little clunky and as I have read elsewhere a bit heavy on boiler plate code. You launch an event that's tied to a command that executes a delegate and then process the results updating your model. Makes perfect sense to me. Then I discovered unit testing. It was discussed in that presentation I mentioned, but I didn't think it really applied to me. After all, I was the only person developing this application and why would I need unit testing, Cairngorm kept everything organized for me. That is until my model got a bit large, my commands dispatched more events to more commands, I was using timers to keep consecutive asynchronous tasks to the same functions from overlapping and I found a change here would break something somewhere else and would go unnoticed until I tried to show someone something cool I had done only to find something else didn't work (yeah, that looks really cool).

So, like any Flex developer would do (I actually have no idea what a real Flex developer would do) I tried out FlexUnit. FlexUnit worked and looked good too. However, have you ever tried to do unit testing with Cairngorm and FlexUnit? Do a web search on that particular subject. It took me a few tries, but I got something working. I realized at this point why Test Driven Driven development has such a good reputation. It works. I've decided to approach all my future projects this way.

But I thought this post was titled "Mapping apps with Flex, Swiz and Fluint", why am I rambling on about Cairngorm and FlexUnit? This is my first blog post, give me a break. Did I mention that the only Flex development I have done is mapping applications with ESRI's Flex API? Now, I know I'm not alone in having my first Flex experience be with the ESRI Flex API. It's fairly new and it's a niche market. We are a niche group. Now out of that group of ESRI Flex API developers (I use the term developer loosely for myself) also use a Framework of some sort? Okay now, I know this list might be short, but out of that group, how many are doing or attempted test driven development?

You can see where I might have been frustrated trying to research that particular subject. When dealing with a mapping application, there is a lot going on. You have a map that you might want multiple functions to access and adjust an extent for, you have multiple map services that you want exposed to other functions so you can turn off layers or adjust transparencies, not mention use query tasks or identify (I'm sure it's just as bad for you real Flex developers if not worse). A framework really lets you get a good grasp on these things. Utilizing testing of some sort is just a cherry on top. Because of some frustrations I had with Cairngorm, I decided to research some more frameworks that I thought lent themselves quite well to mapping applications specifically.

That's when I turned to Swiz. I really did not like Swiz when I looked at it. I mean, come on, Beans? I thought that was a Java thing? But I decided to buckle down and really take a crack at it. So I took one of the ESRI Samples from their website and adjusted it to work in the Swiz framework with some minor tweaks to display a grid of state names. That took me a full Saturday. Being the overambitious glutton for punishment that I am, I decided to try to do some unit testing with Fluint. But why not Flex Unit you might ask? Fluint was touted at the 2009 Developer Summit I went to as being a little more robust. Plus, I figured, I wanted to learn something new. Fluint has some nice features, and I think the most powerful is the ability to add steps to your test and then run it.

So, after spending all weekend, reading multiple tutorials, multiple blogs and forum posts, I got a working, testable (and passing thank goodness) application put together. I figured, someone out there, might be in my situation. Using the ESRI Flex API, trying to wrap their heads around a framework and maybe, just maybe trying to do some unit testing. I did not find a specific example like that anywhere. If you got one, please share it.

Remember, in my example (view source available), I am probably doing a lot wrong. I have my map in my Beans (that sounds hilarious) with a controller, a delegate and a component to control my list of states. I'm kind of thinking that in the way I'm using it, my Beans replace my model. It exposes what I want to everything else in my application. Originally, my map was just part of a single component with a datagrid. I parted it out, so that I could add a function to zoom to a state on a datagrid double click. I need my map accessible outside that page. I probably make some wrong assumptions here, but I haven't tried that part yet.

If you are curious on what sites I used as references, here is a list.
Brian Kotek had an awesome 5-part series on Swiz. There is supposed to be more coming, so I'm sure I'll change how I use it as I learn more of the stuff he didn't post yet. Not to knock the Swiz Getting Started page, but I had a tough time really grasping it with their walk-through.

This insideria article gave me a good start at using Fluint. The Fluint Getting Started pages were a big help as well.

And if you bothered reading all this, I assume you are interested in Mapping applications using Flex with a framework while taking advantage of unit tests. I hope I have helped in your unholy quest.
Sorry, if this wasn't "tutorial" or more specific. If someone has questions (all one of you), I'll try to answer them the best I can. The couple of guys linked above know more than I do about Swiz and Fluint, but I have a newbie perspective on it when it comes to the mapping stuff if you need it. I'll probably still use Cairngorm for stuff already done that way, so might post an example up with that later on. I'm hoping to tackle Mate next weekend, so we'll see how that one goes.