Created
April 24, 2020 21:02
-
-
Save kenanhancer/9cab2bd7a908629931b4e9128449d9f3 to your computer and use it in GitHub Desktop.
nut-ioc loading dependencies with programatic dependency definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nutIoc = require('nut-ioc'); | |
const nutIocContainer = nutIoc(); | |
const mainAsync = async () => { | |
nutIocContainer.useDependency({ | |
ServiceName: "authorBasicInfo", | |
Service: ({ firstName: "Kenan", lastName: "Hancer" }) | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "authorWithContacts", | |
Service: ({ authorBasicInfo }) => ({ ...authorBasicInfo, city: "London", mail: "[email protected]" }) | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "greetingHelper", | |
Service: ({ }) => ({ | |
getFullName: ({ firstName, lastName }) => `${firstName} ${lastName}` | |
}) | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "greetingService", | |
Service: ({ greetingHelper: { getFullName } }) => ({ | |
sayHello: ({ firstName, lastName }) => { | |
const fullName = getFullName({ firstName, lastName }); | |
return `Hello ${fullName}`; | |
}, | |
sayGoodbye: ({ firstName, lastName }) => { | |
const fullName = getFullName({ firstName, lastName }); | |
return `Goodbye, ${fullName}`; | |
} | |
}) | |
}); | |
const { authorWithContacts, greetingService } = await nutIocContainer.build(); | |
const helloMsg = greetingService.sayHello(authorWithContacts); | |
console.log(helloMsg); | |
const goodBydMsg = greetingService.sayGoodbye(authorWithContacts); | |
console.log(goodBydMsg); | |
}; | |
mainAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment