Updates pushed to asfac master
Integrating asfac into a real production codebase brought some necessary enhancements to our attention. The following updates have now been pushed to master.
Register and Resolve with Class ScopeSuppose 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 ScopeWe 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);
Bug FixesWe 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!
Thanks AdobeAlso, 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#.
Learn how #Asfac, an #IoC container, was created in this in-depth #ActionScript video: bit.ly/SZGXo4
â€” Adobe Flash Platform (@AdobeFlash) February 15, 2013
This post originally appeared on The DevStop.