christophe spring actionscript flex java exchange

40
SPRING ACTIONSCRIPT Christophe Herreman

Upload: skills-matter

Post on 21-Jun-2015

2.083 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Christophe Spring Actionscript Flex Java Exchange

SPRING ACTIONSCRIPTChristophe Herreman

Page 2: Christophe Spring Actionscript Flex Java Exchange

Agenda

What is Spring ActionScript The IoC container Configuration MVC Architecture Demo Q&A

Page 3: Christophe Spring Actionscript Flex Java Exchange

What is Spring ActionScript (1/2)

Inversion of Control (IoC) for ActionScript 3.0 Flash/Flex/AIR/Pure AS3 IoC container Tailored architectures (no prescriptive structure)

Page 4: Christophe Spring Actionscript Flex Java Exchange

What is Spring ActionScript (2/2)

Started as an in-house library Formerly known as the Prana framework Incubated as a Spring Extension project Upcoming release 1.0 (1.0RC1) AS3Commons: Lang, Logging, Reflect, …

Page 5: Christophe Spring Actionscript Flex Java Exchange

The IoC Container (1/2)

= Object Factory Creates and assembles objects Centralized dependency management Spring AS: configuration via XML or MXML

Page 6: Christophe Spring Actionscript Flex Java Exchange

The IoC Container (2/2)

Object (bean): object managed and/or created by the container

Object Factory: factory that creates and manages objects factory.getObject(“myObject”)

Object Definition: blueprint for an object Application Context: smarter Object Factory

Page 7: Christophe Spring Actionscript Flex Java Exchange

Configuration (1/9)

MXML Configuration

// AppContext.mxml file, compiled into the application

<Objects>

<app:ApplicationModel id=“appModel” />

<app:ApplicationController id=“appController” applicationModel=“{appModel}”/>

</Objects>

Page 8: Christophe Spring Actionscript Flex Java Exchange

Configuration (2/9)

MXML Configuration: loading

var context:MXMLApplicationContext = new MXMLApplicationContext();context.addConfig(AppContext);context.addEventListener(Event.COMPLETE, context_completeHandler);context.load();

function context_completeHandler(event:Event):void {var appModel:ApplicationModel = context.getObject(“appModel”);var appController:ApplicationController =

context.getObject(“appController”);}

Page 9: Christophe Spring Actionscript Flex Java Exchange

Configuration (3/9)

MXML Configuration: alternative approach

// AppContext.mxml file, compiled into the application

<Objects>

<Object id=“appModel” clazz=“{ApplicationModel}”/>

<Object id=“appController” clazz=“{ApplicationController}”><Property name=“applicationModel” ref=“appModel”/>

</Object>

</objects>

Page 10: Christophe Spring Actionscript Flex Java Exchange

Configuration (4/9)

MXML Configuration: pros

Editor support Simple to use

Page 11: Christophe Spring Actionscript Flex Java Exchange

Configuration (5/9)

MXML Configuration: cons

Compiled into the application Explicit declaration: limited to singletons, no

external references, no prototypes -> use Spring AS MXML dialect

Page 12: Christophe Spring Actionscript Flex Java Exchange

Configuration (6/9)

XML Configuration

// external context.xml, loaded on application startup

<objects>

<object id=“appModel” class=“com.domain.app.ApplicationModel”/>

<object id=“appController” class=“com.domain.app.ApplicationController”>

<property name=“applicationModel” ref=“appModel”/></object>

</objects>

Page 13: Christophe Spring Actionscript Flex Java Exchange

Configuration (7/9)

XML Configuration: loading

var context:XMLApplicationContext = new XMLApplicationContext();context.addConfigLocation(“context.xml”);context.addEventListener(Event.COMPLETE, context_completeHandler);context.load();

function context_completeHandler(event:Event):void {var appModel:ApplicationModel = context.getObject(“appModel”);var appController:ApplicationController =

context.getObject(“appController”);}

Page 14: Christophe Spring Actionscript Flex Java Exchange

Configuration (8/9)

XML Configuration: pros

Richer dialect than MXML config Usable for non-Flex projects No need to recompile (in some cases) Familiar to Spring Java users

Page 15: Christophe Spring Actionscript Flex Java Exchange

Configuration (9/9)

XML Configuration: cons

Classes need to be compiled into the app, no class loading (!)

No editor support, only XSD

Page 16: Christophe Spring Actionscript Flex Java Exchange

MVC Architecture (1/3)

No real prescriptive architecture Do you really need one? Roll your own because one does NOT fit all Many MVC architectures out there:

Cairngorm (2), PureMVC, Mate, …… Spring AS wants to support them (see

extensions)

Page 17: Christophe Spring Actionscript Flex Java Exchange

MVC Architecture (2/3)

Spring AS MVC ingredients Operation API Event Bus Autowiring

Recommendations Presentation Model (Fowler) Layered Architecture (DDD – Evans)

Page 18: Christophe Spring Actionscript Flex Java Exchange

MVC Architecture (3/3)

Layered Architecture (DDD – Evans)

Page 19: Christophe Spring Actionscript Flex Java Exchange

The Operation API (1/7)

Asynchronous behavior in Flash Player: loading external resources, RMI, sqlite, Webservices, HTTPServices, …

No threading API Many different APIs: AsyncToken,

Responders, Callbacks, Events… we need a unified approach!

Page 20: Christophe Spring Actionscript Flex Java Exchange

The Operation API (2/7)

Problem: a user repository (or service)

interface IUserRepository {function getUser(id:int): ???

}

What does the getUser method return? AsyncToken (Flex/RemoteObject), User (In memory), void (events)

Page 21: Christophe Spring Actionscript Flex Java Exchange

The Operation API (3/7)

Spring AS solution: return an IOperation

interface IUserRepository {function getUser(id:int):IOperation;

}

Can now be used in any ActionScript 3 context and easily mocked for testing

Page 22: Christophe Spring Actionscript Flex Java Exchange

The Operation API (4/7)

In Spring AS, an “operation” is used to indicate an asynchronous execution

IOperation interface with “complete” and “error” events

IProgressOperation with “progress” event OperationQueue bundles operations (a

composite operation)

Page 23: Christophe Spring Actionscript Flex Java Exchange

The Operation API (5/7)

Defining an operation:

public class GetUserOperation extends AbstractOperation {

function GetUserOperation(remoteObject:RemoteObject, id:String) {var token:AsyncToken = remoteObject.getUser(id);token.addResponder(new Responder(resultHandler,

faultHandler));}

private function resultHandler(event:ResultEvent):void {dispatchCompleteEvent(User(event.result));

}

private function faultHandler(event:FaultEvent):void {dispatchErrorEvent(event.fault);

}}

Page 24: Christophe Spring Actionscript Flex Java Exchange

The Operation API (6/7)

Usage:

var operation:IOperation = new GetUserOperation(remoteObject, 13);

operation.addCompleteListener(function(event:OperationEvent):void {var user:User = event.result;

});

operation.addErrorListener(function(event:OperationEvent):void {Alert.show(event.error, “Error”);

});

Page 25: Christophe Spring Actionscript Flex Java Exchange

The Operation API (7/7)

A progress operation:

var operation:IOperation = new SomeProgressOperation(param1, param2);

operation.addCompleteListener(function(event:OperationEvent):void {});

operation.addErrorListener(function(event:OperationEvent):void {});

operation.addProgressListener(function(event:OperationEvent):void {var op:IProgressOperation = IProgressOperation(event.operation);trace(“Progress:” + op.progress + “, total: “ + op.total);

});

Page 26: Christophe Spring Actionscript Flex Java Exchange

The Event Bus (1/4)

Publish/subscribe event system Used for global application events Promotes loose coupling Works with standard Flash Events; no need to

subclass Spring AS base classes

Page 27: Christophe Spring Actionscript Flex Java Exchange

The Event Bus (2/4)

Listening/subscribing to events:

// listen for all eventsEventBus.addListener(listener:IEventBusListener);

function onEvent(event:Event):void {}

