Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove getContainer #142

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ArtusLogger, Logger } from './logger';

export class ArtusApplication implements Application {
public manifest?: Manifest;
public container: Container;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 container 其实可以和 execution container 一样设置进容器:

this.container.set({id: Container, value: this.container});

不需要作为 public 属性对外使用,这样外部想用可以直接 Inject

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 进容器的改动已经做在 feat: load default class early #139 里面了
  2. 对于上层框架来说,还是有可能会用到 app.contianer,就像现在广泛使用了的 app.getContainer() 和 ctx.container,即然管不住还不如放开,推荐 Inject 就是了


protected container: Container;
protected lifecycleManager: LifecycleManager;
protected loaderFactory: LoaderFactory;
protected defaultClazzLoaded = false;
Expand Down Expand Up @@ -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 });
Expand Down
6 changes: 5 additions & 1 deletion src/trigger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export default class Trigger implements TriggerType {

async initContext(input: Input = new Input(), output = new Output()): Promise<Context> {
const ctx = new Context(input, output);
ctx.container = new ExecutionContainer(ctx, this.app.getContainer());
ctx.container = new ExecutionContainer(ctx, this.app.container);
ctx.container.set({
id: ExecutionContainer,
value: ctx.container,
});
return ctx;
}

Expand Down
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface ApplicationInitOptions {
}

export interface Application {
container: Container;

manifest?: Manifest;
config?: Record<string, any>;

Expand All @@ -25,9 +27,6 @@ export interface Application {
load(manifest: Manifest): Promise<this>;
run(): Promise<void>;
registerHook(hookName: string, hookFn: HookFunction): void;

// 兜底方法,不建议使用
getContainer(): Container;
}

export interface TriggerType {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/artus_application/src/controller/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/artus_application/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class MyArtusApplication {
static async instance(manifest: Manifest): Promise<MyArtusApplication> {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
12 changes: 6 additions & 6 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface AppConfigWithLoggerOptions extends Record<string, any> {
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,
});
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down