RSDI - Dependency Injection Container
Getting Started
import DIContainer, { object, get, value, factory, IDIContainer } from "rsdi";
const config = {
"ENV": value("PRODUCTION"),
"AuthStorage": object(AuthStorage).construct(
get("Storage")
),
"Storage": object(CookieStorage),
"BrowserHistory": factory(configureHistory),
};
const container = new DIContainer();
container.addDefinitions(config);
function configureHistory(container: IDIContainer): History {
const history = createBrowserHistory();
const env = container.get("ENV");
if (env === "production") {
}
return history;
}
const env = container.get<string>("ENV");
const authStorage = container.get<AuthStorage>("AuthStorage");
const history = container.get<History>("BrowserHistory");
Motivation
Popular solution like inversify
or tsyringe
use reflect-metadata
that allows to fetch argument types and based on
that types and do autowiring. I like autowiring but the I don't like the means by which we achieve it.
I don't like
- Those solutions in fact can deal only with typescript only. Since they rely on argument types that we don't have in JS.
- I have to update my tsconfig because of one package.
- Let my components know about injections
@injectable()
class Foo {
}
Why component Foo should know that it's injectable?
More details thoughts in my blog article