Closed gdwais closed 2 years ago
Hi!
There seems to be a cyclic dependence between file with container
and TOKENS
and file with AssetRepository
. Let's try to move TOKENS
to separate file and use import type
syntax:
/** AssetRepository.ts */
import { injected } from 'brandi';
import type { PrismaClient } from '@prisma/client';
import { TOKENS } from './tokens';
export class AssetRepository {
db: PrismaClient;
constructor(prismaClient: PrismaClient) {
this.db = prismaClient;
}
// a bunch of other methods
}
injected(AssetRepository, TOKENS.prismaClient);
/** tokens.ts */
import { token } from 'brandi';
import type { PrismaClient } from '@prisma/client';
import type { AssetRepository } from './AssetRepository'
export const TOKENS = {
prismaClient: token<PrismaClient>('PrismaClient'),
assetRepository: token<AssetRepository>('AssetRepository'),
}
/** container.ts */
import { Container } from 'brandi';
import { PrismaClient } from '@prisma/client'
import { TOKENS } from './tokens';
import { AssetRepository } from './AssetRepository';
export const container = new Container();
container.bind(TOKENS.prismaClient).toConstant(new PrismaClient());
container.bind(TOKENS.assetRepository).toInstance(AssetRepository).inContainerScope();
/** index.ts */
import { container } from './container';
import { TOKENS } from './tokens';
const repository = container.get(TOKENS.assetRepository);
console.log(repository);
I hope this helps.
same result sadly - I can try and throw together a quick repo that reproduces the issue if that would be helpful
It would definitely be helpful.
https://github.com/gdwais/brandi-sandbox
So I put this together to demonstrate the bug but everything works great! HAHA and now I'm super confused about why i'm having this issue. Either way you can close this. Thank you!
I'm trying to implement this library and I'm having trouble getting an instance at runtime when the constructor requires a dependency. Constants and instances (that don't have dependencies) work fine. What am I doing wrong?!
Here is the stack it throws:
Here is my implementation:
My class
pulling it out of the container: