Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - Improvements on maintaining global application wide config object

The application which I am working on has multiple products. Product-specific config will help to avoid if-else. Also, I want to ensure that the config keys are maintained across product-specific config. This is achieved by using interface so that developer should not miss the key while adding it to a product.

What I am looking for is some suggestions on these below points -

  1. While this object could grow & become heavy in size, I also want to make sure that this class will dynamically import object based on the current product-name ( product1 | product2), etc. (So that overall performance can be improved.)

  2. Better way to write the ProductConfigService class if any.

  3. OR another approach this can be done efficiently ( leveraging TS)

Kindly suggest.

interface PRODUCT_CONFIG {
    API_KEY: string,
    AGE: number,
    TEXT: string

}

const product1: PRODUCT_CONFIG = {
    API_KEY: "SOME_VALUE",
    AGE: 10,
    TEXT: "SOME_VALUE"
}


const product2: PRODUCT_CONFIG = {
    API_KEY: "SOME_VALUE"
    AGE: 12,
    TEXT: "SOME_VALUE"
}


class ProductConfigService {

    private product: PRODUCT_CONFIG;
    

    constructor(private accessKey:string) {
      this.product = product2; // should be dynamic based on the current selected application. This is hard-coded to product2.
    }

    getConfig(): string {
      //@ts-ignore
      return this.product[this.accessKey];
    }

}

// consumer.

let product = new ProductConfigService('AGE');

console.log(product.getConfig());

question from:https://stackoverflow.com/questions/66049030/improvements-on-maintaining-global-application-wide-config-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would create an abstract class that would be inherited to match every application needs.


There would be a PRODUCT_CONFIG by default, that could be also extended.

In the following code, getConfig() returns the correct type of value depending on the configured key.


warning : The annoying thing is that the user of ProductConfigService have to specify the name of the key used for the configuration. I don't know how to extract it from the parameter itself.


playground


// ---------- ABSTRACT CLASS DECLARATION

interface PRODUCT_CONFIG {
    API_KEY: string;
    AGE: number;
    TEXT: string;
}

abstract class AProductConfigService<T extends PRODUCT_CONFIG, U extends keyof T> {
    protected product: T;

    protected accessKey: keyof T;
    
    constructor(accessKey: keyof T, product: T) {
      this.accessKey = accessKey
      this.product = product;
    }

    public getConfig(): T[U] {
      return this.product[this.accessKey] as T[U];
    }
}

// ---------- CLASS IMPLEMENTATION

interface EXTENDED_PRODUCT_CONFIG extends PRODUCT_CONFIG {
    // additionnal keys ...
    ANIMAL: string;
}

class ProductConfigService<U extends keyof EXTENDED_PRODUCT_CONFIG> extends AProductConfigService<EXTENDED_PRODUCT_CONFIG, U> {
    constructor(accessKey: U) {
      super(accessKey, product2);
    }

    // ... new methods
}

// ---------- PRODUCTS DEFINITION

const product1: PRODUCT_CONFIG = {
    API_KEY: 'SOME_VALUE',
    AGE: 10,
    TEXT: 'SOME_VALUE',
}

const product2: EXTENDED_PRODUCT_CONFIG = {
    API_KEY: 'SOME_VALUE',
    AGE: 12,
    TEXT: 'SOME_VALUE',
    ANIMAL: 'fox',
}

// -------------- PRODUCT USE

const productA = new ProductConfigService<'AGE'>('AGE');

const valA = productA.getConfig();

console.log(valA);

const productB = new ProductConfigService<'API_KEY'>('API_KEY');

const valB = productB.getConfig();

console.log(valB);

const productC = new ProductConfigService<'ANIMAL'>('ANIMAL');

const valC = productC.getConfig();

console.log(valC);

// ----------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...