ObjectBuilder

(This tag must be placed inside an <EventHandlers> tag or <MessageHandlers> tag)

When placed inside an EventHandlers block, and the list of handlers is executed, it will create an object of the class specified in the "generator" attribute. You can pass arguments to the constructor of this class that come from a variety of sources, such as the event itself, a server result object, or any other value. Unless you specify cache="false", this object instance will be "cached" and not instantiated again when using the MethodInvoker or PropertyInjectors

Example:

<ObjectBuilder generator="ClassNameToInstantiate"
   constructorArguments="{['argument1','argument2']}" />

The above example would be the same as doing the following in ActionScript code:

var myObject:ClassNameToInstantiate = new ClassNameToInstantiate('argument1', 'argument2');

Attributes

generator

required

The generator attribute specifies what class should be instantiated.
Suppose you have a class called "MyClass" in the package com.yourdomain.business. You can specify a complete path to com.yourdomain.business.MyClass:

<ObjectBuilder generator="com.yourdomain.business.MyClass" />

Generally you may want to use a binding to specify the class name. Assuming you have an import statement like this in your Event Map:

import com.yourdomain.business.MyClass;

or simply:

import com.yourdomain.business.*;

You can then instantiate your worker using bindings:

<ObjectBuilder generator="{MyClass}" />

The advantage of using this syntax is that if you are using Flex Builder, you can press the command key (Mac) or the Ctrl key (Windows) and click on the generator class (MyWorker in the example) and it will take you to the class definition.

constructorArguments

Array

If the constructor of you class requires arguments, you can pass them via the "constructorArguments" attribute. Suppose your constructor has the following signature:

public function MyClass(name:String, value:Number)

then instantiating the class as follows will work:

<ObjectBuilder generator="{MyClass}"
   constructorArguments="{['Tom', 36]}"/>

Note that the constructorArguments attribute expects an array. Besides passing literal values, you can pass values coming from the event that triggered the handlers list:

<ObjectBuilder
   generator="{MyClass}"
   arguments="{[event.userName, event.age]}"/>

This assumes that the event contained a userName property and an age property.

You can also pass values coming from a service result, fault, or others. See MethodInvoker, arguments attribute for more information.

cache

Boolean

The cache attribute lets you specify whether this newly created object should be kept live so that the next time an instance of this class is requested, this already created object is returned instead. The instance can be requested by a MethodInvoker or a PropertyInjector.

For example, you may want to have a MethodInvoker use an already instantiated instance created by an ObjectBuilder.  Since the default value for this attribute is "true", it will do that by default. On the other hand, if you wanted to have two different instances, then you must set this attribute to "false".

<EventHandlers type="myEventType">
   <ObjectBuilder generator="{MyClass}" />
</EventHandlers>

<EventHandlers type="myOtherEventType">
   <MethodInvoker generator="{MyClass}" method="doWork" />
</EventHandlers>

Inner tags

Properties


You can add properties to your instantiated object by using the Properties tag inside the ObjectBuilder tag. These properties must be public.

Suppose you are creating an instance of a ShippingCalculator class. This class has a property called weightFactor and flatFee. In order to set those two properties, you can use the <Properties> inner tag. As attributes of the Properties tag, you can specify the names of your properties and set the values of those properties by setting the value of those attributes as follows:

<ObjectBuilder generator="{ShippingCalculator}">

   <Properties weightFactor="0.5" flatFee="3" />
   
</ObjectBuilder>

Besides specifying literal values, you can assign values coming from the event that triggered the sequence:

<ObjectBuilder generator="{ShippingCalculator}">

   <Properties weightFactor="{event.factor}" flatFee="{event.fee}" />
   
</ObjectBuilder>

This assumes that the original event contained a factor property and a fee property.

Other sources can include service results or faults, values returned by a MethodInvoker, etc.

0 responses so far

Leave a response

If you need help or want to comment on something not related to this page, please post in the forums. Thanks!