Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Transitioning to decorator-based module system #237

Open
rsaz opened this issue Apr 2, 2024 · 0 comments
Open

POC: Transitioning to decorator-based module system #237

rsaz opened this issue Apr 2, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@rsaz
Copy link
Member

rsaz commented Apr 2, 2024

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.
@rsaz rsaz added the enhancement New feature or request label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant