From 88192ff5fd02d13fa3629eb5294608507ffa0a5f Mon Sep 17 00:00:00 2001 From: Thiago Bustamante Date: Thu, 27 Feb 2020 08:53:15 -0300 Subject: [PATCH] rename OnlyContainerCanInstantiate --- README.md | 16 ++++++++-------- src/decorators.ts | 4 ++-- src/typescript-ioc.ts | 2 +- test/integration/ioc-container-tests.spec.ts | 8 ++++---- test/unit/decorators.spec.ts | 6 +++--- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a90eb48..7835ca5 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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; @@ -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 @@ -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 diff --git a/src/decorators.ts b/src/decorators.ts index 05b38db..cfd5e97 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -50,7 +50,7 @@ export function Singleton(target: Function) { * So, if you write: * * ``` - * @ OnlyContainerCanInstantiate + * @ OnlyInstantiableByContainer * class PersonService { * @ Inject * personDAO: PersonDAO; @@ -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; } diff --git a/src/typescript-ioc.ts b/src/typescript-ioc.ts index 7295e39..dd1195f 100644 --- a/src/typescript-ioc.ts +++ b/src/typescript-ioc.ts @@ -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 { diff --git a/test/integration/ioc-container-tests.spec.ts b/test/integration/ioc-container-tests.spec.ts index 2bba651..e2939c9 100644 --- a/test/integration/ioc-container-tests.spec.ts +++ b/test/integration/ioc-container-tests.spec.ts @@ -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', () => { @@ -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; @@ -427,7 +427,7 @@ describe('@OnlyContainerCanInstantiate decorator', () => { } } - @OnlyContainerCanInstantiate + @OnlyInstantiableByContainer class LocalInstantiation { } diff --git a/test/unit/decorators.spec.ts b/test/unit/decorators.spec.ts index 0a2cadd..b5ffe06 100644 --- a/test/unit/decorators.spec.ts +++ b/test/unit/decorators.spec.ts @@ -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; @@ -119,7 +119,7 @@ describe('@Factory decorator', () => { }); }); -describe('@OnlyContainerCanInstantiate decorator', () => { +describe('@OnlyInstantiableByContainer decorator', () => { beforeEach(() => { mockBind.mockClear(); @@ -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; });