swiftsuspenders

This is an OpenFL 2.x/Haxe port of the popular AS3 metadata driven IOC (Inversion Of Control) solution SwiftSuspenders
http://peteshand.net/robotlegs-for-haxe

To install, run:

haxelib install swiftsuspenders 1.1.1 

See using Haxelib in Haxelib documentation for more information.

README.md

Haxe SwiftSuspenders

This is a Haxe port of the AS3 popular dependencies injection library SwiftSuspenders. It's more or less a one for one translation with the added benefit of having a Macro (compile time) reflector, which drastically improves performance.

Features

  • Macro reflector, just make sure than any class that using @inject implements DescribedType
  • injection requests configurable using standardized metadata
  • can inject into vars, setters, methods and constructors
  • injection requests can be optional
  • mapping dependencies by class and, optionally, name
  • satisfying dependencies using value, class instance, singleton or custom providers
  • chaining multiple injectors to create modular dependency-mapping graphs much like inheritance works in OOP
  • defining local and shared scope for mappings, akin to public and private members of a class
  • defining soft mappings that only get used if no injector higher up in the inheritance chain can satisfy a dependency
  • support object live-cycle management (note: The latter part of that management, i.e. the destruction, is not yet implemented, but will be for 2.0 final)

API

Requests

Requests, aka injection points, can be defined using the metadata tag @inject atop the var, setter or method to inject into. For constructor injection, no metadata is required.

Named injections can be defined using the syntax @inject("name=injection name"). In this case, constructors have to have their metadata placed atop the class itself, not the constructor. This is a limitation of the Flex compiler.

Optional injection requests can be defined using the syntax @inject("optional=true")

Mappings

The API is expressed as a DSL to make very fine-grained configuration of each mapping easy and readable:

const injector : Injector = new Injector;

//create a basic mapping:
injector.map(Sprite); //will instantiate a new Sprite for each request for Sprite

//map to another class:
injector.map(Sprite).toType(BetterSprite); //will instantiate a new BetterSprite for each request for Sprite

//map as a singleton:
injector.map(EventDispatcher).asSingleton(); //will lazily create an instance and return it for each consecutive request

//map an interface to a singleton:
injector.map(IEventDispatcher).toSingleton(EventDispatcher);

Beta

So, that's it for now as far as documentation is concerned. The implementation, on the other hand, is very stable and shouldn't regress anything that used to work in 1.x, as long as the API changes are dealt with, of course.

More documentation will soon come here, in the github wiki and as asdocs.

Contributors
tbyrne
p.j.shand
Version
1.1.1
Published
5 years ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub