Register and Resolve with Class Scope
Suppose you have an IDoWhatever interface, and you have some concrete implementations like DoWhateverToFoos and DoWhateverToBars. They both implement IDoWhatever, but the former is to be used for Foos while the latter is for Bars.
You can now use the Class object or any instance of the Class as the registration scope. In particular, this came in handy when we had command handlers for various command classes.
// Standard factory.register(FooCommandHandler, IHandleCommands, FooCommand); var handler:IHandleCommands = factory.resolve(IHandleCommands, FooCommand); // Fluent factory.inScope(FooCommand).register(FooCommandHandler).asType(IHandleCommands); var handler:IHandleCommands = factory.fromScope(FooCommand).resolve(IHandleCommands);
Resolve with Fallback Scope
We also had some cases where a class wasn’t registered in the specific scope being requested, but it was registered in the default scope. For example, a default command handler doing some logging might not find the registered ILogger in its own scope. It is now possible to specify to asfac that you’d like to take a look in the default scope in the case where a resolution cannot be performed from the specified scope.
// Standard factory.register(ConsoleLogger, ILogger); var logger:ILogger = factory.resolve(ILogger, "SomeScope", true); // Fluent factory.register(ConsoleLogger).asType(ILogger); var logger:ILogger = factory.fromScope("SomeScope").resolveWithFallback(ILogger);
We also found a few bugs that are now fixed (with accompanying tests, of course!). Basically these were all chalked up to scoped registration using the fluent syntax.
Consider this code:
We found that Widget was actually registered in scope ‘foo’, and we expected it to be in the default scope. We hope asfac users agree. If you did indeed want to register many things into a common scope, you can do it like this:
var scopedRegistrar:IRegister = factory.inScope('scope'); scopedRegistrar.register(Bar).asType(IBaz); scopedRegistrar.register(Widget).asType(ISprocket); var scopedResolver:IResolve = factory.fromScope('scope'); // you get it…
If you’re using asfac, please let us know what you think. If you run into any problems, please log an issue on github. Thanks!
Also, thanks to Adobe for tweeting about asfac. However, we want to clarify that the video embedded in Joey’s original asfac post is not us creating asfac. It’s Jon Skeet hacking out IOC concepts in C#.
— Adobe Flash Platform (@AdobeFlash) February 15, 2013