ရိုးရိုးလေး ရှင်းပြမယ်
Project: Contact Book – Part 3 (Validation + Polish) ကို ရိုးရိုးလေးနားလည်မယ်
Part 2 အထိ Contact Book က CRUD operations အားလုံး အလုပ်လုပ်နေပြီ ဖြစ်ပေမယ့် user က invalid input (empty name, wrong menu choice) ရိုက်ရင် app crash သွားနိုင်ပါတယ်။ Advanced chapter က exception handling (try/catch) concept ကို ဒီနေရာမှာ လက်တွေ့ အသုံးချပြီး app ကို robust ဖြစ်အောင် ပြင်ဆင်မှာပါ။ ဒါအပြင် LINQ OrderBy() နဲ့ contacts list ကို name အလိုက် sort လုပ်ပြီး ပြသတဲ့အခါ ပိုသေချာသင့်တင့်စေမှာပါ။ ဒီ part ပြီးရင် Contact Book project က end-to-end အနေနဲ့ ပြည့်စုံသွားပါပြီ — Basic, Intermediate, Advanced chapters သုံးခုလုံးက concept တွေကို တစ်ခုတည်းသော project ထဲမှာ ပေါင်းစပ်အသုံးချနိုင်ခဲ့ပါပြီ။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
UI ကexception အားလုံးကိုgeneric success/failure အဖြစ်ဖုံးခြင်းကdebugging ခက်ပြီးdata loss ကိုမသိစေပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Polish အဆင့်တွင်validation consistency, exception boundaries, persistence corruption, logging, deterministic tests နဲ့user-friendly messages ကိုထည့်ပါ။ Save operation ကိုtemporary file + replace pattern ဖြင့်ပိုလုံခြုံစေပြီး secrets/personal data ကိုlogs မထည့်ပါနှင့်။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Project: Contact Book – Part 3 (Validation + Polish) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class InvalidContactException : Exception
{
public InvalidContactException(string message) : base(message) { }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
if (string.IsNullOrWhiteSpace(c.Name) || string.IsNullOrWhiteSpace(c.Phone))
{
throw new InvalidContactException("Name and Phone are required.");
}
contacts.Add(c);
}
public void ShowAll()
{
Console.WriteLine("---- Contact List (sorted) ----");
foreach (Contact c in contacts.OrderBy(c => c.Name))
{
Console.WriteLine($"{c.Name,-15} | {c.Phone,-12} | {c.Email}");
}
Console.WriteLine("--------------------------------");
}
}
// Main() ရဲ့ Add branch ထဲမှာ
// try
// {
// manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
// Console.WriteLine("Contact added successfully.");
// }
// catch (InvalidContactException ex)
// {
// Console.WriteLine("Error: " + ex.Message);
// }Polish အဆင့်တွင်validation consistency, exception boundaries, persistence corruption, logging, deterministic tests နဲ့user-friendly messages ကိုထည့်ပါ။ Save operation ကိုtemporary file + replace pattern ဖြင့်ပိုလုံခြုံစေပြီး secrets/personal data ကိုlogs မထည့်ပါနှင့်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Project requirements
class InvalidContactException : ExceptionPart 2 အထိ Contact Book က CRUD operations အားလုံး အလုပ်လုပ်နေပြီ ဖြစ်ပေမယ့် user က invalid input (empty name, wrong menu choice) ရိုက်ရင် app crash သွားနိုင်ပါတယ်။ Advanced chapter က exception handling (try/catch) concept ကို ဒီနေရာမှာ လက်တွေ့ အသုံးချပြီး app ကို robust ဖြစ်အောင် ပြင်ဆင်မှာပါ။ ဒါအပြင် LINQ OrderBy() နဲ့ contacts list ကို name အလိုက် sort လုပ်ပြီး ပြသတဲ့အခါ ပိုသေချာသင့်တင့်စေမှာပါ။ ဒီ part ပြီးရင် Contact Book project က end-to-end အနေနဲ့ ပြည့်စုံသွားပါပြီ — Basic, Intermediate, Advanced chapters သုံးခုလုံးက concept တွေကို တစ်ခုတည်းသော project ထဲမှာ ပေါင်းစပ်အသုံးချနိုင်ခဲ့ပါပြီ။
- Components + state + services
Console.WriteLine("---- Contact List (sorted) ----");Polish အဆင့်တွင်validation consistency, exception boundaries, persistence corruption, logging, deterministic tests နဲ့user-friendly messages ကိုထည့်ပါ။ Save operation ကိုtemporary file + replace pattern ဖြင့်ပိုလုံခြုံစေပြီး secrets/personal data ကိုlogs မထည့်ပါနှင့်။
- စမ်းသပ်ပြီး UI
// }JSON persistence ကိုvalid, missing, empty, malformed file cases ဖြင့်test လုပ်ပြီး restart ပြီး data ပြန်တက်ကြောင်းစမ်းပါ။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Success
Project: Contact Book – Part 3 (Validation + Polish) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
UI ကexception အားလုံးကိုgeneric success/failure အဖြစ်ဖုံးခြင်းကdebugging ခက်ပြီးdata loss ကိုမသိစေပါ။
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Part 2 အထိ Contact Book က CRUD operations အားလုံး အလုပ်လုပ်နေပြီ ဖြစ်ပေမယ့် user က invalid input (empty name, wrong menu choice) ရိုက်ရင် app crash သွားနိုင်ပါတယ်။ Advanced chapter က exception handling (try/catch) concept ကို ဒီနေရာမှာ လက်တွေ့ အသုံးချပြီး app ကို robust ဖြစ်အောင် ပြင်ဆင်မှာပါ။ ဒါအပြင် LINQ OrderBy() နဲ့ contacts list ကို name အလိုက် sort လုပ်ပြီး ပြသတဲ့အခါ ပိုသေချာသင့်တင့်စေမှာပါ။ ဒီ part ပြီးရင် Contact Book project က end-to-end အနေနဲ့ ပြည့်စုံသွားပါပြီ — Basic, Intermediate, Advanced chapters သုံးခုလုံးက concept တွေကို တစ်ခုတည်းသော project ထဲမှာ ပေါင်းစပ်အသုံးချနိုင်ခဲ့ပါပြီ။
လက်တွေ့ ဆောက်ကြည့်မယ်
Custom exception class InvalidContactException ကို Exception ကနေ inherit ပြီး ရေးပါ။ AddContact() method ထဲမှာ Name သို့မဟုတ် Phone က empty ဖြစ်ရင် InvalidContactException throw လုပ်ပါ။ Main() method ရဲ့ Add branch ထဲမှာ try/catch နဲ့ ဒီ exception ကို catch ပြီး error message ကို console မှာ ပြပါ။ ShowAll() method ကို LINQ OrderBy(c => c.Name) သုံးပြီး name အက္ခရာစဉ် sort ဖြစ်အောင် ပြင်ပါ။ နောက်ဆုံးအနေနဲ့ Console.WriteLine() တွေနဲ့ output format ကို header/separator line ထည့်ပြီး ပိုကြည့်ကောင်းအောင် polish လုပ်ပါ။
Code နမူနာ
class InvalidContactException : Exception
{
public InvalidContactException(string message) : base(message) { }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
if (string.IsNullOrWhiteSpace(c.Name) || string.IsNullOrWhiteSpace(c.Phone))
{
throw new InvalidContactException("Name and Phone are required.");
}
contacts.Add(c);
}
public void ShowAll()
{
Console.WriteLine("---- Contact List (sorted) ----");
foreach (Contact c in contacts.OrderBy(c => c.Name))
{
Console.WriteLine($"{c.Name,-15} | {c.Phone,-12} | {c.Email}");
}
Console.WriteLine("--------------------------------");
}
}
// Main() ရဲ့ Add branch ထဲမှာ
// try
// {
// manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
// Console.WriteLine("Contact added successfully.");
// }
// catch (InvalidContactException ex)
// {
// Console.WriteLine("Error: " + ex.Message);
// }Empty name/phone နဲ့ contact add လုပ်ဖို့ ကြိုးစားရင် app crash မဖြစ်ဘဲ error message ပြပြီး၊ contact list ကို name အက္ခရာစဉ်အတိုင်း သင့်တင့်စွာ ပြနိုင်တဲ့ ပြီးပြည့်စုံတဲ့ Contact Book app ရရှိပါမယ်။၅ မိနစ် စမ်းကြည့်
Phone number format ကို regex သို့မဟုတ် simple length check နဲ့ validate လုပ်ပြီး၊ format မမှန်ရင် InvalidContactException ထပ်ထုတ်အောင် ပြင်ကြည့်ပါ။
သတိလေးတစ်ချက်
Exception ကို catch လုပ်ရုံနဲ့ silent ဖြစ်အောင် message မပြဘဲ ချန်ထားခြင်း မလုပ်ပါနဲ့ — user ကို ဘာမှားနေလဲ အမြဲ ပြောပြပါ။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
JSON persistence ကိုvalid, missing, empty, malformed file cases ဖြင့်test လုပ်ပြီး restart ပြီး data ပြန်တက်ကြောင်းစမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
JSON persistence ကိုvalid, missing, empty, malformed file cases ဖြင့်test လုပ်ပြီး restart ပြီး data ပြန်တက်ကြောင်းစမ်းပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
class InvalidContactException : Exception
{
public InvalidContactException(string message) : base(message) { }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
if (string.IsNullOrWhiteSpace(c.Name) || string.IsNullOrWhiteSpace(c.Phone))
{
throw new InvalidContactException("Name and Phone are required.");
}
contacts.Add(c);
}
public void ShowAll()
{
Console.WriteLine("---- Contact List (sorted) ----");
foreach (Contact c in contacts.OrderBy(c => c.Name))
{
Console.WriteLine($"{c.Name,-15} | {c.Phone,-12} | {c.Email}");
}
Console.WriteLine("--------------------------------");
}
}
// Main() ရဲ့ Add branch ထဲမှာ
// try
// {
// manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
// Console.WriteLine("Contact added successfully.");
// }
// catch (InvalidContactException ex)
// {
// Console.WriteLine("Error: " + ex.Message);
// }ရလဒ်: Project: Contact Book – Part 3 (Validation + Polish) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
JSON persistence ကိုvalid, missing, empty, malformed file cases ဖြင့်test လုပ်ပြီး restart ပြီး data ပြန်တက်ကြောင်းစမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Project requirements → Components + state + services → စမ်းသပ်ပြီး UI
- Polish အဆင့်တွင်validation consistency, exception boundaries, persistence corruption, logging, deterministic tests နဲ့user-friendly messages ကိုထည့်ပါ။ Save operation ကိုtemporary file + replace pattern ဖြင့်ပိုလုံခြုံစေပြီး secrets/personal data ကိုlogs မထည့်ပါနှင့်။
- UI ကexception အားလုံးကိုgeneric success/failure အဖြစ်ဖုံးခြင်းကdebugging ခက်ပြီးdata loss ကိုမသိစေပါ။
- JSON persistence ကိုvalid, missing, empty, malformed file cases ဖြင့်test လုပ်ပြီး restart ပြီး data ပြန်တက်ကြောင်းစမ်းပါ။
Unit testing C# in .NET — Microsoft Learn