Skip to content

Commit 3c9d9c1

Browse files
author
Thiago Bustamante
committed
support to configure constants from files
1 parent cf12c3c commit 3c9d9c1

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ For example, you can create the ```ioc.config.ts``` file:
432432
```typescript
433433
import { MyType, MyTypeImpl, MyType2, MyType2Factory } from './my-types';
434434
import { Scope } from 'typescript-ioc';
435+
import * as yaml from 'js-yaml';
436+
import * as fs from 'fs';
437+
438+
const config = yaml.safeLoad(fs.readFileSync(configFileName, 'utf8'));
435439

436440
export default [
437441
{ bind: MyType, to: MyTypeImpl },
@@ -440,7 +444,8 @@ export default [
440444
factory: MyType2Factory,
441445
withParams: [Date],
442446
scope: Scope.Singleton
443-
}
447+
},
448+
{ bindName: 'config', to: config }
444449
];
445450

446451
```

src/model.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ export interface ContainerConfiguration {
129129
withParams?: Array<any>;
130130
}
131131

132+
/**
133+
* A Container constant configuration
134+
*/
135+
export interface ConstantConfiguration {
136+
/**
137+
* The constant name used to refer the constant in the container
138+
*/
139+
bindName: string;
140+
/**
141+
* The constant value
142+
*/
143+
to?: any;
144+
}
145+
146+
132147
/**
133148
* A Configuration Snapshot. Store the state for a specified binding.
134149
* Can then be restored later. Useful for testing.

src/typescript-ioc.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import 'reflect-metadata';
8-
import { Config, ValueConfig, ObjectFactory, Scope, ContainerConfiguration, Snapshot, BuildContext } from './model';
8+
import { Config, ValueConfig, ObjectFactory, Scope, ContainerConfiguration, ConstantConfiguration, Snapshot, BuildContext } from './model';
99
import { IoCContainer } from './container/container';
1010
import { LocalScope, SingletonScope, RequestScope } from './scopes';
1111

@@ -15,6 +15,7 @@ export { ObjectFactory };
1515
export { BuildContext };
1616
export { Scope };
1717
export { ContainerConfiguration };
18+
export { ConstantConfiguration };
1819
export { Inject, Factory, Singleton, Scoped, OnlyInstantiableByContainer, InRequestScope } from './decorators';
1920
export { Snapshot };
2021

@@ -93,24 +94,42 @@ export class Container {
9394
* Import an array of configurations to the Container
9495
* @param configurations
9596
*/
96-
public static configure(...configurations: Array<ContainerConfiguration>) {
97+
public static configure(...configurations: Array<ContainerConfiguration | ConstantConfiguration>) {
9798
configurations.forEach(config => {
98-
const bind = IoCContainer.bind(config.bind);
99-
if (bind) {
100-
if (config.to) {
101-
bind.to(config.to);
102-
} else if (config.factory) {
103-
bind.factory(config.factory);
104-
}
105-
if (config.scope) {
106-
bind.scope(config.scope);
107-
}
108-
if (config.withParams) {
109-
bind.withParams(config.withParams);
110-
}
99+
if ((config as ContainerConfiguration).bind) {
100+
Container.configureType(config as ContainerConfiguration);
101+
} else if ((config as ConstantConfiguration).bindName) {
102+
Container.configureConstant(config as ConstantConfiguration);
111103
}
112104
});
113105
}
106+
107+
private static configureConstant(config: ConstantConfiguration) {
108+
const bind = IoCContainer.bindName(config.bindName);
109+
if (bind) {
110+
if (config.to) {
111+
bind.to(config.to);
112+
}
113+
}
114+
}
115+
116+
private static configureType(config: ContainerConfiguration) {
117+
const bind = IoCContainer.bind(config.bind);
118+
if (bind) {
119+
if (config.to) {
120+
bind.to(config.to);
121+
}
122+
else if (config.factory) {
123+
bind.factory(config.factory);
124+
}
125+
if (config.scope) {
126+
bind.scope(config.scope);
127+
}
128+
if (config.withParams) {
129+
bind.withParams(config.withParams);
130+
}
131+
}
132+
}
114133
}
115134

116135
class ContainerBuildContext extends BuildContext {

test/unit/typescript-ioc.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
import { IoCContainer } from '../../src/container/container';
33
import { Container, Scope, Config, ObjectFactory } from '../../src/typescript-ioc';
4-
import { BuildContext } from '../../src/model';
4+
import { BuildContext, ValueConfig } from '../../src/model';
55

66
jest.mock('../../src/container/container');
77
const mockBind = IoCContainer.bind as jest.Mock;
8+
const mockBindName = IoCContainer.bindName as jest.Mock;
89
const mockGet = IoCContainer.get as jest.Mock;
910
const mockGetType = IoCContainer.getType as jest.Mock;
1011
const mockSnapshot = IoCContainer.snapshot as jest.Mock;
@@ -14,6 +15,7 @@ const mockFactory = jest.fn();
1415
const mockScope = jest.fn();
1516
const mockWithParams = jest.fn();
1617
let bindResult: Config;
18+
let bindNameResult: ValueConfig;
1719

1820
describe('Container', () => {
1921

@@ -24,7 +26,10 @@ describe('Container', () => {
2426
scope: mockScope,
2527
withParams: mockWithParams
2628
};
27-
mockBind.mockReturnValue(bindResult);
29+
bindNameResult = {
30+
to: mockTo
31+
};
32+
2833
});
2934

3035
beforeEach(() => {
@@ -35,6 +40,10 @@ describe('Container', () => {
3540
mockFactory.mockClear();
3641
mockScope.mockClear();
3742
mockWithParams.mockClear();
43+
mockBind.mockClear();
44+
mockBindName.mockClear();
45+
mockBind.mockReturnValue(bindResult);
46+
mockBindName.mockReturnValue(bindNameResult);
3847
});
3948

4049
class MyBaseType { }
@@ -106,5 +115,12 @@ describe('Container', () => {
106115

107116
expect(mockWithParams).toBeCalledWith(['param']);
108117
});
118+
119+
it('should configure constants in the IoC Container', () => {
120+
Container.configure({ bindName: 'myProp', to: 'a value' });
121+
122+
expect(mockBindName).toBeCalledWith('myProp');
123+
expect(mockTo).toBeCalledWith('a value');
124+
});
109125
});
110126
});

0 commit comments

Comments
 (0)