Skip to content

Boundary Provider

  • import: @azure-net/kit
  • Preferred location (for context): src/app/contexts/{ContextName}/infrastructure/providers

Allows for creating providers that manage dependencies between application layers. They are responsible for registering and linking services, lazy initialization, lifecycle management, and passing dependencies further down the architecture.

Helps collect and provide dependencies (datasource → repository → service → etc.). With providers, you show: "here are my classes, here's how to create them, and here's what they depend on".

Then the provider itself:

  • creates objects only when they are actually needed;
  • remembers them to avoid creating copies;
  • can pull dependencies from other providers;
  • provides a single place where you can see what services exist in the context.

Simply put, Boundary Provider is a factory and dependency manager for your context.

Providers are used not only in the infrastructure layer, but since this is the first case where they are required in the documentation, their basics are described here.

Example of creating a provider for data source

typescript
import { createBoundaryProvider } from '@azure-net/kit';
// First argument - provider name, which must be unique across the entire project
export const DatasourceProvider = createBoundaryProvider('DatasourceProvider', {
    // The register key must be a function that returns an object. Object keys are functions returning the class instance we need.
    register: () => ({
        MyCustomDatasource: () =>
            new MyCustomDatasource({
                http: new HttpService({
                    baseUrl: `https://api-custom.ru`,
                    onRequest: (options) => {
                        const token = UniversalCookie.get('token');
                        if (token) {
                            options.headers = { ...options.headers, Authorization: `Bearer ${token}` };
                        }
                    }
                })
            })
    })
});

Example of creating a provider for infrastructure layer

typescript
// Creating provider
export const InfrastructureProvider = createBoundaryProvider('InfrastructureProvider', {
    dependsOn: { DatasourceProvider }, // Dependencies on other providers; keys registered in dependsOn we get in the register method and can use them
    // Also register what we'll provide to the next layer (application). It will also have a provider, but depending on infrastructure.
    register: ({ DatasourceProvider }) => ({
        MyRepository: () => new MyRepository(DatasourceProvider.MyCustomDatasource),
        AnotherRepository: () => new AnotherRepository(DatasourceProvider.MyCustomDatasource)
    }),
    boot: (services) => {
        // Optional initialization function; happens after all services are registered and receives them in callback.
        // Allows performing actions with services before provider initialization
        console.log('Infrastructure Provider loaded');
    }
});

Features:

  • Lazy initialization - services are created on demand
  • Caching - each instance is created once
  • Dependencies - automatic dependency resolution between providers
  • Context - support for server and client context
  • Cleanup - automatic resource cleanup