Skip to content

Commit

Permalink
rename OnlyContainerCanInstantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Bustamante committed Feb 27, 2020
1 parent 83cd536 commit 88192ff
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It can be used on browser, on react native or on node.js server code.
- [Local Scope](#local-scope)
- [Custom Scopes](#custom-scopes)
- [@Factory](#factories)
- [@OnlyContainerCanInstantiate](#the-onlycontainercaninstantiate-annotation)
- [@OnlyInstantiableByContainer](#the-onlyinstantiablebycontainer-annotation)
- [@The Container Class](#the-container-class)
- [Creating temporary configurations](#creating-temporary-configurations)
- [Importing configurations from external file](#importing-configurations-from-external-file)
Expand Down Expand Up @@ -251,14 +251,14 @@ class PersonService {
}
```

## The @OnlyContainerCanInstantiate annotation
The @OnlyContainerCanInstantiate annotation transforms the annotated class, changing its constructor. So, it will only be able to create new instances for the decorated class through to the IoC Container.
## The @OnlyInstantiableByContainer annotation
The @OnlyInstantiableByContainer annotation transforms the annotated class, changing its constructor. So, it will only be able to create new instances for the decorated class through to the IoC Container.

It is usefull, for example, to avoid direct instantiation of Singletons.

```typescript
@Singleton
@OnlyContainerCanInstantiate
@OnlyInstantiableByContainer
class PersonService {
@Inject
private personDAO: PersonDAO;
Expand Down Expand Up @@ -302,12 +302,12 @@ let otherPersonDAO: PersonDAO = new PersonDAO();
```

```typescript
@OnlyContainerCanInstantiate
@OnlyInstantiableByContainer
@Singleton
class PersonDAO {
}

let p: PersonDAO = new PersonDAO(); // throws a TypeError. classes decorated with @OnlyContainerCanInstantiate can not be instantiated directly
let p: PersonDAO = new PersonDAO(); // throws a TypeError. classes decorated with @OnlyInstantiableByContainer can not be instantiated directly

const personFactory: ObjectFactory = () => new PersonDAO();
Container.bind(PersonDAO).factory(personFactory); //Works OK
Expand Down Expand Up @@ -548,9 +548,9 @@ If you need to support es5 code, you can keep using the 1.2.6 version

#### @AutoWired renamed

A lot of confusion with ```@AutoWired``` motivated us to rename it to ```@OnlyContainerCanInstantiate```. It is a big name, but it says exactly what that decorator does. It is completely optional (The container will always work in the same way when instrumenting the types), but it transforms the decorated constructor to avoid that anybody create new instances calling direct a new expression.
A lot of confusion with ```@AutoWired``` motivated us to rename it to ```@OnlyInstantiableByContainer```. It is a big name, but it says exactly what that decorator does. It is completely optional (The container will always work in the same way when instrumenting the types), but it transforms the decorated constructor to avoid that anybody create new instances calling direct a new expression.

So you need to change all references to ```@AutoWired``` to ```@OnlyContainerCanInstantiate```.
So you need to change all references to ```@AutoWired``` to ```@OnlyInstantiableByContainer```.

#### @Provided @Provides and Provider interface removed

Expand Down
4 changes: 2 additions & 2 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function Singleton(target: Function) {
* So, if you write:
*
* ```
* @ OnlyContainerCanInstantiate
* @ OnlyInstantiableByContainer
* class PersonService {
* @ Inject
* personDAO: PersonDAO;
Expand All @@ -63,7 +63,7 @@ export function Singleton(target: Function) {
* let PersonService = new PersonService(); // will thrown a TypeError exception
* ```
*/
export function OnlyContainerCanInstantiate(target: Function) {
export function OnlyInstantiableByContainer(target: Function) {
return IoCContainer.bind(target).instrumentConstructor().decoratedConstructor as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/typescript-ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Scope.Request = new RequestScope();

/**
* The IoC Container class. Can be used to register and to retrieve your dependencies.
* You can also use de decorators [[OnlyContainerCanInstantiate]], [[Scoped]], [[Singleton]], [[Factory]]
* You can also use de decorators [[OnlyInstantiableByContainer]], [[Scoped]], [[Singleton]], [[Factory]]
* to configure the dependency directly on the class.
*/
export class Container {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/ioc-container-tests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { Container, Inject, Scoped, Scope, ObjectFactory, Singleton, Factory } from '../../src/typescript-ioc';
import { OnlyContainerCanInstantiate, InRequestScope } from '../../src/decorators';
import { OnlyInstantiableByContainer, InRequestScope } from '../../src/decorators';

describe('@Inject annotation on a property', () => {

Expand Down Expand Up @@ -414,9 +414,9 @@ describe('The IoC Container.snapshot(source)', () => {
});
});

describe('@OnlyContainerCanInstantiate decorator', () => {
describe('@OnlyInstantiableByContainer decorator', () => {

@OnlyContainerCanInstantiate
@OnlyInstantiableByContainer
@Singleton
class SingletonInstantiation {
public static countInstances = 0;
Expand All @@ -427,7 +427,7 @@ describe('@OnlyContainerCanInstantiate decorator', () => {
}
}

@OnlyContainerCanInstantiate
@OnlyInstantiableByContainer
class LocalInstantiation {
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { IoCContainer } from '../../src/container/container';
import { Inject, Config, Singleton, Scope, Scoped, Factory } from '../../src/typescript-ioc';
import { OnlyContainerCanInstantiate } from '../../src/decorators';
import { OnlyInstantiableByContainer } from '../../src/decorators';

jest.mock('../../src/container/container');
const mockInjectProperty = IoCContainer.injectProperty as jest.Mock;
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('@Factory decorator', () => {
});
});

describe('@OnlyContainerCanInstantiate decorator', () => {
describe('@OnlyInstantiableByContainer decorator', () => {

beforeEach(() => {
mockBind.mockClear();
Expand All @@ -135,7 +135,7 @@ describe('@OnlyContainerCanInstantiate decorator', () => {
mockBind.mockReturnValue(bind);
class WiredInject { }

expect(OnlyContainerCanInstantiate(WiredInject)).toEqual(constructor);
expect(OnlyInstantiableByContainer(WiredInject)).toEqual(constructor);
expect(mockBind).toBeCalledWith(WiredInject);
expect(mockInstrumentConstructor).toBeCalled;
});
Expand Down

0 comments on commit 88192ff

Please sign in to comment.