Skip to main content
Module

x/diosaur/docs/multiple-services.md

A dependency injection library for Deno
Latest
File

Injecting multiple services

We saw previously that we could tag our services, as such some of them might share the same service identifier. We can inject all services that share a common identifier using the @InjectAll decorator.

Taking the example from previous page:

const TYPES = {
    cache: Symbol('cache')
};

@Service({ identifier: TYPES.cache, tag: 'redis' })
class RedisCache implements ICache { /* ... */ }

@Service({ identifier: TYPES.cache, tag: 'local' })
class LocalCache implements ICache { /* ... */ }

We can inject them all in another service easily:

@Service()
class MyService {

    @InjectAll(TYPES.cache)
    private caches: ICache[];

    // Or through constructor
    constructor(@InjectAll(TYPES.cache) caches: ICache[]) {}
}

Resolving them manually can be done with:

container.getAll(TYPES.cache);