Apollo is a lightweight Actionscript and Flex framework for dependency injection and message passing. Itt is an ideal basis for applications built using model-view-controller, or more specifically model-view-adapter patterns. The approach of this framework is slightly different from most other well know frameworks, such as Robotlegs or pureMVC. The aim is to give developers the best tool to focus on the fun part of development and less on writing boilerplate code.
With Apollo I decided to go with a public interface driven design for dependency injection, instead of the more common meta tags+introspection, or the string based message passing as in pureMVC or the AS3 event model.
So what is the good thing about using interfaces in an observer pattern I hear you ask?
- Using interfaces gives the application architect the opportunity to divide an application up into certain areas of interest and group methods and properties together to be used across various controllers.
- Ide’s will let you create stub code for all the interfaces that are implemented, preventing you from writing boilerplate code.
- Type safety for object passing
- No horrible switch statements
You might think: ‘ok, I get that, but why another framework if the existing ones do a good job?’
This framework was developed by me as a way to test this new subscriber/observer method. Just because I can. Hopefully it will give you some new ideas or even a handy new tool.
quick overview
The basis of the framework is a static Injector class which uses the observer pattern to register controllers.
The methods inject and call are used to update controllers. Inject does exactly what you expect it does. I will inject a strongly typed object in every controller that has a registered property, based on their interface.
Given an interface ‘IPersonInjectable’ which has a method set person(val:Person), then a call to Injector.inject(new Person(‘me’)) will inject this new Person instance into every controller. You can see that with altering your models a bit, you can make databinding extremely easy.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| public interface IPersonInjectable
{
function set person(val:Person):void
}
public class PersonViewController extends Controller implements IPersonInjectable
{
public function PersonViewController()
{
super();
}
public function set person(val:Person):void
{
view.person = val;
}
}
new PersonViewController();
Injector.inject(new Person('Laurent', 'Zuijdwijk')) |
Have a look at https://github.com/LaurentZuijdwijk/Apollo for some examples.