ရိုးရိုးလေး ရှင်းပြမယ်
Services & DI ကို ရိုးရိုးလေးနားလည်မယ်
Services များသည် application logic သို့မဟုတ် data များကို components များအကြား share ရန် အသုံးပြုသော singleton objects များဖြစ်သည်။ Dependency Injection (DI) ဖြင့် Angular သည် components များလိုအပ်သော services များကို အလိုအလျောက် ထောက်ပံ့ပေးသည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Service တစ်ခုကို component ထဲ `new` နဲ့တည်ဆောက်ခြင်းက DI scope, configuration နဲ့ test replacement ကိုကျော်သွားပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Service က shared business logic, state coordination သို့ external boundary ကို component UI မှခွဲပေးပါတယ်။ Dependency injection က concrete construction ကိုconsumer မှဖယ်ပြီး provider scope အလိုက် singleton သို့ feature instance lifecycle သတ်မှတ်ပါတယ်။ Injection token, factory/value providers နဲ့ interface-like abstraction သုံးကာ tests မှာ fake dependency ထည့်နိုင်အောင် design လုပ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Services & DI ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// logger.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class LoggerService {
log(message: string) {
console.log(message);
}
}
// my.component.ts
export class MyComponent {
constructor(private logger: LoggerService) {
this.logger.log('Component created!');
}
}Service က shared business logic, state coordination သို့ external boundary ကို component UI မှခွဲပေးပါတယ်။ Dependency injection က concrete construction ကိုconsumer မှဖယ်ပြီး provider scope အလိုက် singleton သို့ feature instance lifecycle သတ်မှတ်ပါတယ်။ Injection token, factory/value providers နဲ့ interface-like abstraction သုံးကာ tests မှာ fake dependency ထည့်နိုင်အောင် design လုပ်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
// logger.service.tsServices များသည် application logic သို့မဟုတ် data များကို components များအကြား share ရန် အသုံးပြုသော singleton objects များဖြစ်သည်။ Dependency Injection (DI) ဖြင့် Angular သည် components များလိုအပ်သော services များကို အလိုအလျောက် ထောက်ပံ့ပေးသည်။
- Component + template
}Service က shared business logic, state coordination သို့ external boundary ကို component UI မှခွဲပေးပါတယ်။ Dependency injection က concrete construction ကိုconsumer မှဖယ်ပြီး provider scope အလိုက် singleton သို့ feature instance lifecycle သတ်မှတ်ပါတယ်။ Injection token, factory/value providers နဲ့ interface-like abstraction သုံးကာ tests မှာ fake dependency ထည့်နိုင်အောင် design လုပ်ပါ။
- Rendered Angular UI
}Pricing service ကိုtax config token နဲ့ inject လုပ်ပြီး component နှစ်ခုကသုံးပါ။ Test မှာ fake config/service ထည့်ပြီး scope က shared မshared စစ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Services & DI ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Service တစ်ခုကို component ထဲ `new` နဲ့တည်ဆောက်ခြင်းက DI scope, configuration နဲ့ test replacement ကိုကျော်သွားပါတယ်။
Services များသည် application logic သို့မဟုတ် data များကို components များအကြား share ရန် အသုံးပြုသော singleton objects များဖြစ်သည်။ Dependency Injection (DI) ဖြင့် Angular သည် components များလိုအပ်သော services များကို အလိုအလျောက် ထောက်ပံ့ပေးသည်။
// logger.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class LoggerService {
log(message: string) {
console.log(message);
}
}
// my.component.ts
export class MyComponent {
constructor(private logger: LoggerService) {
this.logger.log('Component created!');
}
}(MyComponent တည်ဆောက်လိုက်သောအခါ browser console တွင် 'Component created!' ဟု ပေါ်လာမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Pricing service ကိုtax config token နဲ့ inject လုပ်ပြီး component နှစ်ခုကသုံးပါ။ Test မှာ fake config/service ထည့်ပြီး scope က shared မshared စစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Services & DI နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// logger.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class LoggerService {
log(message: string) {
console.log(message);
}
}
// my.component.ts
export class MyComponent {
constructor(private logger: LoggerService) {
this.logger.log('Component created!');
}
}ရလဒ်: Services & DI ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Pricing service ကိုtax config token နဲ့ inject လုပ်ပြီး component နှစ်ခုကသုံးပါ။ Test မှာ fake config/service ထည့်ပြီး scope က shared မshared စစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Service က shared business logic, state coordination သို့ external boundary ကို component UI မှခွဲပေးပါတယ်။ Dependency injection က concrete construction ကိုconsumer မှဖယ်ပြီး provider scope အလိုက် singleton သို့ feature instance lifecycle သတ်မှတ်ပါတယ်။ Injection token, factory/value providers နဲ့ interface-like abstraction သုံးကာ tests မှာ fake dependency ထည့်နိုင်အောင် design လုပ်ပါ။
- Service တစ်ခုကို component ထဲ `new` နဲ့တည်ဆောက်ခြင်းက DI scope, configuration နဲ့ test replacement ကိုကျော်သွားပါတယ်။
- Pricing service ကိုtax config token နဲ့ inject လုပ်ပြီး component နှစ်ခုကသုံးပါ။ Test မှာ fake config/service ထည့်ပြီး scope က shared မshared စစ်ပါ။
Angular — Dependency Injection — Angular