S
M i c r o D I
a M
m P
n L
E
Very Simple Dependency Injection library for Deno
Provide a service:
import { provide } from 'https://deno.land/x/microdi/Provider.ts';
class ServiceFoo {
public foo() {
console.log('Foo!');
}
}
provide(
// The service you'd like to provide
ServiceFoo,
// Service dependencies (i.e. other services)
[]
);
class ServiceBar {
public bar() {
console.log('Bar!');
}
}
provide(ServiceBar, []);
class ServiceFooBar {
public constructor(
private readonly foo: ServiceFoo,
private readonly bar: ServiceBar
) {}
}
provide(ServiceFooBar, [ServiceFoo, ServiceBar]);
class App {
public constructor(fooBar: ServiceFooBar) {}
}
provide(App, [ServiceFooBar]);
const app = resolve(App); // App!
- No experimental decorators!
- Lazy initialization: services are being instantiated on the fly on demand.
- You can use a scope for your services. Just do the following:
import { ServicesMap } from 'https://deno.land/x/microdi/ServiceStorage.ts';
const servicesMap = new ServicesMap();
class Foo {}
servicesMap.provides(Foo, []);
- Cyclic dependencies are not supported.
- This library can instantiate services as a singletons only.
- No typechecking at dependencies
$ deno test