Skip to content

Global Container

Francisco Ruiz edited this page Sep 15, 2024 · 2 revisions

Global Container

Getting the global container:

import { Container } from 'packetjs-di';

const globalContainer = Container.getContainer();

Note that getContainer() method is a static method of the Container class.

You can also get the global container using the import statement:

// Import the global container using CommonJS modules
const { default: packetJsDi } = require('packetjs-di');

// Import the global container using ES6 imports
import packetJsDi from 'packetjs-di';

Register a service using the global container from the import statement:

import packetJsDi from 'packetjs-di';

packetJsDi.add('Service', () => {
  return new SomeService();
});

Get the service in the application using the global container from the import statement:

import packetJsDi from 'packetjs-di';

const service = packetJsDi.get('Service'); // Instantiate the service

In both cases, packetJsDi will always have the same global instance of the container. Except in environments rendered in SSR vs CSR, like Next.js, Angular, etc., where the container instance may be different depending on the rendering environment.

1. Getting Started

2. Core Concepts

3. Middleware

4. Caching and Memoization

5. Typescript

6. Considerations

7. API Reference

8. Examples and Use Cases

9. Contributing

We welcome contributions from the community! Whether it's reporting a bug, suggesting a feature, or contributing code, your involvement is key to making Packet.js better. Please check out this section for more details.

10. Support

If you encounter any issues or bugs while using packetjs, or if you have any feature requests, please feel free to open an issue in our GitHub repository. You can report issues, track progress, and engage in discussions with the community via the following link:

11. FAQ

If you have any questions or need further assistance, feel free to reach out via the discussion board.

12. License

Clone this wiki locally