Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

POC: Transitioning to decorator-based module system #237

Copy link
Copy link
@rsaz

Description

@rsaz
Issue body actions

Description

To study the replacement of the current module registration and dependency injection system with a decorator-based approach.
The existing mechanism, which relies on explicit imports and manual module registration via Container from "inversify" and AppContainer from "@expressots/core", has served us well but lacks the simplicity and elegance that decorators can provide. By adopting decorators, we aim to streamline the module system, improve developer experience, and align with modern TypeScript best practices.

Proposed Decorator-Based Approach

@container Decorator: This decorator can be used to mark a class as a container that can contain modules.

Current approach:

export const appContainer: AppContainer = new AppContainer({
    autoBindInjectable: false,
});

export const container: Container = appContainer.create([
    // Add your modules here
    AppModule,
]);

Proposed new approach

@Container({
    config: {
        autoBindInjectable: false,
        defaultScope: BindingScopeEnum.Transient,
        skipBaseClassChecks: true,
    },
    modules: [],
})
export class AppContainer {}

To be used in the bootstrap:

async function bootstrap() {
    const app = await AppFactory.create(AppContainer, App);
    await app.listen(3000, ServerEnvironment.Development);
}

Benefits

  • @container Decorator Improvements: We enhance the @container decorator to accept a configuration object. This decorator is responsible for marking a class as the root container for the application, allowing for centralized configuration of the dependency injection container options and registered modules.
  • Module Registration: We can further simplify module registration by allowing the @container decorator to automatically include modules listed in its modules array, reducing manual setup.
  • Simplified Bootstrap Function: The bootstrap function becomes cleaner, focusing solely on application initialization and setup, without directly dealing with container configuration details.
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestNew feature or request

    Type

    No type

    Projects

    Status

    Backlog
    Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.