// listen for specific eventsEventBus.addEventListener(“anEvent”, handler);

function handler(event:Event):void {}

Page 28: Christophe Spring Actionscript Flex Java Exchange

The Event Bus (3/4)

Dispatching/publishing events:

// dispatch standard eventEventBus.dispatchEvent(new Event(“someEvent”));

// dispatch custom eventclass UserEvent extends Event {

... }

EventBus.dispatchEvent(new UserEvent(UserEvent.DELETE, user));

Page 29: Christophe Spring Actionscript Flex Java Exchange

The Event Bus (4/4)

Routing events:

<object id="routeEventsProcessor" class="org.springextensions.actionscript.ioc.factory.config.RouteEventsMetaDataPostProcessor"/>

[RouteEvents]Public class MyClass {}

or…

[RouteEvents(events=“eventA,eventB”)]Public class MyClass {}

Page 30: Christophe Spring Actionscript Flex Java Exchange

Autowiring (1/2)

Auto Dependency Injection via metadata Autowire by type, name, constructor,

autodetect Works for objects managed by the container

and for view components Be careful: magic happens, no explicit

configuration

Page 31: Christophe Spring Actionscript Flex Java Exchange

Autowiring (2/2)

Annotate a property with [Autowired]

class UserController {

[Autowired]public var userRepository:IUserRepository;

}

[Autowired(name=“myObject“, property=“prop”)]

Page 32: Christophe Spring Actionscript Flex Java Exchange

The Container (1/5)

Object references

<object id=“me” class=“Person”/>

<object id=“john” class=“Person”><property name=“buddy" ref=“me“ />

</object>

Page 33: Christophe Spring Actionscript Flex Java Exchange

The Container (2/5)

PropertyPlaceholderConfigurerer Loads external properties

Properties: (server.properties)host=172.16.34.4port=8081contex-root=myapp

Config:<object class=“org.springextensions.actionscript.ioc.factory.confi

g.PropertyPlaceholderConfigurer”><property name=“location” value=“server.properties”/>

</object>

<object id=“userRemoteObject” class=“mx.rpc.remoting.mxml.RemoteObject”><property name="endpoint" value="http://${host}:${port}/${context- root}/messagebroker/amf" />

</object>

Page 34: Christophe Spring Actionscript Flex Java Exchange

The Container (3/5)

Parent definitions / abstract definitions

<object id="remoteObject" class="mx.rpc.remoting.mxml.RemoteObject" abstract="true">

<property name="endpoint" value="http://${host}:${port}/${context-root}/messagebroker/amf" />

<property name="showBusyCursor" value="true" />

</object>

<object id=“userRemoteObject" parent="remoteObject"><property name="destination" value=“userService" />

</object>

Page 35: Christophe Spring Actionscript Flex Java Exchange

The Container (4/5)

Object definition scopesSingleton (default): same instance is returned on

each requestPrototype: new instance is returned

<object id=“mySingleton” class=“MyClass”/>

<object id=“myPrototype” class=“MyClass” scope=“prototype”/>

or…<object id=“myPrototype” class=“MyClass”

singleton=“false”/>

Page 36: Christophe Spring Actionscript Flex Java Exchange

The Container (5/5)

Lazy initialized objectsSingletons that are not instantiated when the

container starts

<object id=“myLazyObject” class=“MyClass”lazy-init=“true”/>

Page 37: Christophe Spring Actionscript Flex Java Exchange

And there is more…

Module support XML namespaces (rpc, messaging, util, …) Task API Service base classes Container extension points (factory post

processors, object post processors, custom metadata handlers, …)

Working on: testing support, AOP, …

Page 38: Christophe Spring Actionscript Flex Java Exchange

Summary

Dependency Management Loose coupling, type to interfaces Use with other frameworks Spring mentality: provide choice Promote best practices

Page 39: Christophe Spring Actionscript Flex Java Exchange

DEMOA sample application

Valerie Hillewaere
DELETE THIS ITEM: The Demo slide should be used before each demo, so we've a good indication during post-processing. If possible try to group all of your demos in one block.