Repository
Current version released
3 years ago
dino
Minimalistic type safe DI (Dependency Injection) library for Deno/Node
Installation
Deno
import { DI } from "https://deno.land/x/dino@{version}/mod.ts";
Node
npm install @keroxp/dino
import { DI } from "@keroxp/dino";
Usage
interface Deps {
service: IService;
repository: IRepository;
}
interface IService {
send(message: string): Promise<void>;
}
class Service implements IService {
async send(message: string) {}
}
interface IRepository {
get(key: string): string;
}
class Respository implements IRepository {
get(key: string): string {
return "value";
}
}
const di = new DI<Deps>(); // <- instantiate with dependency types
di.set("service", new Service());
di.set("repository", new Repository());
const service = di.get("service");
service.send("message"); // <- type safe
const repository = di.get("repository");
repository.get("key"); // <- type safe
// error (doesn't exist in Deps)
di.set("something");
di.get("anything");
Lazy Loader
const di = new DI<{ something: number }>();
di.setLazy("something", () => 1);
di.has("something"); // false
const val = di.get("something"); // 1