From 8abd861de5a002b6074cb835604519ee132217dd Mon Sep 17 00:00:00 2001 From: Ziheng Gao Date: Thu, 14 Jul 2022 18:38:29 +0800 Subject: [PATCH 1/2] refactor: remove all of getContainer --- src/application.ts | 7 +------ src/trigger/index.ts | 2 +- src/types.ts | 5 ++--- .../artus_application/src/controller/plugin.ts | 2 +- test/fixtures/artus_application/src/index.ts | 2 +- .../src/plugins/artus_plugin_mysql_ob/src/app.ts | 2 +- .../src/plugins/artus_plugin_mysql_rds/src/app.ts | 2 +- .../src/plugins/artus_plugin_redis/src/app.ts | 2 +- test/loader.test.ts | 2 +- test/logger.test.ts | 12 ++++++------ 10 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/application.ts b/src/application.ts index de6013e..5841420 100644 --- a/src/application.ts +++ b/src/application.ts @@ -11,8 +11,8 @@ import { ArtusLogger, Logger } from './logger'; export class ArtusApplication implements Application { public manifest?: Manifest; + public container: Container; - protected container: Container; protected lifecycleManager: LifecycleManager; protected loaderFactory: LoaderFactory; protected defaultClazzLoaded = false; @@ -54,11 +54,6 @@ export class ArtusApplication implements Application { return this.container.get(ConfigurationHandler); } - // 兜底方法,不建议对外部使用 - getContainer(): Container { - return this.container; - } - async loadDefaultClass() { // load Artus default clazz this.container.set({ id: ArtusInjectEnum.Application, value: this }); diff --git a/src/trigger/index.ts b/src/trigger/index.ts index 9ce7499..e76d41d 100644 --- a/src/trigger/index.ts +++ b/src/trigger/index.ts @@ -22,7 +22,7 @@ export default class Trigger implements TriggerType { async initContext(input: Input = new Input(), output = new Output()): Promise { const ctx = new Context(input, output); - ctx.container = new ExecutionContainer(ctx, this.app.getContainer()); + ctx.container = new ExecutionContainer(ctx, this.app.container); return ctx; } diff --git a/src/types.ts b/src/types.ts index e612d72..afd40ad 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,8 @@ export interface ApplicationInitOptions { } export interface Application { + container: Container; + manifest?: Manifest; config?: Record; @@ -25,9 +27,6 @@ export interface Application { load(manifest: Manifest): Promise; run(): Promise; registerHook(hookName: string, hookFn: HookFunction): void; - - // 兜底方法,不建议使用 - getContainer(): Container; } export interface TriggerType { diff --git a/test/fixtures/artus_application/src/controller/plugin.ts b/test/fixtures/artus_application/src/controller/plugin.ts index 39e2c51..5735895 100644 --- a/test/fixtures/artus_application/src/controller/plugin.ts +++ b/test/fixtures/artus_application/src/controller/plugin.ts @@ -27,7 +27,7 @@ export default class Hello { const app: ArtusApplication = ctx.input.params.app; let client; try { - client = app.getContainer().get('ARTUS_REDIS'); + client = app.container.get('ARTUS_REDIS'); } catch { } diff --git a/test/fixtures/artus_application/src/index.ts b/test/fixtures/artus_application/src/index.ts index 20a25eb..49b0aea 100644 --- a/test/fixtures/artus_application/src/index.ts +++ b/test/fixtures/artus_application/src/index.ts @@ -13,7 +13,7 @@ export default class MyArtusApplication { static async instance(manifest: Manifest): Promise { const app = new ArtusApplication(); await app.load(manifest, path.join(__dirname, '..')); - const instance = app.getContainer().get(MyArtusApplication); + const instance = app.container.get(MyArtusApplication); return instance; } diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts index 125707f..be128f0 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts @@ -16,7 +16,7 @@ export default class MyLifecycle implements ApplicationLifecycle { @LifecycleHook() async willReady() { - const mysql = this.app.getContainer().get('ARTUS_MYSQL') as Client; + const mysql = this.app.container.get('ARTUS_MYSQL') as Client; await mysql.init(this.app.config.mysql as MysqlConfig); } } diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts index 125707f..be128f0 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts @@ -16,7 +16,7 @@ export default class MyLifecycle implements ApplicationLifecycle { @LifecycleHook() async willReady() { - const mysql = this.app.getContainer().get('ARTUS_MYSQL') as Client; + const mysql = this.app.container.get('ARTUS_MYSQL') as Client; await mysql.init(this.app.config.mysql as MysqlConfig); } } diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts index 352edb0..72f1dba 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts @@ -16,7 +16,7 @@ export default class MyLifecycle implements ApplicationLifecycle { @LifecycleHook() async willReady() { - const redis = this.app.getContainer().get('ARTUS_REDIS') as Client; + const redis = this.app.container.get('ARTUS_REDIS') as Client; await redis.init(this.app.config.redis as RedisConfig); } } diff --git a/test/loader.test.ts b/test/loader.test.ts index defd345..c4c801b 100644 --- a/test/loader.test.ts +++ b/test/loader.test.ts @@ -63,7 +63,7 @@ describe('test/loader.test.ts', () => { describe('custom instance', () => { it('should not overide custom instance', async () => { const app = await createApp(); - expect(app.getContainer().get(Custom).getName()).toBe('foo'); + expect(app.container.get(Custom).getName()).toBe('foo'); }); }); }); diff --git a/test/logger.test.ts b/test/logger.test.ts index 525cb42..4f1c9af 100644 --- a/test/logger.test.ts +++ b/test/logger.test.ts @@ -12,7 +12,7 @@ interface AppConfigWithLoggerOptions extends Record { const _getAppWithConfig = async (config: AppConfigWithLoggerOptions = {}, manifest: Manifest = { items: [] }) => { const app = new ArtusApplication(); await app.load(manifest); - app.getContainer().set({ + app.container.set({ id: ArtusInjectEnum.Config, value: config, }); @@ -107,7 +107,7 @@ describe('test/logger.test.ts', () => { const { default: manifest } = await import('./fixtures/logger/src'); const app = await _getAppWithConfig({}, manifest); - const testClazz = app.getContainer().get(TestLoggerClazz); + const testClazz = app.container.get(TestLoggerClazz); testClazz.testTrace('trace', 0, {}); expect(console.trace).toBeCalledTimes(0); @@ -134,7 +134,7 @@ describe('test/logger.test.ts', () => { const { default: manifest } = await import('./fixtures/logger/src'); const app = await _getAppWithConfig({}, manifest); - const testClazz = app.getContainer().get(TestLoggerClazz); + const testClazz = app.container.get(TestLoggerClazz); testClazz.testLog(LoggerLevel.TRACE, 'trace', 0, {}); expect(console.trace).toBeCalledTimes(0); @@ -250,7 +250,7 @@ describe('test/logger.test.ts', () => { }, }, manifest); - const testClazz = app.getContainer().get(TestLoggerClazz); + const testClazz = app.container.get(TestLoggerClazz); testClazz.testTrace('trace', 0, {}); expect(console.trace).toBeCalledTimes(1); @@ -283,7 +283,7 @@ describe('test/logger.test.ts', () => { }, }, manifest); - const testClazz = app.getContainer().get(TestLoggerClazz); + const testClazz = app.container.get(TestLoggerClazz); testClazz.testLog(LoggerLevel.TRACE, 'trace', 0, {}); expect(console.trace).toBeCalledTimes(1); @@ -327,7 +327,7 @@ describe('test/logger.test.ts', () => { it('should log message with custom method', async () => { const { manifestWithCustomLogger: manifest } = await import('./fixtures/logger/src'); const app = await _getAppWithConfig({}, manifest); - const testClazz = app.getContainer().get(TestCustomLoggerClazz); + const testClazz = app.container.get(TestCustomLoggerClazz); app.logger.info('info', 0, {}); expect(console.info).toBeCalledTimes(1); From 57d256efe001b83fa8b5a7a0568dad3faf581862 Mon Sep 17 00:00:00 2001 From: Ziheng Gao Date: Thu, 14 Jul 2022 18:42:12 +0800 Subject: [PATCH 2/2] refactor(trigger): set ExecutionContainer to itself --- src/trigger/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/trigger/index.ts b/src/trigger/index.ts index e76d41d..71d231d 100644 --- a/src/trigger/index.ts +++ b/src/trigger/index.ts @@ -23,6 +23,10 @@ export default class Trigger implements TriggerType { async initContext(input: Input = new Input(), output = new Output()): Promise { const ctx = new Context(input, output); ctx.container = new ExecutionContainer(ctx, this.app.container); + ctx.container.set({ + id: ExecutionContainer, + value: ctx.container, + }); return ctx; }