RSDI - Dependency Injection Container
Getting Started
import DIContainer, { object, get, value, factory, IDIContainer } from "rsdi";
const config = {
"ENV": value("PRODUCTION"),
"Storage": object(CookieStorage),
"AuthStorage": object(AuthStorage).construct(
get("Storage")
),
"BrowserHistory": factory(configureHistory),
};
const container = new DIContainer();
container.addDefinitions(config);
const env = container.get<string>("ENV");
const authStorage = container.get<AuthStorage>("AuthStorage");
const history = container.get<History>("BrowserHistory");
function configureHistory(container: IDIContainer): History {
const history = createBrowserHistory();
const env = container.get("ENV");
if (env === "production") {
}
return history;
}
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?