1.0.3 • Published 5 years ago

@smertos/tydi v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

typescriptDI

npm version why not

tydi is a simple Dependency Injection helper, that adds 2 simple decorators for easy property/constructor parameters injection of decorated classes or any other kind of values using assigned custom keys.

Example

First you'll need to define some service.

import { Service } from '@smertos/tydi';

@Service()
class AuthService {
	
    isLoggedIn(): boolean {
    	return ...;
    }
    	
}

Then somewhere you need this service, you'll need to inject the service.

import { Container, Inject } from '@smertos/tydi';
import { AuthService } from '...';

class LoginComponent {
	
    @Inject() authService: AuthService;
    
    logIn(): void {
    	if (this.authService.isLoggedIn) return;
        
        ...
    }

}

If you do not manage instantiation of class (i.e. React components) and do not pass any arguments, then you can use constructor parameter injection (works like in Angular 2+). Access modificators can be used with injected parameters without any problems.

class LoginComponent {
	
    constructor(private authService: AuthService) {}
    
    logIn(): void {
    	if (this.authService.isLoggedIn) return;
        
        ...
    }

}

If you wish to do instantiation yourself as well, as pass parameters to constructor, you can do it as it is supported, but TypeScript may think about it otherwise.

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago