All Projects → zhouhoujun → tsioc

zhouhoujun / tsioc

Licence: MIT License
AOP, Ioc container, Boot framework, unit testing framework , activities workflow framework.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to tsioc

React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+786.67%)
Mutual labels:  ioc, decorators, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+2746.67%)
Mutual labels:  ioc, decorators, ioc-container
Summer
这是一个支持分布式和集群的java游戏服务器框架,可用于开发棋牌、回合制等游戏。基于netty实现高性能通讯,支持tcp、http、websocket等协议。支持消息加解密、攻击拦截、黑白名单机制。封装了redis缓存、mysql数据库的连接与使用。轻量级,便于上手。
Stars: ✭ 336 (+2140%)
Mutual labels:  ioc, ioc-container, aop
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+1040%)
Mutual labels:  ioc, decorators, ioc-container
VCore
VCore is a Swift collection containing objects, functions, and extensions that I use for my projects
Stars: ✭ 32 (+113.33%)
Mutual labels:  unittest, unit-test
thinking-in-spring
Spring source code reading
Stars: ✭ 105 (+600%)
Mutual labels:  ioc, aop
TestIt
Generate unit testing boilerplate from kotlin files.
Stars: ✭ 32 (+113.33%)
Mutual labels:  unittest, unit-test
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (+500%)
Mutual labels:  ioc, decorators
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+15566.67%)
Mutual labels:  ioc, decorators
SwiftInjection
Dependency Injection framework for Swift
Stars: ✭ 21 (+40%)
Mutual labels:  ioc, ioc-container
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (+226.67%)
Mutual labels:  ioc, ioc-container
containerx
⚡简单的Java依赖注入框架,代码量少,实现了依赖注入和AOP。适合Spring源码的初学者掌握其核心原理
Stars: ✭ 76 (+406.67%)
Mutual labels:  ioc, ioc-container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+46.67%)
Mutual labels:  ioc, ioc-container
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+1653.33%)
Mutual labels:  ioc, ioc-container
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (+140%)
Mutual labels:  ioc, ioc-container
unittest expander
A library that provides flexible and easy-to-use tools to parameterize Python unit tests, especially those based on unittest.TestCase.
Stars: ✭ 12 (-20%)
Mutual labels:  unittest, unit-test
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (+100%)
Mutual labels:  ioc, ioc-container
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+100%)
Mutual labels:  ioc, aop
AppListManager
📱 AppListManager (Android Library) makes managing application and activity lists easy.
Stars: ✭ 59 (+293.33%)
Mutual labels:  activity, activities
boot-bundle
boot-bundle: managed dependencies for boot, the clojure build tool
Stars: ✭ 43 (+186.67%)
Mutual labels:  boot, build-tool

packaged @tsdi/ioc

This repo is for distribution on npm. The source for this module is in the main repo.

@tsdi/ioc is Ioc container, Injector, via typescript decorator.

version 5+ of @ts-ioc/core tsioc

builder

build: npm run build

//build with version:
npm run build -- --setvs=4.0.0-beta

//deploy: 
./deploy.cmd
//or
 npm run build -- --deploy=true

Install

npm install @tsdi/ioc

add extends modules

use aop

// install aop
npm install @tsdi/aop
import { AopModule } from '@tsdi/aop';
import { IocContainer } from '@tsdi/ioc';

let container = new IocContainer();

// use aop.
container.use(AopModule);
// also can
container.register(AopModule);
// or
container.inject(AopModule)
//or
container.injectModule(AopModule)

use aop logs

// install aop logs
npm install @tsdi/logs
import { LogModule } from '@tsdi/logs';
// in server
import { ContainerBuilder } from '@tsdi/platform-server'
// in browser
import { ContainerBuilder } from '@tsdi/platform-browser'

let builder = new ContainerBuilder();

let container = build.create();

container.use(LogModule);

Documentation

core

extends ioc

  1. @IocExt class decortator, use to define the class is Ioc extends module. it will auto run after registered to helper your to setup module.
  2. add service resolve.
  3. module inject.

Ioc

  1. Register one class will auto register depdence class (must has a class decorator).

  2. get Instance can auto create constructor param. (must has a class decorator or register in container).

decorators

  1. @Abstract abstract class decorator.
  2. @AutoRun class, method decorator, use to define the class auto run (via a method or not) after registered.
  3. @AutoWried property or param decorator, use to auto wried type instance or value to the instance of one class with the decorator.
  4. @Inject property or param decorator, use to auto wried type instance or value to the instance of one class with the decorator.
  5. @Injectable class decortator, use to define the class. it can setting provider to some token, singleton or not.
  6. @AutoWried method decorator.
  7. @Param param decorator, use to auto wried type instance or value to the instance of one class with the decorator.
  8. @Singleton class decortator, use to define the class is singleton.
  9. @Providers Providers decorator, for class. use to add private ref service for the class.
  10. @Refs Refs decorator, for class. use to define the class as a service for target.

