本文整理汇总了TypeScript中inversify.Container.load方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Container.load方法的具体用法?TypeScript Container.load怎么用?TypeScript Container.load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inversify.Container
的用法示例。
在下文中一共展示了Container.load方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it("Should be able to auto-wire binding declarations", () => {
let container = new Container();
// Note @inject annotations are required autoProvide
// even when using classes are used as identifiers
// See declaration of Warrior for more details
autoProvide(container, entites);
container.load(buildProviderModule());
let warrior = container.get(entites.Warrior);
expect(warrior.fight()).eql("Using Katana...");
});
示例2: beforeEach
beforeEach(async () => {
container = new Container();
container.load(ormModule);
// Services
container
.bind<SettingsServiceAttributes>(registry.SettingsService)
.to(MockSettingsService)
.inSingletonScope();
mockSettingsService = container.get<SettingsServiceAttributes>(
registry.SettingsService
);
mockSettingsService.loggerTransports = [
new winston.transports.Console({
silent: true
})
];
mockLogger = new winston.Logger();
const loggerFactory: interfaces.FactoryCreator<winston.LoggerInstance> = (
context: interfaces.Context
) => {
return () => {
return mockLogger;
};
};
container
.bind<interfaces.Factory<winston.LoggerInstance>>(registry.LoggerFactory)
.toFactory(loggerFactory);
// ORM
container
.bind<ConnectionOptions>(registry.ORMConnectionOptions)
.toConstantValue({
autoSchemaSync: true,
type: 'sqlite',
database: ':memory:'
});
connection = (await container.get<interfaces.Provider<Connection>>(
registry.ORMConnectionProvider
)()) as Connection;
// Express configs
container
.bind<CardSetControllerAttributes>(CardSetController)
.toSelf();
app = express();
useContainer(container);
useExpressServer(app, {
routePrefix: '/api',
controllers: [CardSetController],
development: true
});
});
示例3: Container
/* tslint:enable:no-import-side-effect */
import * as express from 'express';
import * as winston from 'winston';
import { interfaces } from 'inversify';
import { Connection, ConnectionOptions } from 'typeorm';
import { expressWebAppProdFactory } from 'src/server/express/factories/express-prod';
import { registry } from 'src/server/registry';
import { SettingsServiceAttributes } from 'src/server/shared/services/Settings.interface';
import { Container } from 'inversify';
import { module as expressModule } from 'src/server/express/module';
import { module as sharedModule } from 'src/server/shared/module';
import { module as ormModule } from 'src/server/orm/module';
const container = new Container();
container.load(sharedModule, ormModule, expressModule);
/** ORM DB Connection */
const connectionOptions: ConnectionOptions = container.getTagged<
interfaces.Factory<ConnectionOptions>
>(
registry.ORMConnectionOptionsFactory,
'type',
registry.ORMSQLiteConnectionOptionsFactory
)({
autoSchemaSync: true
}) as ConnectionOptions;
container
.bind<ConnectionOptions>(registry.ORMConnectionOptions)
.toConstantValue(connectionOptions);
const connectionProvider: interfaces.Provider<Connection> = container.get<
示例4: it
it("Should resolve from container directly", () => {
const container = new Container();
const {
lazyInject,
lazyInjectNamed,
lazyInjectTagged,
lazyMultiInject
} = getDecorators(container, false);
const SINGLETON_FOO = "SINGLETON_FOO";
const FOO = "FOO";
const BAR = "BAR";
@injectable()
class FooBarBase {
}
@injectable()
class SingletonFoo extends FooBarBase {
}
@injectable()
class Foo extends FooBarBase {
}
@injectable()
class Bar extends FooBarBase {
}
@injectable()
class NamedBar extends FooBarBase {
}
@injectable()
class TaggedBar extends FooBarBase {
}
const mFoo = new ContainerModule((bind: interfaces.Bind) => {
bind<FooBarBase>(SINGLETON_FOO).to(SingletonFoo);
bind<FooBarBase>(FOO).to(Foo);
});
const mBar = new ContainerModule((bind: interfaces.Bind) => {
bind<FooBarBase>(FOO).to(Bar);
bind<FooBarBase>(BAR).to(NamedBar).whenTargetNamed("bar");
bind<FooBarBase>(BAR).to(TaggedBar).whenTargetTagged("bar", true);
});
container.load(mFoo, mBar);
@injectable()
class Test {
@lazyInject(SINGLETON_FOO) public singletonFoo: FooBarBase;
@lazyMultiInject(FOO) public foos: FooBarBase[];
@lazyInjectNamed(BAR, "bar") @named("bar") public namedFoo: FooBarBase;
@lazyInjectTagged(BAR, "bar", true) @tagged("bar", true) public taggedFoo: FooBarBase;
}
const sut: any = new Test();
function actual(key: string): any {
return sut[key];
}
expect(actual("singletonFoo")).to.be.instanceof(SingletonFoo);
let foos: FooBarBase[] = actual("foos");
expect(foos.length).to.equal(2);
expect(foos[0]).to.be.instanceof(Foo);
expect(foos[1]).to.be.instanceof(Bar);
container.unload(mBar);
expect(actual("singletonFoo")).to.be.instanceof(SingletonFoo);
foos = actual("foos");
expect(foos.length).to.equal(1);
expect(foos[0]).to.be.instanceof(Foo);
function throws(key: string): () => any {
return () => {
return sut[key];
};
}
expect(throws("namedFoo")).to.throw(
"No matching bindings found for serviceIdentifier: BAR"
);
expect(throws("taggedFoo")).to.throw(
"No matching bindings found for serviceIdentifier: BAR"
);
container.unload(mFoo);
expect(throws("singletonFoo")).to.throw(
"No matching bindings found for serviceIdentifier: SINGLETON_FOO"
);
expect(throws("foos")).to.throw(
"No matching bindings found for serviceIdentifier: FOO"
);
});
示例5: require
.to(Statistics)
.inSingletonScope()
container
.bind<DataRetentionJanitor>(TYPES.DataRetentionJanitor)
.to(DataRetentionJanitor)
.inSingletonScope()
container
.bind<DataRetentionService>(TYPES.DataRetentionService)
.to(DataRetentionService)
.inSingletonScope()
const isPackaged = !!eval('process.pkg')
const isProduction = process.IS_PRODUCTION
container.bind<boolean>(TYPES.IsProduction).toConstantValue(isProduction)
container.bind<boolean>(TYPES.IsPackaged).toConstantValue(isPackaged)
container.load(...DatabaseContainerModules)
container.load(...RepositoriesContainerModules)
container.load(...ServicesContainerModules)
if (process.BOTPRESS_EDITION && process.BOTPRESS_EDITION !== 'ce') {
// Otherwise this will fail on compile when the submodule is not available.
const ProContainerModule = require('pro/services/pro.inversify')
container.load(ProContainerModule)
}
applyDisposeOnExit(container)
export { container }
示例6: Container
//Game
bind<IGame>(TYPES.Game).to(Game).inSingletonScope();
bind<LoadSessionState>(TYPES.LoadSessionState).to(LoadSessionState);
bind<LoadAudioState>(TYPES.LoadAudioState).to(LoadAudioState);
bind<IntroState>(TYPES.IntroState).to(IntroState);
bind<StageState>(TYPES.StageState).to(StageState);
bind<EntityManager>(TYPES.EntityManager).to(EntityManager).inSingletonScope();
bind<Player>(TYPES.Player).to(Player).inSingletonScope();
bind<PlayerState>(TYPES.PlayerState).to(PlayerState).inSingletonScope();
bind<IPianoKey>(TYPES.PianoKey).to(PianoKey);
bind<Piano>(TYPES.Piano).to(Piano);
bind<interfaces.Newable<IPianoKey>>(TYPES.NewablePianoKey).toConstructor<IPianoKey>(PianoKey);
bind<interfaces.Newable<IColor>>(TYPES.NewableColor).toConstructor<IColor>(Color);
bind<interfaces.Newable<ICssColor>>(TYPES.NewableCssColor).toConstructor<ICssColor>(CssColor);
bind<interfaces.Newable<IVector2d>>(TYPES.NewableVector2d).toConstructor<IVector2d>(Vector2d);
bind<Set<IProjectile>>(TYPES.Projectiles).toConstantValue(projectiles);
bind<Set<IItem>>(TYPES.Items).toConstantValue(items);
bind<Set<IEnemy>>(TYPES.Enemies).toConstantValue(enemies);
bind<Set<IBoss>>(TYPES.Bosses).toConstantValue(bosses);
bind<Array<IPianoKey>>(TYPES.PianoKeys).toConstantValue(pianoKeys);
//Audio
bind<Audio>(TYPES.Audio).to(Audio).inSingletonScope();
});
const container = new Container();
container.load(thirdPartyDependencies, applicationDependencies);
export { container };
示例7: Container
import getDecorators from "inversify-inject-decorators";
import { Container } from 'inversify';
const ioc: Container = new Container();
const decorators = getDecorators(ioc);
const inject = decorators.lazyInject;
import { iocModule as commonIocModule } from '../ui/common/iocModule';
import { iocModule as todoIocModule } from '../ui/todo/iocModule';
// import { iocModule as todo2IocModule } from '../ui/todo2/iocModule';
ioc.load(
commonIocModule,
todoIocModule
);
// if (Math.random() >= 0.5) {
// ioc.load(todo2IocModule);
// }
export { ioc, inject };
示例8: DbConfig
import { Container } from 'inversify';
import { DbConfig, repositoryModule, TYPES as RepoTypes, ICounterRepository } from '@sample-stack/store';
import * as Hemera from 'nats-hemera';
import { pubsub, client as natsClient } from './pubsub';
import { TaggedType } from '@sample-stack/core';
import { database as DEFAULT_DB_CONFIG } from '../../../../config/development/settings.json';
import { logger } from '@sample-stack/utils';
const dbConfig = new DbConfig(DEFAULT_DB_CONFIG);
let counterRepo;
try {
let container = new Container();
container.load(repositoryModule(dbConfig));
logger.info('Running in environment : [%s]', process.env.NODE_ENV);
if (process.env.NODE_ENV === 'development') {
// development
counterRepo = container.get<ICounterRepository>(RepoTypes.ICounterRepository);
} else {
// all other environment
const hemera = new Hemera(natsClient, {
logLevel: process.env.HEMERA_LOG_LEVEL as Hemera.LogLevel || 'info',
childLogger: true,
tag: 'hemera-server',
timeout: 10000,
});
container.bind('Hemera').toConstantValue(hemera);
counterRepo = container.getNamed<ICounterRepository>(RepoTypes.ICounterRepository, TaggedType.MICROSERVICE);
}
} catch (err) {
logger.error('Server start failed when building the containers');