Comments (8)
I love these questions!
This is very easy to do because of the way Awilix' resolver system works. 😃
const container = createContainer()
.register({
// Let's use a custom resolver!
createdEventHandlers: asArray([
asClass(DatabaseEventHandler),
asClass(MessageBusEventHandler)
]),
deletedEventHandler: asArray([
asClass(CleanUpEventHandler)
])
})
/**
* Given an array of resolvers, returns a resolver that
* resolves to an array of resolved values... RESOLVER!
*/
function asArray(resolvers) {
return {
resolve: (container, opts) => resolvers.map(r => container.build(r, opts))
}
}
Thanks for the great question!
EDIT: fix example
from awilix.
@jeffijoe Thank you for your suggestion. I tweaked your resolver function a little bit to better fit the TypeScript system, as well as my own needs. The following works perfectly for me.
function asArray<T>(resolvers: Resolver<T>[]): Resolver<T[]> {
return {
resolve: c => resolvers.map(r => r.resolve(c))
};
}
container .register({
// Let's use a custom resolver!
createdEventHandlers: asArray([
asClass(DatabaseEventHandler),
asClass(MessageBusEventHandler)
]),
deletedEventHandler: asArray([
asClass(CleanUpEventHandler)
])
})
from awilix.
No problem! I forgot that the resolver is an object with a resolve
function and not just a function so this was on me. 😄
from awilix.
Happy to help!
from awilix.
Hi, @jeffijoe!
I'm trying to do the same thing. But I'm getting a
TypeError: resolver.resolve is not a function
https://github.com/eizonishime/poc-dependency-injection-nodejs/blob/master/index.js#L14
and I'm using asArray
as you suggested:
https://github.com/eizonishime/poc-dependency-injection-nodejs/blob/master/helper/AwilixHelper.js#L2
Again, I'm probably doing something very silly.
You can see my tests in this repo: https://github.com/eizonishime/poc-dependency-injection-nodejs
from awilix.
@eizonishime I most likely screwed up my own example 😅
This should work:
/**
* Given an array of resolvers, returns a resolver that
* resolves to an array of resolved values... RESOLVER!
*/
function asArray(resolvers) {
return {
resolve: (container, opts) => resolvers.map(r => container.build(r, opts))
}
}
from awilix.
Oh sorry, I didn't pay attention to the syntax.
And it worked now.
Thanks again!
from awilix.
I love these questions!
This is very easy to do because of the way Awilix' resolver system works. smiley
const container = createContainer() .register({ // Let's use a custom resolver! createdEventHandlers: asArray([ asClass(DatabaseEventHandler), asClass(MessageBusEventHandler) ]), deletedEventHandler: asArray([ asClass(CleanUpEventHandler) ]) }) /** * Given an array of resolvers, returns a resolver that * resolves to an array of resolved values... RESOLVER! */ function asArray(resolvers) { return { resolve: (container, opts) => resolvers.map(r => container.build(r, opts)) } }Thanks for the great question!
EDIT: fix example
@jeffijoe: an even more elegant solution is to register de dependencies and to resolve an array of names so that is is easy to reuse the registrations and change to array contents. Which is ideal for a plugin architecture.
const container = createContainer()
.register({
databaseEventHandler: asClass(DatabaseEventHandler),
messageBusEventHandler: asClass(MessageBusEventHandler),
cleanUpEventHandler: asClass(CleanUpEventHandler),
// Let's use a custom resolver!
createdEventHandlers: asArray([
'databaseEventHandler',
'messageBusEventHandler'
]),
deletedEventHandler: asArray([
'cleanUpEventHandler'
])
})
function asArray(names) {
return {
resolve: (container, opts) => names.map(name => container.resolve(name, opts))
}
}
or even better make it possible to register an array of names which is automaticly resolved by the engine:
const container = createContainer()
.register({
databaseEventHandler: asClass(DatabaseEventHandler),
messageBusEventHandler: asClass(MessageBusEventHandler),
cleanUpEventHandler: asClass(CleanUpEventHandler),
// Let the engine resolve!
createdEventHandlers: [
'databaseEventHandler',
'messageBusEventHandler'
],
deletedEventHandler: [
'cleanUpEventHandler'
]
})
}
from awilix.
Related Issues (20)
- container.resolve type auto-inference feature HOT 1
- Whiteboard pattern HOT 7
- [Question] Support for assisted inject HOT 2
- awilix object is undefined when using default import, but named import of functions work HOT 1
- Support for ESM modules HOT 3
- Is it possible to reinforce constructor param name/types? HOT 1
- Can't resolve HOT 4
- How to unregister a dependency from the container? HOT 6
- Default value not working in class constructor in Proxy mode HOT 2
- asFunction -> asClass Cyclic dependency HOT 2
- Awilix TypeError: Cannot read properties of undefined (reading 'cache') HOT 2
- Calling `loadModules` does not register all the file paths HOT 2
- Is it possible to detect / error on singletons dependent on transient or scoped registrations? HOT 1
- Awilix 10 type exposure HOT 1
- [QUESTION] Userland async initializers HOT 7
- replace camel-case dependency HOT 3
- Possible valid use-case for disabling strict mode for specific resolver HOT 2
- Critical dependency: the request of a dependency is an expression HOT 3
- Add TypeScript support HOT 1
- [QUESTION] - Awilix Fails to Autoload Modules in Vitest Tests with TypeScript Setup HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from awilix.