AOP

It's a dynamic aop base on ioc.

define a Aspect class, must with decorator:

  • @Aspect Aspect decorator, define for class. use to define class as aspect. it can setting provider to some token, singleton or not.

  • @Before(matchstring|RegExp) method decorator, aop Before advice decorator.

  • @After(matchstring|RegExp) method decorator, aop after advice decorator.

  • @Around(matchstring|RegExp) method decorator, aop around advice decorator.

  • @AfterThrowing(matchstring|RegExp) method decorator, aop AfterThrowing advice decorator.

  • @AfterReturning(matchstring|RegExp) method decorator, aop AfterReturning advice decorator.

  • @Pointcut(matchstring|RegExp) method decorator, aop Pointcut advice decorator.

see simples

boot

DI Module manager, application bootstrap. base on AOP.

  • @DIModule DIModule decorator, use to define class as DI Module.
  • @Bootstrap Bootstrap decorator, use to define class as bootstrp module.
  • @Annotation Annotation decorator, use to define class build metadata config.
  • @Message Message decorator, for class. use to define the class as message handle register in global message queue.

mvc boot simple

import { DIModule, BootApplication } from '@tsdi/boot';


export class TestService {
    testFiled = 'test';
    test() {
        console.log('test');
    }
}

@DIModule({
    providers: [
        { provide: 'mark', useFactory: () => 'marked' },
        TestService
    ],
    exports: [

    ]
})
export class ModuleA {

}

@Injectable
export class ClassSevice {
    @Inject('mark')
    mark: string;
    state: string;
    start() {
        console.log(this.mark);
    }
}

@Aspect
export class Logger {

    @Around('execution(*.start)')
    log() {
        console.log('start........');
    }
}


@DIModule({
    imports: [
        AopModule,
        ModuleA
    ],
    providers:[
        Logger,
        ClassSevice
    ]
    bootstrap: ClassSevice
})
export class ModuleB {

}

BootApplication.run(ModuleB);

components

  • @Component Component decorator, use to defaine class as component with template.
  • @Input Input decorator, use to define property or param as component binding field or args.

see activity build boot simple

Activites

create Container

let container = new IocContainer();

Container is ioc root.

see @tsdi/ioc interface IIocContainer

see @tsdi/core interface IContainer

// 1. register a class
container.register(Person);

// 2. register a factory;
container.register(Person, (container)=> {
    ...
    return new Person(...);
});

// 3. register with keyword
container.register('keyword', Perosn);

// 4. register with alais
container.register(new Registration(Person, aliasname));

// register singleton
container.registerSingleton(Person)

// bind provider
container.bindProvider
// bind providers.
container.bindProviders

Injector is basic ioc injector.

see interface IInjector

// get the injector of Person class type injected.
let injector =  container.getInjector(Person);

// create new injector
let injector = container.createInjector();
// or create new injector via
let injector = container.getInstance(InjectorFactoryToken);

Invoke method

you can use yourself MethodAccessor by implement IMethodAccessor, register MethodAccessorToken with your MethodAccessor in container, see interface.

@Injectable
class Person {
    constructor() {

    }
    say() {
        return 'I love you.'
    }
}

@Injectable
class Child extends Person {
    constructor() {
        super();
    }
    say() {
        return 'Mama';
    }
}

class MethodTest {
    constructor() {

    }

    @AutoWried
    sayHello(person: Person) {
        return person.say();
    }
}

class MethodTest2 {
    constructor() {

    }

    @AutoWried()
    sayHello( @Inject(Child) person: Person) {
        return person.say();
    }
}

class MethodTest3 {
    constructor() {

    }

    @AutoWried
    sayHello( @Inject(Child) personA: Person, personB: Person) {
        return personA.say() + ', '  + personB.say();
    }
}

@Injectable
class Geet {
    constructor(private name: string){

    }

    print(hi?:string){
        return `${hi}, from ${this.name}`;
    }
}

container.register(Geet);

container.invoke(Geet, gt=> gt.print, ... //inject providers.)

let instance = container.resolve(Geet, )

container.invoke(instance, gt=> gt.print, ...//inject providers.);
container.invoke(instance, 'print', ...);

container.register(MethodTest);
container.invoke(MethodTest, 'sayHello');

container.register(MethodTest2);
container.invoke(MethodTest2, tg=> tg.sayHello);

container.register(MethodTest3);
container.invoke(MethodTest3, 'sayHello');

Use Demo

import { Method, ContainerBuilder, AutoWired, Injectable, Singleton, IContainer, ParameterMetadata, Param, Aspect } from '@tsdi/core';


export class SimppleAutoWried {
    constructor() {
    }

    @AutoWired
    dateProperty: Date;
}

@Singleton
export class Person {
    name = 'testor';
}
// > v0.3.5 all class decorator can depdence.
@Singleton
// @Injectable
export class RoomService {
    constructor() {

    }
    @AutoWired
    current: Date;
}

@Injectable()
export class ClassRoom {
    constructor(public service: RoomService) {

    }
}

export abstract class Student {
    constructor() {
    }
    abstract sayHi(): string;
}

@Injectable({ provide: Student })
export class MiddleSchoolStudent extends Student {
    constructor() {
        super();
    }
    sayHi() {
        return 'I am a middle school student';
    }
}

@Injectable()
export class MClassRoom {
    @AutoWired(MiddleSchoolStudent)
    leader: Student;
    constructor() {

    }
}


@Injectable({ provide: Student, alias: 'college' })
export class CollegeStudent extends Student {
    constructor() {
        super();
    }
    sayHi() {
        return 'I am a college student';
    }
}

@Injectable
export class CollegeClassRoom {
    constructor(
        @Param(CollegeStudent)
        @AutoWired(CollegeStudent)
        public leader: Student) {

    }
}


@Injectable
export class InjMClassRoom {
    // @Inject(MiddleSchoolStudent)
    @Inject
    // @Inject({ type: MiddleSchoolStudent })
    // @Inject({ provider: MiddleSchoolStudent })
    leader: Student;
    constructor() {

    }
}


export interface IClassRoom {
    leader: Student;
}

@Injectable
export class InjCollegeClassRoom {
    constructor(
        // all below decorator can work, also @AutoWired, @Param is.
        // @Inject(new Registration(Student, 'college')) // need CollegeStudent also register.
        @Inject(CollegeStudent)
        // @Inject({ provider: CollegeStudent })
        // @Inject({ provider: Student, alias: 'college' }) //need CollegeStudent also register.
        // @Inject({ type: CollegeStudent })
        public leader: Student
    ) {

    }
}

@Injectable
export class InjCollegeAliasClassRoom {
    constructor(
        // all below decorator can work, also @AutoWired, @Param is.
        @Inject(new Registration(Student, 'college')) // need CollegeStudent also register.
        // @Inject(CollegeStudent)
        // @Inject({ provider: CollegeStudent })
        // @Inject({ provider: Student, alias: 'college' }) // need CollegeStudent also register.
        // @Inject({ type: CollegeStudent })
        public leader: Student
    ) {

    }
}


@Injectable('StringClassRoom')
export class StingMClassRoom {
    // @Inject(MiddleSchoolStudent)
    @Inject
    // @Inject({ type: MiddleSchoolStudent })
    leader: Student;
    constructor() {

    }
}

export class StringIdTest {
    constructor(@Inject('StringClassRoom') public room: IClassRoom) {

    }
}

export const CollClassRoom = Symbol('CollegeClassRoom');

@Injectable(CollClassRoom)
export class SymbolCollegeClassRoom {

    @Inject(CollegeStudent)
    leader: Student;
    constructor() {

    }
}

export class SymbolIdest {
    @Inject(CollClassRoom)
    public room: IClassRoom
    constructor() {

    }
}

@Injectable
class MethodTestPerson {
    say() {
        return 'hello word.'
    }
}

class MethodTest {

    @AutoWried
    sayHello(person: MethodTestPerson) {
        return person.say();
    }
}


// 1. Custom register one class will auto inject depdence class (must has a class decorator).

let builder = new ContainerBuilder();
let container = builder.create();


container.register(MethodTest);
container.invoke(MethodTest, 'sayHello')
    .then(data =>{
        console.log(data);
    });

container.register(SimppleAutoWried);
let instance = container.get(SimppleAutoWried);
console.log(instance.dateProperty);


container.register(ClassRoom);
let room = container.get(ClassRoom);
console.log(room.service.current);

container.register(MiddleSchoolStudent);
container.register(CollegeStudent);

let student = container.get(Student);
console.log(student.sayHi());

let student2 = container.get(new Registration(Student, 'college'));

console.log(student2.sayHi());

let student3 = container.get(Student, 'college'));

console.log(student3.sayHi());


builder.build({
    files: __dirname + '/*{.ts,.js}'
})
    .then(container => {
        let instance = container.get(Student);
        console.log(instance.sayHi());

        let instance2 = container.get(new Registration(Student, 'college'));
        console.log(instance2.sayHi());

        let instance3 = container.get(Student, 'college');
        console.log(instance3.sayHi())
    });

Extend decorator, Demo fo extends

see AOP extends (https://github.com/zhouhoujun/tsioc/blob/master/packages/aop/src/AopModule.ts) You can extend yourself decorator via:

Documentation

Documentation is available on the

License

MIT © Houjun

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].