working example
This commit is contained in:
@@ -1,23 +1,37 @@
|
||||
|
||||
import { MusicCatalogService } from "../domain/services/MusicCatalogService"
|
||||
import { IMusicRepository } from "../domain/ports/IMusicRepository";
|
||||
|
||||
import { VinylCatalog } from "../adapters/music/VinylCatalog"
|
||||
import { Track } from "../domain/models/Track";
|
||||
import { VinylCatalog } from "../adapters/music/VinylCatalog";
|
||||
|
||||
export class MusicComponent {
|
||||
constructor() {
|
||||
|
||||
let container = {
|
||||
IMusicRepository: () => new VinylCatalog()
|
||||
let container : any = { };
|
||||
|
||||
let register = (name : string, fn : Function) => {
|
||||
container[name] = fn;
|
||||
};
|
||||
|
||||
let inject = (name : string) : any => {
|
||||
if (container[name]) {
|
||||
return container[name];
|
||||
return container[name]();
|
||||
}
|
||||
throw new Error(`Failed to resolve ${name}`)
|
||||
};
|
||||
let musicCatalogService = new MusicCatalogService(inject("IMusicRepository") as IMusicRepository);
|
||||
|
||||
console.log('registering IMusicRepository');
|
||||
register('IMusicRepository', () => new VinylCatalog());
|
||||
|
||||
let musicRepository = inject("IMusicRepository") as IMusicRepository;
|
||||
console.log('IMusicRepository is', musicRepository);
|
||||
|
||||
let musicCatalogService = new MusicCatalogService(musicRepository);
|
||||
|
||||
let track = new Track(8, "Niels 1st", "Niels Kooiman", 45);
|
||||
musicCatalogService.add(track);
|
||||
|
||||
let tracks = musicCatalogService.get();
|
||||
console.log(tracks);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user