Refactor DI container for better structure and clarity
- Moved `TSinjex` class to `classes` directory for better organization. - Updated imports across the codebase to reflect the new location of `TSinjex`. - Introduced `Identifier` type to standardize dependency identifiers. - Enhanced JSDoc comments for improved clarity and consistency. - Adjusted error messages for `DependencyResolutionError` to provide clearer information. - Updated and expanded decorator and function types to use `Identifier` type.
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
import { TSinjex } from '../TSinjex';
|
||||
import { Identifier } from 'src/types/Identifier';
|
||||
import { TSinjex } from '../classes/TSinjex';
|
||||
import { InitDelegate } from '../types/InitDelegate';
|
||||
|
||||
/**
|
||||
* A decorator to inject a dependency from a DI (Dependency Injection) container.
|
||||
* The dependency is lazily evaluated when the property is accessed for the first time.
|
||||
* This can help avoid issues like circular dependencies and not-found dependencies.
|
||||
* @template ClassType The type of the property to be injected.
|
||||
* @param identifier The identifier used to resolve the dependency from the DI container.
|
||||
* A decorator to inject a dependency from a DI (Dependency Injection) container into a class property.
|
||||
* @template T The type of the dependency to be injected.
|
||||
* @template U The type of the property to be injected.
|
||||
* @param identifier The identifier used to resolve the class in the DI container.
|
||||
* @see {@link Identifier} for more information on identifiers.
|
||||
* @param init An optional initializer function to transform the dependency before injection.
|
||||
* @param necessary Indicates if the dependency is necessary.
|
||||
* - If `true`, an error will be thrown if the dependency cannot be resolved.
|
||||
* - If `false`, `undefined` will be returned if the dependency cannot be resolved.
|
||||
* @returns A decorator function to be applied on the class property.
|
||||
* @see {@link TSinjex}
|
||||
* @see {@link InitDelegate} for more information on initializer functions.
|
||||
* @param necessary If true, throws an error if the dependency is not found.
|
||||
* @returns The resolved dependency or undefined if the dependency is not necessary
|
||||
* and not found, or throws an error if the dependency is necessary and not found.
|
||||
* @throws A {@link DependencyResolutionError} if the dependency is not found and necessary.
|
||||
* @example
|
||||
* ```ts
|
||||
* class MyClass {
|
||||
@@ -29,7 +30,7 @@ import { InitDelegate } from '../types/InitDelegate';
|
||||
* ```
|
||||
*/
|
||||
export function Inject<T, U>(
|
||||
identifier: string,
|
||||
identifier: Identifier,
|
||||
init?: InitDelegate<T, U>,
|
||||
necessary = true,
|
||||
) {
|
||||
|
@@ -1,12 +1,14 @@
|
||||
import { TSinjex } from '../TSinjex';
|
||||
import { Identifier } from 'src/types/Identifier';
|
||||
import { TSinjex } from '../classes/TSinjex';
|
||||
|
||||
/**
|
||||
* A decorator to register a class in the DI (Dependency Injection) container.
|
||||
* A decorator to register a class in the **TSinjex** DI (Dependency Injection) container.
|
||||
* @template TargetType The type of the class to be registered.
|
||||
* @param identifier The identifier used to register the class in the DI container.
|
||||
* @param deprecated If true, the dependency is deprecated => a warning
|
||||
* is logged when the dependency is resolved.
|
||||
* @returns A function that is applied as a decorator to the class.
|
||||
* @see {@link Identifier} for more information on identifiers.
|
||||
* @param deprecated If true, the dependency is deprecated and a warning
|
||||
* is logged only once upon the first resolution of the dependency.
|
||||
* @returns The decorator function to be applied on the class.
|
||||
* @example
|
||||
* ```ts
|
||||
* \@Register('MyClassIdentifier')
|
||||
@@ -17,7 +19,7 @@ import { TSinjex } from '../TSinjex';
|
||||
*/
|
||||
export function Register<
|
||||
TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
|
||||
>(identifier: string, deprecated?: boolean) {
|
||||
>(identifier: Identifier, deprecated?: boolean) {
|
||||
return function (constructor: TargetType, ...args: unknown[]): void {
|
||||
// Get the instance of the DI container
|
||||
const diContainer = TSinjex.getInstance();
|
||||
|
@@ -1,17 +1,19 @@
|
||||
import { TSinjex } from '../TSinjex';
|
||||
import { Identifier } from 'src/types/Identifier';
|
||||
import { TSinjex } from '../classes/TSinjex';
|
||||
import { InitDelegate } from '../types/InitDelegate';
|
||||
|
||||
/**
|
||||
* A decorator to register an instance of a class in the DI (Dependency Injection) container.
|
||||
* The instance is created only when it is first needed (Lazy Initialization).
|
||||
* @template TargetType The type of the class whose instance is to be registered.
|
||||
* @param identifier The identifier used to register the instance in the DI container.
|
||||
* @see {@link Identifier} for more information on identifiers.
|
||||
* @param init An optional initializer function which get the constructor of the class
|
||||
* as input and returns an instance of the class.
|
||||
* @returns A function that is applied as a decorator to the class.
|
||||
* @see {@link InitDelegate} for more information on initializer functions.
|
||||
* @returns The decorator function to be applied on the class.
|
||||
* @example
|
||||
* ```ts
|
||||
* \@RegisterInstance('MyClassInstanceIdentifier', arg1, arg2)
|
||||
* \@RegisterInstance('MyClassInstanceIdentifier', (constructor) => new constructor())
|
||||
* class MyClass {
|
||||
* // ...
|
||||
* }
|
||||
@@ -20,7 +22,7 @@ import { InitDelegate } from '../types/InitDelegate';
|
||||
export function RegisterInstance<
|
||||
TargetType extends new (..._args: unknown[]) => InstanceType<TargetType>,
|
||||
>(
|
||||
identifier: string,
|
||||
identifier: Identifier,
|
||||
init?: InitDelegate<
|
||||
TargetType & { new (..._args: unknown[]): InstanceType<TargetType> },
|
||||
InstanceType<TargetType>
|
||||
|
Reference in New Issue
Block a user