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

Add inversify 7 code examples #317

Merged
merged 13 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/docs/tools/inversify-code-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"description": "InversifyJs docs code examples package",
"dependencies": {
"inversify": "6.2.1"
"inversify": "6.2.1",
"inversify7": "npm:[email protected]"
},
"devDependencies": {
"@eslint/js": "9.18.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Container, injectable, ResolutionContext } from 'inversify7';

const container: Container = new Container();

// Begin-example
@injectable()
class Katana {
public use(): string {
return 'hit!';
}
}

container
.bind<Katana>('Katana')
.to(Katana)
.onActivation((_context: ResolutionContext, katana: Katana) => {
const handler: ProxyHandler<() => string> = {
apply: function (
target: () => string,
thisArgument: unknown,
argumentsList: [],
) {
console.log(`Starting: ${new Date().getTime().toString()}`);
const result: string = target.apply(thisArgument, argumentsList);
console.log(`Finished: ${new Date().getTime().toString()}`);
return result;
},
};

katana.use = new Proxy(katana.use.bind(katana), handler);

return katana;
});
// End-example

export { container, Katana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from '@jest/globals';

import { isSameKatana, warriorHasSameKatana } from './bindingScopeRequest';

describe('BindingInSyntax API (inSingletonScope)', () => {
it('should provide same Katana', () => {
expect(isSameKatana).toBe(false);
expect(warriorHasSameKatana).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Container, inject } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
export class LegendaryWarrior {
constructor(
@inject('Weapon') public readonly firstWeapon: Weapon,
@inject('Weapon') public readonly secondWeapon: Weapon,
@inject('Weapon') public readonly thirdWeapon: Weapon,
) {}
}

const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inRequestScope();
container.bind(LegendaryWarrior).toSelf();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

const legendaryWarrior: LegendaryWarrior = container.get(LegendaryWarrior);

// Returns false
const isSameKatana: boolean = firstKatana === secondKatana;

// Returns true
const warriorHasSameKatana: boolean =
legendaryWarrior.firstWeapon === legendaryWarrior.secondWeapon &&
legendaryWarrior.secondWeapon === legendaryWarrior.thirdWeapon;
// End-example

export { isSameKatana, warriorHasSameKatana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@jest/globals';

import { isSameKatana } from './bindingScopeSingleton';

describe('BindingInSyntax API (inSingletonScope)', () => {
it('should provide same Katana', () => {
expect(isSameKatana).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Container } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inSingletonScope();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

// Returns true
const isSameKatana: boolean = firstKatana === secondKatana;
// End-example

export { isSameKatana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@jest/globals';

import { isSameKatana } from './bindingScopeTransient';

describe('BindingInSyntax API (inTransientScope)', () => {
it('should provide same Katana', () => {
expect(isSameKatana).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Container } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inTransientScope();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

// Returns false
const isSameKatana: boolean = firstKatana === secondKatana;
// End-example

export { isSameKatana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@jest/globals';

import { Katana, katana } from './bindingToSyntaxApiTo';

describe('BindingToSyntax API (to)', () => {
it('should bind Katana weapon', () => {
expect(katana).toBeInstanceOf(Katana);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Container } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana);

const katana: Weapon = container.get<Weapon>('Weapon');
// End-example

export { katana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@jest/globals';

import { Katana, katana } from './bindingToSyntaxApiToConstantValue';

describe('BindingToSyntax API (toConstantValue)', () => {
it('should bind Katana weapon', () => {
expect(katana).toBeInstanceOf(Katana);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Container } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
const container: Container = new Container();
container.bind<Weapon>('Weapon').toConstantValue(new Katana());

const katana: Weapon = container.get<Weapon>('Weapon');
// End-example

export { katana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@jest/globals';

import { Katana, katana } from './bindingToSyntaxApiToDynamicValue';

describe('BindingToSyntax API (toDynamicValue)', () => {
it('should bind Katana weapon', () => {
expect(katana).toBeInstanceOf(Katana);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Container } from 'inversify7';

interface Weapon {
damage: number;
}

export class Katana implements Weapon {
public readonly damage: number = 10;
}

// Begin-example
const container: Container = new Container();
container.bind<Weapon>('Weapon').toDynamicValue((): Weapon => new Katana());

const katana: Weapon = container.get<Weapon>('Weapon');
// End-example

export { katana };
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from '@jest/globals';

import {
container,
DieselCarFactory,
DieselEngine,
} from './bindingToSyntaxApiToFactory';

describe('BindingToSyntax API (toFactory)', () => {
it('should provide a factory able to provide a diesel engine', () => {
container.bind(DieselCarFactory).toSelf();
expect(container.get(DieselCarFactory).createEngine(3)).toBeInstanceOf(
DieselEngine,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
Container,
Factory,
inject,
injectable,
ResolutionContext,
} from 'inversify7';

export interface Engine {
displacement: number;
}

class BaseEngine implements Engine {
public displacement: number;

constructor(displacement?: number) {
this.displacement = displacement ?? 0;
}
}

export class DieselEngine extends BaseEngine {}

export class PetrolEngine extends BaseEngine {}

interface CarFactory {
createEngine(displacement: number): Engine;
}

const container: Container = new Container();

// Begin-example
container.bind<Engine>('Engine').to(PetrolEngine).whenNamed('petrol');
container.bind<Engine>('Engine').to(DieselEngine).whenNamed('diesel');

container
.bind<Factory<(displacement: number) => Engine, [string]>>('Factory<Engine>')
.toFactory((context: ResolutionContext) => {
return (named: string) => (displacement: number) => {
const engine: Engine = context.get<Engine>('Engine', {
name: named,
});
engine.displacement = displacement;
return engine;
};
});

@injectable()
class DieselCarFactory implements CarFactory {
readonly #dieselFactory: (displacement: number) => Engine;

constructor(
@inject('Factory<Engine>')
factory: (category: string) => (displacement: number) => Engine, // Injecting an engine factory
) {
// Creating a diesel engine factory
this.#dieselFactory = factory('diesel');
}

public createEngine(displacement: number): Engine {
// Creating a concrete diesel engine
return this.#dieselFactory(displacement);
}
}
// End-example

export { container, DieselCarFactory };
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from '@jest/globals';

import {
Katana,
notSoPowerfulGoldKatana,
powerfulGoldKatana,
} from './bindingToSyntaxApiToProvider';

describe('BindingToSyntax API (toFactory)', () => {
it('should provide a provider able to provide a katanas', async () => {
const expectedNotSoPowerfulKatana: Katana = new Katana();
expectedNotSoPowerfulKatana.damage = 10;
expectedNotSoPowerfulKatana.material = 'gold';

const expectedPowerfulKatana: Katana = new Katana();
expectedPowerfulKatana.damage = 100;
expectedPowerfulKatana.material = 'gold';

expect(await notSoPowerfulGoldKatana).toStrictEqual(
expectedNotSoPowerfulKatana,
);
expect(await powerfulGoldKatana).toStrictEqual(expectedPowerfulKatana);
});
});
Loading
Loading