Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Implement factory injection #21

Closed
tschneidereit opened this issue Mar 14, 2010 · 4 comments
Closed

Implement factory injection #21

tschneidereit opened this issue Mar 14, 2010 · 4 comments
Labels

Comments

@tschneidereit
Copy link
Owner

Instead of letting SwiftSuspenders directly handle the creation of injected values, it might sometimes be useful to delegate that to a factory.
sammyt's Dawn framework supports factory injections, which looks like a perfect match for this functionality.

Syntax proposal:

Injector#mapFactoryClass(whenAskedFor : Class, useFactoryClass : Class, named : String = '');

and

Injector#mapFactory(whenAskedFor : Class, useFactoryInstance : Object, named : String = '');

The second option will enabled configuring a specific instance of the factory. It might be more useful to use child injectors for configuring the factory itself.

@texastoland
Copy link
Contributor

Should the first snippet read Injector#mapFactoryClass()? What is a factory? Did you like his Provider annotation? That could negate any inter-library interface dependency.

@tschneidereit
Copy link
Owner Author

Yes, that's what I meant, thanks. I've corrected the text.

I like the [Provider] annotation indeed: Much better than to prescribe an interface for the factory. Also, it enables multiple factory methods in one class, each annoted with [Provider] and selected by its return type.

I haven't found any standard annotations, but will look some more. If Guice or Spring support something similar, it might be worthwhile to use they annoation syntax.

@texastoland
Copy link
Contributor

I hadn't considered multiple Providers brilliant!

@tschneidereit
Copy link
Owner Author

Instead of explicitly supporting factory mappings, Swiftsuspenders 2 will have a generic way of mapping to custom providers that only need to implement the method apply(targetClass:Class, usingInjector:Injector):Object. To make it easier to use the custom provider, Swiftsuspenders 2 comes bundled with a FactoryProvider that takes a Class implementing DependencyProvider as a ctor argument and construct an instance of that class for each application, enabling easy implementation of DepencyProviders with their own dependencies.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants