Created
February 22, 2018 21:12
-
-
Save msarsha/e4b47f292a0ff81d57967517dddff3c7 to your computer and use it in GitHub Desktop.
switch\case angular di
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class CaseOne implements CaseLogicHandler{ | |
key: any = 1; | |
doLogic() { | |
// case logic | |
} | |
} | |
export class CaseTwo implements CaseLogicHandler{ | |
key: any = 2; | |
doLogic() { | |
// case logic | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface LogicHandler { | |
doLogic(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NgModule({ | |
providers: [ | |
{provide: CASE_LOGIC_HANDLER, useClass: CaseOne, multi: true}, | |
{provide: CASE_LOGIC_HANDLER, useClass: CaseOne, multi: true}, | |
// ... | |
] | |
}) | |
export class AppModule { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class NastyService { | |
constructor() { | |
} | |
doNastyOperation(key: number) { | |
switch (key) { | |
case 1: | |
// logic | |
case 2: | |
// logic | |
case 3: | |
// logic | |
case 4: | |
// logic | |
default: | |
// logic | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class NastyService { | |
constructor(@Inject(CASE_LOGIC_HANDLER) private caseHandlers: CaseLogicHandler[]) { | |
} | |
doNastyOperation(key: any) { | |
const handler = this.caseHandlers.find(h => h.key === key); | |
if (!handler) throw new Error(`No case handler provided for key: ${key}`); | |
handler.doLogic(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class NastyService { | |
constructor(@Inject(CASE_LOGIC_HANDLER) private caseHandlers: CaseLogicHandler[]) { | |
} | |
doNastyOperation(key: any) { | |
switch (key) { | |
case 1: | |
// logic | |
case 2: | |
// logic | |
case 3: | |
// logic | |
case 4: | |
// logic | |
default: | |
// logic | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const LOGIC_HANDLER = new InjectionToken<LogicHandler>('logic.handlers'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment