Chapter 2. The Robotlegs dream...

Some great frameworks exist, but they’re intimidating

When Robotlegs came into existence, there was no lack of AS3 frameworks to choose from. PureMVC and Cairngorm had forged the way. Parsley, Swiz, Mate—they were all getting plenty of attention. So why bother creating yet another AS3 framework? And what would Robotlegs offer that other frameworks didn’t already have covered?

Well, that’s simple—Shaun Smith was intrigued by the idea of a framework that wouldn’t give him a headache—a framework that did less, and thus required you to learn less, change your programming behavior less and left fewer opportunities for the only metric that we think matters: WTFs per minute.

80% of the problems can be solved with 20% of the API

(and 90% less cognitive load)

Developers often get excited about the Robotlegs filesize footprint—adding less than 20k to your published swf. We’re much more excited about the Robotlegs cognitive footprint—how little there is to learn to get up and running with Robotlegs.

Robotlegs was always intended to be a pareto[1] solution: it’s the 20% of functionality that solves 80% of your programming problems. The YAGNIator (YAGNI = you aren’t gonna need it!) was applied ruthlessly. This means that you can carry it in your brain’s pocket: complete use of the core Robotlegs framework only requires you to understand how to use eight classes. Yes, eight. That’s all. And these aren’t monolithic enormous classes. Most Robotlegs apps require you to make use of less than twenty methods within the framework. And Robotlegs incorporates only two custom metadata tags and, in practice, most applications only ever require the use of the [Inject] tag. More on those metadata tags later.

We’ll cover the Robotlegs API in depth but quite slowly through the following chapters. We want to help you understand why you would choose to use a particular combination of Robotlegs classes and API to solve a problem, and what the alternative approaches might be. But in case you’re craving a concrete answer to the “What is Robotlegs?” question, here’s a snapshot of the most frequently used Robotlegs code in action:

Table 2-1. A taster of the most frequently used parts of the Robotlegs API
ClassMost used API functions
Context
new VideoLibraryContext(rootView, true);
startup();
Command
execute();
dispatch(event);
CommandMap
mapEvent(ConfigEvent.LOAD_COMPLETE, ApplyUserSettingsCommand, ConfigEvent);
MediatorMap
mapView(DocumentTabMenu, DocumentTabMenuMediator);
Mediator
onRegister();
addViewListener(TabEvent.TAB_SELECTED, dispatchDocumentFocusChange);
addContextListener(QuitEvent.QUIT_REQUESTED, hilightUnsavedDocumentTabs);
dispatch(event);
EventMap
mapListener(view.yesBtn, MouseEvent.CLICK, dispatchYes, MouseEvent);
Actor
dispatch(event);
Injector
mapValue(IUserConfig, loadedUserConfigVO);
mapSingleton(UserXMLLoadingService);
mapSingletonOf(IUserLoadingService, UserXMLLoadingService);
mapClass(IPermissionRules, StrictPermissionRules);
instantiate(DatabaseKey);
getInstance(ILoggingService);

Coding for Robotlegs shouldn’t tie you to the framework

Your classes can play outside Robotlegs too!

One of the potential drawbacks of using a framework is the difficulty of reusing your classes outside of that framework. Robotlegs is, by design, minimally intrusive—for example, if you create a model or service class that is used within a Robotlegs application, you should be able to re-use that class in a non-Robotlegs application by simply providing it with one ActionScript native property: an instance of IEventDispatcher.

Robotlegs commands only require you to provide an execute() method. There are no special messaging objects, just your own custom AS3 Events (or Signals if you prefer them and use the AS3Signals extension packages).

In addition, the metadata-based Automated Dependency Injection (not nearly as fancy as it sounds, we’ll de-mystify that in Chapter 4), that wires up Robotlegs applications, supports normal coding practices. Outside of Robotlegs you’ll have to wire your objects together manually, but we set a very low hurdle to jump when reusing sections of your code in projects where you’re integrating with another framework, or decide to go framework-free for optimization purposes.

Robotlegs aims to enable and not dictate

From the outset, Robotlegs was intended to be easy to customize. To facilitate this, instead of being a single unit, Robotlegs is separated into three layers:

The Injector

Creates objects (using Automated Dependency Injection).

The Robotlegs Core Architecture

Provides control flow and communication between the different tiers of your application (business logic and user interface for example).

Top Level Architecture

Supports the individual tiers of your application. The out-of-the-box version supports MVCS.

Robotlegs is a three-layer cake
Figure 2-1. Robotlegs is a three-layer cake

The middle layer of this three-layer cake is the part that is the essence of Robotlegs. Initially the assumption was that many users would want to swap out the top and bottom layers to suit themselves.

As a result, there are very few things that Robotlegs won’t ‘let’ you do. It’s possible to implement a whole range of different patterns and architectures through Robotlegs, but we’ve actually found that newcomers tend to prefer a strong prescription. We often answer “Can I do X?” questions on the forum with “Yes, you can, but really you’d probably rather do Y”. People seem to appreciate the clarity of direction.

So, while you could apply many different top-layer architectures within Robotlegs, the vast majority of users are content with the out-of-the-box architecture—what we like to think of as the standard issue trousers for Robotlegs. These trousers are an implementation of the architectural pattern most frequently applied to ActionScript applications: MVCS (Model, View, Controller, Service).

Most AS3 applications benefit from the MVCS approach

Model, View, Controller, Service is a pattern, which separates your application’s responsibilities in a way that fits a wide range of applications.

Model

Holds and manipulates the application’s state.

View

Relates to stuff you can see. And—confusingly—stuff you can hear too.

Controller

Deals with application logic—translating user actions into application state changes and so on.

Service

Links the application to external players—data services, external APIs, the user’s file system etc.

Testing, testing! (We test, and we make it easy for you to test)

The team behind Robotlegs is test crazy, and this has a big influence on the way Robotlegs is put together. No statics, no singletons, no reliance on display-list event bubbling. In creating Robotlegs, every single decision was subjected to the “How do I test a class that uses this?” filter.

Robotlegs makes it easy to unit test your individual classes, but it also makes it easy to integration-test features, and to end-to-end test user stories in your application.

If you’re already a test nut yourself, you’ll appreciate the reduction in setup that nicely decoupled cooperation and communication gives you. If you’ve dabbled in testing but found yourself smacking into walls relating to difficulties testing your classes in isolation, you should find that working with Robotlegs removes those problems completely. Again, the consistent approach that a strong framework gives you means that, once you’ve learned the ropes, you only have to apply real brain power to what’s unique and interesting about each part of the code when you test it.

If you’re not sold on testing, or you’ve never given it a try, we strongly recommend that you experiment with incorporating it into your workflow. Test-first development is hard—it’s hard because it forward-shifts your confusion. You can’t fiddle with your code while you ignore the fact that you haven’t really figured out what this class is meant to do, or how it’s going to fit in with the other classes in your application. We’ve found that testing is like a really (horribly) honest friend. It frequently tells you things you really didn’t want to hear right now. It’s up to you whether you listen!

Some final things every Robotlegs cadet should know

None of your normal AS3 OO solutions stop being relevant just because you’re using Robotlegs. Well, almost none—there are two significant exceptions:

  • Static Singletons. No more static public function getInstance()

  • Event bubbling through the display-list (you can still bubble mouse events within composite views, but you won’t need to rely on bubbling for wiring your application together)

As your codebase becomes more flexible, you’ll start to notice higher-order problems that previously never bothered you because they were obscured by more immediate difficulties. This happens to all of us. It is a great problem to have—so try to enjoy pushing through it, shifting your architectural skills to the next level, and remember to:

  • Favor composition over inheritance—composing the functionality of a class out of smaller classes is more flexible than imposing long inheritance chains, and Robotlegs makes composition very easy to do

  • Make use of factories and helpers to keep each class adhering to the Single Responsibility Principle—every class should have one job and do it well

Robotlegs is not a Golden Hammer. Not every problem in your codebase is a nail. Specifically, Robotlegs should stay out of your views. There is an understandable temptation to use Robotlegs and Automated Dependency Injection everywhere—resist! Resist!



[1] Pareto Analysis is a decision making technique which works on the principle that 80% of a set of problems can be attributed to 20% of problem-causing factors. The Pareto principle also says that you can generate 80% of the benefit by doing the first 20% of the work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.192.3