Learning Flex Part 2: Why Service Layers Rule
In my first entry on the process of learning Flex, I mentioned that my primary learning device is my Bookstore sample application. I used this application as the basis for my talk at the ColdFusion Frameworks conference earlier this year. In that talk, I showed how building a framework-neutral model layer makes your programming life a lot easier. To illustrate why, I used a single Model, running ColdSpring and Reactor, to interact with a Fusebox 5, a Model-Glue:Unity, and a Mach-II application. The key to making this work is a well defined service layer, because it acts as a neutral API to your model that anything can use.
Well, the benefits of a good service layer were hammered home again when I started work on creating a Flex version of my Bookstore. As a result of having a neutral API into my model, I was able to reuse the same model again to feed data to my Flex application with only minor additions:
- Creation of some simple Value Objects which can be automatically mapped to ActionScript classes on the Flex side. This was about 10 minutes of work.
- Creation of a ColdSpring AOP advice that will translate queries into arrays of Value Objects. This was about 30 minutes of work.
Yes, as you might have anticipated, this is where I praise ColdSpring yet again. Not only will it automatically generate remote proxy CFCs for my services, but with a little AOP magic it will translate things into the format that works best with Flex. I could have let it just send raw queries out to the Flex UI, but I don't like arrays of untyped objects coming into the Flex app. I much prefer to have actual ActionScript classes that I can use as I see fit. And, yes, it was for the learning experience too. I'll probably create a separate entry to talk about (and ask for advice on) when it's best to just send a query to Flex vs. when to send an array of Value Objects.
In any event, this brings me back to the point of the article: Service layers rule. Maybe it's just me (and quite possibly it is), but there was just something really cool about being able to slap a Flex application on top of my existing model and have the whole thing JUST WORK with just a few minutes of additional coding.



