Skip to content

Commit 7482fd8

Browse files
authored
Merge pull request #52 from dolittle/use-execution-context-in-container
Pass along ExecutionContext to Container for DI
2 parents 2fa0fe3 + 972907a commit 7482fd8

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

Source/common/IContainer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Dolittle. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
import { ExecutionContext } from '@dolittle/sdk.execution';
45
import { Constructor } from '@dolittle/types';
56

67
/**
@@ -10,8 +11,9 @@ export abstract class IContainer {
1011

1112
/**
1213
* Get the instance of a service.
13-
* @param {Function} service Type of service by its constructor to get
14+
* @param {Constructor} service Type of service by its constructor to get
15+
* @param {ExecutionContext} executionContext The current execution context to use for resolving tenant-dependant services.
1416
* @returns The instance.
1517
*/
16-
abstract get (service: Constructor): any;
18+
abstract get (service: Constructor, executionContext: ExecutionContext): any;
1719
}

Source/common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"lint:fix": "gulp lint-fix --gulpfile ../../node_modules/@dolittle/typescript.build/Gulpfile.js"
3333
},
3434
"dependencies": {
35-
"@dolittle/types": "5.0.1"
35+
"@dolittle/types": "5.0.1",
36+
"@dolittle/sdk.execution": "14.3.2"
3637
},
3738
"devDependencies": {}
3839
}

Source/events.handling/Builder/EventHandlerClassBuilder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import { ICanBuildAndRegisterAnEventHandler } from './ICanBuildAndRegisterAnEven
2323

2424
export class EventHandlerClassBuilder<T> extends ICanBuildAndRegisterAnEventHandler {
2525
private readonly _eventHandlerType: Constructor<T>;
26-
private readonly _getInstance: (container: IContainer) => T;
26+
private readonly _getInstance: (container: IContainer, executionContext: ExecutionContext) => T;
2727

2828
constructor(typeOrInstance: Constructor<T> | T) {
2929
super();
3030
if (typeOrInstance instanceof Function) {
3131
this._eventHandlerType = typeOrInstance;
32-
this._getInstance = container => container.get(typeOrInstance);
32+
this._getInstance = (container, executionContext) => container.get(typeOrInstance, executionContext);
3333

3434
} else {
3535
this._eventHandlerType = Object.getPrototypeOf(typeOrInstance).constructor;
3636
if (this._eventHandlerType === undefined) {
3737
throw new CannotRegisterEventHandlerThatIsNotAClass(typeOrInstance);
3838
}
39-
this._getInstance = _ => typeOrInstance;
39+
this._getInstance = () => typeOrInstance;
4040
}
4141
}
4242

@@ -104,7 +104,7 @@ export class EventHandlerClassBuilder<T> extends ICanBuildAndRegisterAnEventHand
104104
return (event, eventContext) => {
105105
let instance: T;
106106
try {
107-
instance = this._getInstance(container);
107+
instance = this._getInstance(container, eventContext.executionContext);
108108
} catch (ex) {
109109
throw new CouldNotCreateInstanceOfEventHandler(this._eventHandlerType, ex);
110110
}

0 commit comments

Comments
 (0)