0.1.0 • Published 1 year ago

@webspaceiq/service-objects v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Service Objects

A small TypeScript library for defining, looking up and executing user defined service objects.

A service is a user defined class that implements the IService interface.

export interface IService<T, U> {

    execute(context: IServiceContext<T>): U;
}

Additionally a service has a constructor interface.

export interface IServiceConstructor {

    new(...args: any[]): IServiceInterface; 

    /**
     * Every service class has a static 
     * serviceName property
     */   
    serviceName: string;
}

This library defines an API for creating, storing, looking up and executing services. This is achieved with the help of service executors, service repositories and decorators.

import { IExecutionContext, IServiceInterface, IServiceConstructor } from "@alstradocs/service-objects";

export interface IServiceExecutor {

    executeService<T, U>(context: IExecutionContext<T>): U
}

export interface IServiceRepository {

    get(serviceName: string): IServiceInterface;

    register(serviceName: string, serviceConstructor: IServiceConstructor): void;
}

Installation

NPM:

npm install @alstradocs/service-objects --save

Usage example

1. Define Services

First you need a service. You can simply annotate a class with the @IsService decorator.

import { IsService, IServiceContext } from "@alstradocs/service-objects";

interface BinaryOperands {
    firstOperand: number;
    secondOperand: number;
}

@IsService()
class AdditionService {

    public static serviceName = 'AdditionService';

    execute(context: IServiceContext<BinaryOperands>): number {
        let { firstOperand, secondOperand } = context.data;
        return firstOperand + secondOperand;
    }
}

2. Instantiate/Define Service Repository

Next you add your shining new service to an instance of IServiceRepository. A service repository is just a fancy name for a service store. You can use the default repository

import { ServiceRepository } from "@alstradocs/service-objects";

// Initialize service repo with just a single service ...
let serviceInfo = { 
        serviceName:AdditionService.serviceName, 
        serviceContructor: AdditionService 
};
let repository = new ServiceRepository([serviceInfo]);

// ... or add to existing repo
repository.register(AdditionService.serviceName, AdditionService);

Or you can implement your own repository

import { IsServiceRepository, IServiceConstructor } from "@alstradocs/service-objects";

@IsServiceRepository()
export class MyServiceRepository {

    get(serviceName: string): IServiceInterface {
        ...
    }

    register(serviceName: string, serviceConstructor: IServiceConstructor): void {
        ...
    }
}
// ... Instantiate repo and add service
let repository = new MyServiceRepository();
repository.register(AdditionService.serviceName, AdditionService);

3. Define/Instanstiate A Service Execcutor

To execute your service, you need an instance of IServiceExecutor. The service executor is a object that knows how to find and execute services. It uses the service repository for this. You can use the default executor.

import { ServiceRepository } from "@alstradocs/service-objects";

let repository = // initialize repo
// instantiate executor with given repo
let serviceExecutor = new ServiceExecutor(repository);

Or you can implement your own executor

import { IsServiceExecutor, IExecutionContext } from "@alstradocs/service-objects";

@IsServiceExecutor()
export class MyServiceExecutor {
    
    executeService<T, U>(context: IExecutionContext<T>): U {
        ...
    }
}
// ... instantiate and execute (context contains all info required to execute a service)
let serviceExecutor = new MyServiceExecutor();

4. Execute Service

The final step is executing the service.

serviceExecutor.executeService(context);

Typically all the entire setup above (apart from step 4) will be done once before or during app/script inititialization. You can then store the executor app/script wide and make it available to execute services as needed.

For more examples and usage, please refer to the source code.

Release History

  • 1.0.0
    • Work in progress

Meta

Edward Banfa – @EdwardBanfa – ebanfa@gmail.com

Distributed under the Apache license. See LICENSE for more information.

https://github.com/alstradocs/service-objects

Readme Image

robot clipart png from pngtree.com