0.0.0-alpha.40 • Published 4 days ago

yohira v0.0.0-alpha.40

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

Yohira

Yohira is an experimental project to port ASP.NET Core to TypeScript.

Installation

npm i yohira

Dependency Injection

This is a TypeScript port of Microsoft.Extensions.DependencyInjection.

Add interfaces

import { ServiceLifetime } from 'yohira';

interface IReportServiceLifetime {
    readonly id: string;
    readonly lifetime: ServiceLifetime;
}

const IExampleTransientService = Symbol.for('IExampleTransientService');
interface IExampleTransientService extends IReportServiceLifetime {
    readonly lifetime: ServiceLifetime.Transient;
}

const IExampleScopedService = Symbol.for('IExampleScopedService');
interface IExampleScopedService extends IReportServiceLifetime {
    readonly lifetime: ServiceLifetime.Scoped;
}

const IExampleSingletonService = Symbol.for('IExampleSingletonService');
interface IExampleSingletonService extends IReportServiceLifetime {
    readonly lifetime: ServiceLifetime.Singleton;
}

Add default implementations

import { randomUUID } from 'node:crypto';

class ExampleTransientService implements IExampleTransientService {
    readonly id = randomUUID();
    readonly lifetime = ServiceLifetime.Transient;
}

class ExampleScopedService implements IExampleScopedService {
    readonly id = randomUUID();
    readonly lifetime = ServiceLifetime.Scoped;
}

class ExampleSingletonService implements IExampleSingletonService {
    readonly id = randomUUID();
    readonly lifetime = ServiceLifetime.Singleton;
}

Add a service that requires DI

import { inject } from 'yohira';

function logService<T extends IReportServiceLifetime>(
    name: symbol,
    service: T,
    message: string,
): void {
    console.log(`    ${Symbol.keyFor(name)}: ${service.id} (${message})`);
}

class ServiceLifetimeReporter {
    constructor(
        @inject(IExampleTransientService)
        private readonly transientService: IExampleTransientService,

        @inject(IExampleScopedService)
        private readonly scopedService: IExampleScopedService,

        @inject(IExampleSingletonService)
        private readonly singletonService: IExampleSingletonService,
    ) {}

    reportServiceLifetimeDetails(lifetimeDetails: string): void {
        console.log(lifetimeDetails);

        logService(
            IExampleTransientService,
            this.transientService,
            'Always different',
        );
        logService(
            IExampleScopedService,
            this.scopedService,
            'Changes only with lifetime',
        );
        logService(
            IExampleSingletonService,
            this.singletonService,
            'Always the same',
        );
    }
}

Register services for DI

import {
    IServiceProvider,
    addScopedCtor,
    addSingletonCtor,
    addTransientCtor,
    createDefaultBuilder,
    createScope,
    getRequiredService,
    runApp,
} from 'yohira';

function exemplifyServiceLifetime(
    hostProvider: IServiceProvider,
    lifetime: string,
): void {
    const serviceScope = createScope(hostProvider);
    const provider = serviceScope.serviceProvider;
    const reporter1 = getRequiredService<ServiceLifetimeReporter>(
        provider,
        Symbol.for('ServiceLifetimeReporter'),
    );
    reporter1.reportServiceLifetimeDetails(
        `${lifetime}: Call 1 to getRequiredService<ServiceLifetimeReporter>` +
            "(provider, Symbol.for('ServiceLifetimeReporter'))",
    );

    console.log('...');

    const reporter2 = getRequiredService<ServiceLifetimeReporter>(
        provider,
        Symbol.for('ServiceLifetimeReporter'),
    );
    reporter2.reportServiceLifetimeDetails(
        `${lifetime}: Call 2 to getRequiredService<ServiceLifetimeReporter>` +
            "(provider, Symbol.for('ServiceLifetimeReporter'))",
    );

    console.log('\n');

    serviceScope.dispose();
}

const host = createDefaultBuilder()
    .configureServices((context, services) => {
        addTransientCtor(
            services,
            IExampleTransientService,
            ExampleTransientService,
        );
        addScopedCtor(services, IExampleScopedService, ExampleScopedService);
        addSingletonCtor(
            services,
            IExampleSingletonService,
            ExampleSingletonService,
        );
        addTransientCtor(
            services,
            Symbol.for('ServiceLifetimeReporter'),
            ServiceLifetimeReporter,
        );
    })
    .build();

exemplifyServiceLifetime(host.services, 'Lifetime 1');
exemplifyServiceLifetime(host.services, 'Lifetime 2');

await runApp(host);

See also

References

0.0.0-alpha.39

4 days ago

0.0.0-alpha.40

4 days ago

0.0.0-alpha.37

1 month ago

0.0.0-alpha.36

1 month ago

0.0.0-alpha.35

1 month ago

0.0.0-alpha.34

3 months ago

0.0.0-alpha.33

3 months ago

0.0.0-alpha.32

3 months ago

0.0.0-alpha.31

3 months ago

0.0.0-alpha.30

4 months ago

0.0.0-alpha.29

4 months ago

0.0.0-alpha.24

5 months ago

0.0.0-alpha.22

5 months ago

0.0.0-alpha.28

4 months ago

0.0.0-alpha.27

5 months ago

0.0.0-alpha.26

5 months ago

0.0.0-alpha.25

5 months ago

0.0.0-alpha.19

6 months ago

0.0.0-alpha.18

6 months ago

0.0.0-alpha.20

6 months ago

0.0.0-alpha.13

10 months ago

0.0.0-alpha.21

6 months ago

0.0.0-alpha.17

6 months ago

0.0.0-alpha.16

6 months ago

0.0.0-alpha.15

7 months ago

0.0.0-alpha.14

7 months ago

0.0.0-alpha.12

1 year ago

0.0.0-alpha.11

1 year ago

0.0.0-alpha.10

1 year ago

0.0.0-alpha.9

1 year ago

0.0.0-alpha.8

1 year ago

0.0.0-alpha.7

1 year ago

0.0.0-alpha.6

1 year ago

0.0.0-alpha.5

1 year ago

0.0.0-alpha.4

1 year ago

0.0.0-alpha.3

1 year ago

0.0.0-alpha.2

1 year ago

0.0.0-alpha.1

1 year ago