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 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);

Bug Fixes

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:

factory.inScope('foo').register(Bar).asType(IBaz);
factory.register(Widget).asType(ISprocket);

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 Adobe

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

Advertisements

About David Ruttka

I've been "making computers do things" since I first saw King's Quest on a 286 PC in the mid-80's, but I turned it into a career just over a decade ago. While the majority of my experience has been on the Microsoft stack (C#, .NET, ASP.NET), I've recently been diving deeper into JavaScript and exploring the Ruby universe. Occasionally, I'll do a public speaking gig or write a blog post. When I'm not coding, I enjoy spending time with my family, watching hockey, and playing the occasional video game. You can also find me on Stack Overflow, Google Plus, and Twitter. Microsoft Certified Programming in HTML5 with JavaScript and CSS3 Specialist; MCPD Windows Developer 3.5
This entry was posted in Development and tagged , , , . Bookmark the permalink.

One Response to Updates pushed to asfac master

  1. Anonymous says:

    Whoever managed the Adobe twitter feed. #NailedIt

Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s