2016-06-18 9 views
0

私はAngular2アプリに列挙型の列挙型を持っています。列挙型ごとにいくつかの追加機能を提供する対応するutilsクラスを作成しました。これは単純に、TypeScriptの制限を克服しています.Javaでも同じようにenumに関数を追加できません。私は必要なところ、私はDIで使用できるように、私は注射用としてutilsのクラスをマークした見ることができるように、今Angular2とTypeScriptの列挙型 - doとdon'ts

import {Injectable} from "@angular/core"; 

export enum Country { 
    ANDORRA = <any>"ANDORRA", 
    UNITED_ARAB_EMIRATES = <any>"UNITED_ARAB_EMIRATES", 
    AFGHANISTAN = <any>"AFGHANISTAN", 
    // ... 
    // more countries skipped for brevity 
    // ... 
    ZAMBIA = <any>"ZAMBIA", 
    ZIMBABWE = <any>"ZIMBABWE" 
} 


@Injectable() 
export class CountryUtils { 

    private _emptyEntry: Array<string> = ["", "", ""]; 

    private countryData: any = { 
     ANDORRA: ["Andorra", "AD", "AND"], 
     UNITED_ARAB_EMIRATES: ["United Arab Emirates", "AE", "ARE"], 
     AFGHANISTAN: ["Afghanistan", "AF", "AFG"], 
     // ... 
     // more countries skipped for brevity 
     // ... 
     ZAMBIA: ["Zambia", "ZM", "ZMB"], 
     ZIMBABWE: ["Zimbabwe", "ZW", "ZWE"] 
    } 

    getData(country: Country): any { 
     if (!country) { 
      return this._emptyEntry; 
     } 
     return this.countryData[Country[country]]; 
    } 

    getName(country: Country): any { 
     return this.getData(country)[0]; 
    } 

    getCode2(country: Country): any { 
     return this.getData(country)[1]; 
    } 

    getCode3(country: Country): any { 
     return this.getData(country)[2]; 
    } 
} 

は、ここに私の国の列挙型の例だと、対応するutilsのクラスそれ。

私が直面している問題は、Angular2に依存しないコアTS/JSモデルクラスでも使用されているため、私のutilsクラスのインスタンスを挿入するためにDIを使用することはできません)をコアモデルクラスの内側に配置します。

私が見ている唯一の回避策は、Injectable()アプローチを削除し、utilsクラスのメソッドを静的にマークすることです。だから、私はちょうどenumと一緒に各utilsクラスをインポートし、私が欲しいところでそれを使うことができます。

私の質問は、デザインの決定がどれほど悪いのでしょうか? Angular2でこれを行うことは容認できますか?私は通常のDI手法と比較して目が痛いのと同じように特に有害なものは見当たりません。

アドバイスをいただければ幸いです。前もって感謝します!

答えて

0

静的クラスが問題として表示されません。また、Declaration Mergingを使用して、enumにutil機能を追加することもできます。余分なクラスは必要ありません。以下を確認してください。

enum Country { 
    ANDORRA = <any>"ANDORRA", 
    UNITED_ARAB_EMIRATES = <any>"UNITED_ARAB_EMIRATES", 
    AFGHANISTAN = <any>"AFGHANISTAN", 
    // ... 
    // more countries skipped for brevity 
    // ... 
    ZAMBIA = <any>"ZAMBIA", 
    ZIMBABWE = <any>"ZIMBABWE" 
} 

namespace Country { 

    const _emptyEntry: Array<string> = ["", "", ""]; 

    const countryData: any = { 
     ANDORRA: ["Andorra", "AD", "AND"], 
     UNITED_ARAB_EMIRATES: ["United Arab Emirates", "AE", "ARE"], 
     AFGHANISTAN: ["Afghanistan", "AF", "AFG"], 
     // ... 
     // more countries skipped for brevity 
     // ... 
     ZAMBIA: ["Zambia", "ZM", "ZMB"], 
     ZIMBABWE: ["Zimbabwe", "ZW", "ZWE"] 
    }; 

    export function getData(country: Country): any { 
     if (!country) { 
      return this._emptyEntry; 
     } 
     return this.countryData[Country[country]]; 
    } 

    export function getName(country: Country): any { 
     return this.getData(country)[0]; 
    } 

    export function getCode2(country: Country): any { 
     return this.getData(country)[1]; 
    } 

    export function getCode3(country: Country): any { 
     return this.getData(country)[2]; 
    } 
} 

Country.getCode2(Country.AFGHANISTAN);