packaged tsioc
This repo is for distribution on npm
. The source for this module is in the
main repo.
tsioc
is AOP, Ioc container, via typescript decorator.
now package rename as tsioc
Install
npm install tsioc
Documentation
class name First char must be UpperCase.
Ioc
-
Register one class will auto register depdence class (must has a class decorator).
-
get Instance can auto create constructor param. (must has a class decorator or register in container).
create Container
- in browser can not:
- use syncBuild
- syncLoadModule
- can not use minimatch to match file.
- support es5 uglify, use typescript-class-annotations to get class annotations before typescript compile.
; // 1. via create.; // 2. via build.//with BuildOptions to auto register module.; // 3. via syncBuild;
init & register Container
see interface IContainer
// 1. you can load modules by selfawait builder.loadModulecontainer, ;// 2. load syncbuilder.syncLoadModulecontainer, ; // 3. register a classcontainer.registerPerson; // 4. register a factory;container.registerPerson,; // 5. register with keywordcontainer.register'keyword', Perosn; // 7. register with alaiscontainer.registernew RegistrationPerson, aliasname;
get instance of type
// 8. get instance use get method of container./** * Retrieves an instance from the container based on the provided token. * * @template T * @param * @param * @returns * @memberof IContainer */gettoken: Token<T>, alias?: string: T; /** * resolve type instance with token and param provider. * * @template T * @param * @param * @returns * @memberof IContainer */resolvetoken: Token<T>, ...providers: Providers: T; //get simple person;//get colloge person; // resolve with providerscontainer.resolvePerson, ...providers;
Invoke method
you can use yourself MethodAccessor
by implement IMethodAccessor, register symbols.IMethodAccessor
with your MethodAccessor
in container, see interface IMethodAccessor.
container.registerGeet; container.invokeGeet, 'print', null,,, ... },,Provider.createParam'name', 'zhou',Provider.create'hi', value:'Hello',// or use ProviderMap.... container.resolveGeet,, }, container.registerMethodTest;container.invokeMethodTest, 'sayHello' .then; container.registerMethodTest2;container.invokeMethodTest2, 'sayHello' .then; container.registerMethodTest3;container.invokeMethodTest3, 'sayHello' .then;
AOP
It's a dynamic aop base on ioc.
define a Aspect class, must with decorator:
- @Aspect
define advice decorator have
-
@Before(matchstring|RegExp)
-
@After(matchstring|RegExp)
-
@Around(matchstring|RegExp)
-
@AfterThrowing(matchstring|RegExp)
-
@AfterReturning(matchstring|RegExp)
-
@Pointcut(matchstring|RegExp)
see simples
; ; // auth check simple. // Log simple
New Features
-
1.5.3
- refactor aop, add IAdvisorProceeding ... to control advices invoke way when target method return value with sync, async. can custom define invoke way.
- refactor Registration to support symbol keywords with desc
-
1.5.0
- refactor aop, add AdvisorChain to control advices invoke when target method invoking.
-
1.4.5
- refactor tsioc aop
- fix import bug for web debug.
-
1.4.3
- remove
log4js
adapter for fix browser load log4js error. - update task for uglify
tsioc.umd.js
smaller.
- remove
-
1.4.2
- fix bug, inherit sub class with
classAnnations
overrid error method params.
- fix bug, inherit sub class with
-
1.4.0
- add logger aspect, logger adapter and logger services.
-
1.3.18
- fix aop advice order bug.
- fix aop advice not match inherit method bug.
-
1.3.16
- set only with
@Component
decorator have bindCoreActions.componentBeforeInit, CoreActions.componentInit
actions. auto invokebeforeInit
,onInit
hooks
- set only with
-
1.3.15
- support es5 uglify. modify isClass check way to support uglify min js. use typescript-class-annotations to get class annotations before typescript compile.
-
1.3.14
- fix bug, object provider
{xxx:'MyClassName'}
string val equals to class alias will auto create Class instance. if want create the class instance, use Provider.create ...
- fix bug, object provider
-
1.3.13
- fix inherit sub class constructor and method param type and param name match error bug, confuse with the parent class param.
-
1.3.12
- add cache able features. Class metadata add
expires
to set class instance cache timeout when not used, viaCacheManager
. - implement
ComponentLifecycle
onDestory
hook for cache class instance. @Injectable
,@Component
and@Abstract
decorator have bindCoreActions.componentCache
actions.
- add cache able features. Class metadata add
-
1.3.11
- fix isNull type check bug.
- add
ComponentLifecycle
lifecycle hooks for decorator (the decorator had bind liefcycle action) class to invoke auto. now only implementbeforeInit
,onInit
hooks.
-
1.3.8
- fix bug of method params not match with providers when have not method decorator.
- fix function param can not invoke bug.
-
1.3.7
- fix error imports. remove unused code.
-
1.3.6
- refactor components, add generic type.
GComponent<T extends IComponent>
,GComposite<T extends IComponent>
for extends easy.
- refactor components, add generic type.
-
1.3.5
- fix bug inherit property not auto inject in browser. set param, method and property metadata will store all metadata include base class.
-
1.3.4
- class, method metadata only get own
- fix bug two dectorator match * infinite loop error, * only match class without @Aspcet.
-
1.3.3
- param provider name type match first. last with index order.
-
1.3.2
- refactor provider. improve to better code in browser.
- improve class check.
- add @Abstract decorator to declare Abstract class.
-
1.3.0
- refactor AOP. add Object instance entends action.
- support @annotation pointcut, add Pointcut decorator
- Aspect is not default singleton.
- add Pointcut decorator.
- Aspect can advice Aspect method or self pointcut method.
-
1.2.11
- improvement
hasPropertyMetadata
,hasMethodMetadata
has metadata check special propertyKey
- improvement
-
1.2.10
- add
getTokenImpl
inIContainer
, to get implement class Type for token. - method invoke can use token,
invoke<T>(token: Token<any>, propertyKey: string | symbol, target?: any, ...providers: Providers[]): Promise<T>; syncInvoke<T>(token: Token<any>, propertyKey: string | symbol, target?: any, ...providers: Providers[]): T;
- add
-
1.2.8
- fix bug inherit decorator metadata from base with same decorator.
- add
getOwnTypeMetadata
,getOwnMethodMetadata
,getOwnPropertyMetadata
,getOwnParamMetadata
. - add feature, inject property and bindProvider can resolve with providers.
-
1.2.6
- fix bug metadata covered, use same decorator on method, property and paramerter.
- add
hasClassMetadata
,hasMethodMetadata
,hasPropertyMetadata
andhasParamMetadata
to check if has decorator metadata.
-
1.2.5
- fix get invok when get method name.
- add get set advice aop.
-
1.2.4
- fix aop bugs.
- fix param name match bug.
-
1.2.2 fix @NonePointcut zip error in tsioc.umd.js
-
1.2.1
-
Refactor service provider, add ProviderMap, enable to config provider map options. eg. `container.invoke(Geet, 'print', null, {hi: 'How are you.', name:'zhou' }, { hi: (container: IContainer)=> 'How are you.' }, ... }, { hi:{type: Token, value: any |(container: IContainer)=>any }}, {index:'name', value:'zhou'}, {index:'hi', value:'Hello'} ... )
container.resolve(Geet, {name: 'zhou' }, { name: (container: IContainer)=>any } }, {name:{type: Token, value: any|(container: IContainer)=>any }})`
-
add @NonePointcut decorator for class, to skip Aop aspect advice work.
-
add @Component decorator for class.
-
-
1.2.0 remove unused
notFoundValue
param inget
,resolve
method in container. if not register the token will returnnull
. -
1.1.7
- add isBoolean isNull isDate check. fix zip error, uglify tsioc.umd.js.
-
1.1.3
- update Symbol Class Check, to support IE9+
IE9 or lower only check name of class first word is UpperCase.
-
1.1.2
- add custom MapSet to support browser without Map.
-
1.1.1
- refactor.
- support browser, use
bundles/tsioc.umd.js
.
-
0.6.21
- improvement method invoker, ParamProvider match name and index faild, will match provider via type of param is equal.
- improvement AOP advice invoker, add more param provider.
- have not register Type, container.get now will return null;
-
0.6.18
- complie src to es5, support in browser. fix class check bug in es5 model. class name First char must be UpperCase.
-
0.6.15
- add
resolve
. supportresolve
instance withproviders
.resolve<T>(token: Token<T>, ...providers: ParamProvider[]);
- add
createSyncParams(params: IParameter[], ...providers: ParamProvider[]): any[]
andcreateParams(params: IParameter[], ...providers: AsyncParamProvider[]): Promise<any[]>
- add
-
0.6.12
- support Method paramerter name opertor. Method Invoker ParamProvider can setting index as paramerter name.
Use Demo
; // > v0.3.5 all class decorator can depdence.// @Injectable ; // 1. Custom register one class will auto inject depdence class (must has a class decorator). ;; container.registerMethodTest;container.invokeMethodTest, 'sayHello' .then; container.registerSimppleAutoWried;;console.loginstance.dateProperty; container.registerClassRoom;;console.logroom.service.current; container.registerMiddleSchoolStudent;container.registerCollegeStudent; ;console.logstudent.sayHi; ; console.logstudent2.sayHi; ; console.logstudent3.sayHi; builder.build .then;
Extend decorator
see interface LifeScope You can extend yourself decorator via:
createClassDecorator
/** * create class decorator * * @export * @template T metadata type. * @param * @param * @param * @returns */
createClassMethodDecorator
/** * create method decorator. * * @export * @template T metadata type. * @param * @param * @param * @returns */
createClassMethodDecorator
/** * create decorator for class and method. * * @export * @template T * @param * @param * @param * @returns */
createParamDecorator
/** * create parameter decorator. * * @export * @template T metadata type. * @param * @param * @param * @returns */
createPropDecorator
/** * create property decorator. * * @export * @template T metadata type. * @param * @param * @param * @returns */
createParamPropDecorator
/** * create parameter or property decorator * * @export * @template T * @param * @param * @param * @returns */
createDecorator
/** * create dectorator for class params props methods. * * @export * @template T * @param * @param * @param * @returns */
Demo fo extends yourself decorator
//eg.// 1. create decorator; ; // 2. add decorator action ; ; lifeScope.addActionfactory.createAopActions.registAspect, DecoratorType.Class, IocState.design; // 3. register decoratorlifeScope.registerDecoratorAspect, AopActions.registAspect;
Container Interface
see more interface. all document is typescript .d.ts.
Documentation is available on the tsioc docs site.
License
MIT © Houjun