« Dynamic creation of messaging destinations in BlazeDS | Main | Keeping Google App Engine alive »

Swiz Framework - Inversion of Control micro-architecture

Swiz is a Flex framework that aims at simplicity in development of Rich Internet Applications by hiding most of the boilerplate code behind well-designed metadata that implement Inversion of Control and simply inject objects where they are required. Besides this, Swiz also introduces Custom Metadata Processors, with which developers can easily introduce their own, meaningful metadata and extend processors to grant them functionality.
In this post we will see how to configure and use the last available version of Swiz, 1.0.0 beta, that can be downloaded from their site as a pre-compiled library or source code from their GIT repository.
All of Swiz configuration is done within the main application file; for example, in MXML:


<fx:Declarations>
<swiz:SwizConfig id="swizConfig" strict="true"
eventPackages="com.comtaste.rss.events"
viewPackages="com.comtaste.rss.view" />
<swiz:Swiz id="swiz" config="{swizConfig}">
<swiz:beanProviders>
<swiz:BeanProvider>
<control:RSSController />
<model:RSSModel />
<swiz:Bean name="serviceLocator">
<model:ServiceLocator />
</swiz:Bean>
</swiz:BeanProvider>
</swiz:beanProviders>
</swiz:Swiz>
</fx:Declarations>

SwizConfig defines the global configuration:


  • eventPackages - name of the package(s) where custom events are defined. These events will be automatically associated with custom controllers that perform logical or remote operations.

  • viewPackages - name of the package(s) where views of the application are defined. Swiz listens for custom events launched by these views and triggers the mapped controller(s).


These two properties ensure that launched events are captured and mapped to their actions, automatically in the background. It is possible to use Swiz injection without automatic event mapping, simply by not setting these properties.

Swiz uses the previously defined SwizConfig to define which objects are to be injected: each instance of this class has its own set of beans, that can be defined in two ways:


  • by class type, inserting each type in the bean provider list

  • by name, inserting a Bean object with a name and a type in the bean provider list

In this example I have chosen to create three beans: controller, model and service locator. I used the service locator in reference to Cairngorm's one, as a repository of remote services, but in this case it's just a simple Object. One can make many other choices, such as placing the remote services within delegates, controllers, or define them as additional beans.

A powerful feature of this new version of Swiz is that it can define multiple bean contexts, each with its set of beans, simply by defining Swiz instances.

The last set of classes we need are the controllers, that may or may not extend Swiz's AbstractController. Extending this class provides methods such as executeServiceCall or executeURLRequest, which allow to call a remote service and assign handler methods:


[Mediate(event="RSSEvent.GET_RSS_FEED")]
public function getFeed():void {
super.executeServiceCall(serviceLocator.rssService.send(), onFeedResult, onFault);
}

Another powerful feature of Swiz comes with the Mediate metadata, that allows a custom event that is defined in one of the packages configured through the eventPackages property to be automatically associated with a method. This handler method can also take parameters in input, as for example the event itself:


[Mediate(event="RSSEvent.GET_RSS_FEED")]
public function getFeed(event:RSSEvent):void {}

More than this, if the event has properties, they can be defined within the metadata and their values are passed to the handler method. Suppose the event has two properties, url and date:


[Mediate(event="RSSEvent.GET_RSS_FEED", properties="url,date")]
public function getFeed(url:String, date:Date):void {}

The properties of each metadata are carefully checked by the Swiz engine, at runtime, and errors are thrown when event names or properties are spelled wrong. These errors are descriptive and it's rather easy to understand which property of which metadata is wrong.

Bean injection is realized through the use of the Inject metadata. This metadata replaces the Autowire one, which got deprecated from the previous stable version:


[Inject(bean="serviceLocator")]
public var serviceLocator:ServiceLocator;

[Inject]
public var model:RSSModel;

In this example, serviceLocator is injected through the name of its bean while model is injected through the type of its class, as defined in the bean providers above.

Swiz, with its dependency injection and custom metadata, is a powerful framework and offers a lot of interesting possibilities. In future posts we will see how to take full advantage of it and use custom metadata.

This working example and its source-code can be found here.

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/112

Comments (3)

Arun:

Dont you think Inject meta data internally creates singleton classes and calls getInstance() where ever required.

Code wise dont you think it is same as adding either Inject public var model:Model same as public var model:Model = Model.getInstance () which is a singleton implementation.

Dont you think there is a downside of using singletons everywhere across is if we are developing an enterprise app with close to 2000+ objects sitting in memory, will it degrade client performance?

Interesting observation, injected beans do act as singletons, within the scope of each Swiz instance. There is another construct which Swiz offers, that is the Prototype. It returns a new instance of an object each time it is called, achieving a sort of deferred instantiation and allowing for the injected object to be unloaded, when its caller is unloaded. More information can be found here.

Swiz aimes at simplicity but is isn't easy as it seems

Carroll

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on April 13, 2010 3:19 PM.

The previous post in this blog was Dynamic creation of messaging destinations in BlazeDS.

The next post in this blog is Keeping Google App Engine alive.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33