ObjectBuilder

(This tag must be placed inside an <EventHandlers> tag, <MessageHandlers> tag or <Injectors> 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. See Passing Arguments for more information.

cache

either "global", "inherit", "local" or "none"

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 "inherit", 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 "none".

The value "inherit" will globally cache the object in a normal EventMap and it will locallly cache the object in a LocalEventMap. If you want to cache globally in a LocalEventMap so that the object can be accessed from other event maps, set the value to "global".

<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.

15 responses so far

  1. the docs here seem to indicate that cache is a boolean property, while the source code has it set as a string with this enumeration:[Inspectable(enumeration="local,global,inherit,none")]

    the docs should probably be updated to reflect his.
  2. While on this topic, is it expected that an object built with the objectBuilder and a cache="global" in a top level EventMap will be the same object reused in a localEventMap referencign that same class?

    Having trouble making that work, if that is indeed how it should work
  3. yes, I need to update the docs.

    If your local event map needs to access the same object, then, when you use it with MethodInvoker, you need to specify cache="global" to indicate that it should access the global instance. You don't need to specify that in the EventMap, as global is the default. I believe the local event map tags use the default "inherit" default cache value, which is local for a Local Event map, so unless you specify otherwise, a Local event map and a global event map would not share those instances.
  4. Thanks Laura, that seems to do the trick. I also had to use sourceCache="global" on the PropertyInjectors which use that object.

    BTW, looking at the source, it seems as if ObjectBuilder has "inherit" not "global" as its default.

    Thanks again
  5. Sorry, im just putting it all together now, I finally understand that cache="inherit" means it has the same cache as the EventMap itself, and the base EventMap has cache=Cache.GLOBAL, which LocalEventMap has cache=Cache.LOCAL.

    So, of course, you were right to say that by default the ObjectBuilder was cached globally in the EventMap, and locally in the LocalEventMap.

    Sorry for the confusion

Leave a response

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

Leave this field empty