ရိုးရိုးလေး ရှင်းပြမယ်
Project: Contact Book – Part 1 (Setup + Core Structure) ကို ရိုးရိုးလေးနားလည်မယ်
ဒီ project မှာ Contacts (name, phone, email) တွေကို သိမ်းထား၊ ပြောင်းလဲနိုင်တဲ့ simple Contact Book console app တစ်ခု တည်ဆောက်သွားမှာပါ။ Basic chapter က class, object, property, constructor concepts တွေကို ပြန်အသုံးချပြီး, real-world data structure တစ်ခုကို model လုပ်နည်း လေ့လာရမှာပါ။ Part 1 မှာတော့ project ရဲ့ foundation ဖြစ်တဲ့ Contact class နဲ့ contacts တွေကို List ထဲမှာ သိမ်းမယ့် ContactManager class ကို ဦးဆုံး ရေးမှာပါ။ ဒီ structure ကောင်းကောင်း တည်ဆောက်ထားရင် နောက် part တွေမှာ feature တွေ ထပ်ဖြည့်ရတာ လွယ်ကူသွားပါမယ်။ Console.ReadLine() နဲ့ user input ကို လက်ခံပြီး Add Contact feature အခြေခံကို လုပ်ဆောင်ကြည့်ပါမယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Phone သို့name ကိုobject ဘယ်နေရာမဆိုinvalid value ထည့်နိုင်အောင်ထားခြင်းကvalidation ပျံ့နှံ့စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Contact Book setup တွင်Contact entity, stable ID, validation rules, repository contract နဲ့application service boundary ကိုအရင်သတ်မှတ်ပါ။ Console UI ကdomain objects ကိုတိုက်ရိုက်မပြင်ဘဲservice ကိုခေါ်သင့်ပါတယ်။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Project: Contact Book – Part 1 (Setup + Core Structure) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class Contact
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
contacts.Add(c);
Console.WriteLine("Contact added: " + c.Name);
}
public void ShowAll()
{
Console.WriteLine("--- Contact List ---");
foreach (Contact c in contacts)
{
Console.WriteLine($"{c.Name} | {c.Phone} | {c.Email}");
}
}
}
class Program
{
static void Main(string[] args)
{
ContactManager manager = new ContactManager();
bool running = true;
while (running)
{
Console.WriteLine("\n1. Add Contact 2. Show All 0. Exit");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("Phone: ");
string phone = Console.ReadLine();
Console.Write("Email: ");
string email = Console.ReadLine();
manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
}
else if (choice == "2")
{
manager.ShowAll();
}
else if (choice == "0")
{
running = false;
}
}
}
}Contact Book setup တွင်Contact entity, stable ID, validation rules, repository contract နဲ့application service boundary ကိုအရင်သတ်မှတ်ပါ။ Console UI ကdomain objects ကိုတိုက်ရိုက်မပြင်ဘဲservice ကိုခေါ်သင့်ပါတယ်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Project requirements
class Contactဒီ project မှာ Contacts (name, phone, email) တွေကို သိမ်းထား၊ ပြောင်းလဲနိုင်တဲ့ simple Contact Book console app တစ်ခု တည်ဆောက်သွားမှာပါ။ Basic chapter က class, object, property, constructor concepts တွေကို ပြန်အသုံးချပြီး, real-world data structure တစ်ခုကို model လုပ်နည်း လေ့လာရမှာပါ။ Part 1 မှာတော့ project ရဲ့ foundation ဖြစ်တဲ့ Contact class နဲ့ contacts တွေကို List ထဲမှာ သိမ်းမယ့် ContactManager class ကို ဦးဆုံး ရေးမှာပါ။ ဒီ structure ကောင်းကောင်း တည်ဆောက်ထားရင် နောက် part တွေမှာ feature တွေ ထပ်ဖြည့်ရတာ လွယ်ကူသွားပါမယ်။ Console.ReadLine() နဲ့ user input ကို လက်ခံပြီး Add Contact feature အခြေခံကို လုပ်ဆောင်ကြည့်ပါမယ်။
- Components + state + services
ContactManager manager = new ContactManager();Contact Book setup တွင်Contact entity, stable ID, validation rules, repository contract နဲ့application service boundary ကိုအရင်သတ်မှတ်ပါ။ Console UI ကdomain objects ကိုတိုက်ရိုက်မပြင်ဘဲservice ကိုခေါ်သင့်ပါတယ်။
- စမ်းသပ်ပြီး UI
}Solution ကိုApp, Core နဲ့Tests projects အဖြစ်ခွဲပြီး in-memory repository ဖြင့်add/list vertical slice တစ်ခု run ပါ။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Success
Project: Contact Book – Part 1 (Setup + Core Structure) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Phone သို့name ကိုobject ဘယ်နေရာမဆိုinvalid value ထည့်နိုင်အောင်ထားခြင်းကvalidation ပျံ့နှံ့စေပါတယ်။
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ project မှာ Contacts (name, phone, email) တွေကို သိမ်းထား၊ ပြောင်းလဲနိုင်တဲ့ simple Contact Book console app တစ်ခု တည်ဆောက်သွားမှာပါ။ Basic chapter က class, object, property, constructor concepts တွေကို ပြန်အသုံးချပြီး, real-world data structure တစ်ခုကို model လုပ်နည်း လေ့လာရမှာပါ။ Part 1 မှာတော့ project ရဲ့ foundation ဖြစ်တဲ့ Contact class နဲ့ contacts တွေကို List ထဲမှာ သိမ်းမယ့် ContactManager class ကို ဦးဆုံး ရေးမှာပါ။ ဒီ structure ကောင်းကောင်း တည်ဆောက်ထားရင် နောက် part တွေမှာ feature တွေ ထပ်ဖြည့်ရတာ လွယ်ကူသွားပါမယ်။ Console.ReadLine() နဲ့ user input ကို လက်ခံပြီး Add Contact feature အခြေခံကို လုပ်ဆောင်ကြည့်ပါမယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
Console App project အသစ်တစ်ခု create လုပ်ပြီး Contact.cs ထဲမှာ Name, Phone, Email properties တွေပါတဲ့ Contact class ကို ရေးပါ။ ContactManager class ထဲမှာ List<Contact> field တစ်ခု ထားပြီး AddContact() method နဲ့ ShowAll() method နှစ်ခု ရေးပါ။ Main() method ထဲမှာ simple menu loop (while loop) ဆောက်ပြီး user ကနေ "1. Add Contact" ရွေးရင် Console.ReadLine() နဲ့ name/phone/email ကို ဖတ်ပြီး AddContact() ကို ခေါ်ပါ။ "2. Show All" ရွေးရင် ShowAll() ကို ခေါ်ပြီး contacts list ကို console မှာ ပြပါ။ "0. Exit" နဲ့ loop ကနေ ထွက်နိုင်အောင် condition ထည့်ပါ။
Code နမူနာ
class Contact
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
contacts.Add(c);
Console.WriteLine("Contact added: " + c.Name);
}
public void ShowAll()
{
Console.WriteLine("--- Contact List ---");
foreach (Contact c in contacts)
{
Console.WriteLine($"{c.Name} | {c.Phone} | {c.Email}");
}
}
}
class Program
{
static void Main(string[] args)
{
ContactManager manager = new ContactManager();
bool running = true;
while (running)
{
Console.WriteLine("\n1. Add Contact 2. Show All 0. Exit");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("Phone: ");
string phone = Console.ReadLine();
Console.Write("Email: ");
string email = Console.ReadLine();
manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
}
else if (choice == "2")
{
manager.ShowAll();
}
else if (choice == "0")
{
running = false;
}
}
}
}Console မှာ menu ပေါ်ပြီး Contact တွေကို add လုပ်နိုင်၊ list အနေနဲ့ ပြန်ကြည့်နိုင်တဲ့ working console app ရရှိမှာ ဖြစ်ပါတယ်။၅ မိနစ် စမ်းကြည့်
Contact class ထဲမှာ Category ("Family", "Work", "Friend") property တစ်ခု ထပ်ဖြည့်ပြီး, ShowAll() ထဲမှာ category ပါအောင် ပြပါ။
သတိလေးတစ်ချက်
Menu loop ရေးတဲ့အခါ Exit condition ကို အမှန်ထားဖို့ သတိထားပါ၊ မဟုတ်ရင် infinite loop ဖြစ်သွားနိုင်ပါတယ်။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Solution ကိုApp, Core နဲ့Tests projects အဖြစ်ခွဲပြီး in-memory repository ဖြင့်add/list vertical slice တစ်ခု run ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Solution ကိုApp, Core နဲ့Tests projects အဖြစ်ခွဲပြီး in-memory repository ဖြင့်add/list vertical slice တစ်ခု run ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
class Contact
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
class ContactManager
{
private List<Contact> contacts = new List<Contact>();
public void AddContact(Contact c)
{
contacts.Add(c);
Console.WriteLine("Contact added: " + c.Name);
}
public void ShowAll()
{
Console.WriteLine("--- Contact List ---");
foreach (Contact c in contacts)
{
Console.WriteLine($"{c.Name} | {c.Phone} | {c.Email}");
}
}
}
class Program
{
static void Main(string[] args)
{
ContactManager manager = new ContactManager();
bool running = true;
while (running)
{
Console.WriteLine("\n1. Add Contact 2. Show All 0. Exit");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("Phone: ");
string phone = Console.ReadLine();
Console.Write("Email: ");
string email = Console.ReadLine();
manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
}
else if (choice == "2")
{
manager.ShowAll();
}
else if (choice == "0")
{
running = false;
}
}
}
}ရလဒ်: Project: Contact Book – Part 1 (Setup + Core Structure) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Solution ကိုApp, Core နဲ့Tests projects အဖြစ်ခွဲပြီး in-memory repository ဖြင့်add/list vertical slice တစ်ခု run ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Project requirements → Components + state + services → စမ်းသပ်ပြီး UI
- Contact Book setup တွင်Contact entity, stable ID, validation rules, repository contract နဲ့application service boundary ကိုအရင်သတ်မှတ်ပါ။ Console UI ကdomain objects ကိုတိုက်ရိုက်မပြင်ဘဲservice ကိုခေါ်သင့်ပါတယ်။
- Phone သို့name ကိုobject ဘယ်နေရာမဆိုinvalid value ထည့်နိုင်အောင်ထားခြင်းကvalidation ပျံ့နှံ့စေပါတယ်။
- Solution ကိုApp, Core နဲ့Tests projects အဖြစ်ခွဲပြီး in-memory repository ဖြင့်add/list vertical slice တစ်ခု run ပါ။
Unit testing C# in .NET — Microsoft Learn