Phone va Smartphone - Dasturiy injiniring (EntityFramework 6)

Phone va Smartphone klasslarda barcha xususiyatlardan tashqari Discriminator ustuni ham mavjud bo’lib, u nvarchar(128) tipiga mansub qiymatni o’zida saqlaydi. Ushbu ustun jadval qiymatining Phone yoki Smartphone tipiga mansubligini ifodalaydi. Ushbu klasslardan dasturda quyidagicha foydalaniladi:
using (MobileContext db = new MobileContext())
{
db.Phones.Add(new Phone { Name = "Samsung Galaxy S5", Company = "Samsung", Price = 14000 });
db.Phones.Add(new Phone { Name = "Nokia Lumia 630", Company = "Nokia", Price = 8000 });
Smartphone s1 = new Smartphone { Name = "iPhone 6", Company = "Apple", Price = 32000, OS = "iOS" };
db.Smarts.Add(s1);
db.SaveChanges();
foreach (Phone p in db.Phones)
Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
Console.WriteLine();
foreach (Smartphone p in db.Smarts)
Console.WriteLine("{0} ({1}, {2}) - {3}", p.Name, p.Company, p.Price, p.OS);
}
Natijada konsolga quyidagi natija hosil qilinadi:
Smartphone ob’ekti o’z navbatida Phone ob’ekti hisoblanganligi sababli, unga ham db.Phones orqali murojaat qilish mumkin.
TPT yondashuvi
TPT yondashuvi asosida jadvalda bosh klassda aniqlangan hamda vorislangan xususiyatlar jadvalda saqlanadi. Faqat vorislangan klassga mansub qiymatlar esa alohida jadvalda saqlanadi.
Ushbu yondashuvni amalga oshirish uchun avvalgi mavzuda keltirilgan Smartphone klassiga Table atributini qo’llaymiz:
public class Phone
{
public int Id { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public int Price { get; set; }
}
[Table("Smartphones")]
public class Smartphone : Phone
{
public string OS { get; set; }
}
class MobileContext : DbContext
{
public MobileContext()
: base("DefaultConnection")
{ }
public DbSet Phones { get; set; }
public DbSet Smarts { get; set; }
}
Qolgan amallar TPH yondashuviga o’xshash tarzda amalga oshiriladi. Ammo TPT yondashuviga mos ma’lumotlar bazasidagi jadvallar quyidagicha shakllantiriladi:
Smartphonlarni o’zida saqlovchi jadval faqat bitta OS maydonidan iborat bo’lib, Id maydoni orqali Phones jadvali bilan bog‘langan.
using (MobileContext db = new MobileContext())
{
db.Phones.Add(new Phone { Name = "Samsung Galaxy S5", Company = "Samsung", Price = 14000 });
db.Phones.Add(new Phone { Name = "Nokia Lumia 630", Company = "Nokia", Price = 8000 });
Smartphone s1 = new Smartphone { Name = "iPhone 6", Company = "Apple", Price = 32000, OS = "iOS" };
db.Smarts.Add(s1);
db.SaveChanges();
foreach (Phone p in db.Phones)
Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
Console.WriteLine();
foreach (Smartphone p in db.Smarts)
Console.WriteLine("{0} ({1}, {2}) - {3}", p.Name, p.Company, p.Price, p.OS);
}
TPС yondashuvi
TPC yondashuvida har bir model uchun alohida jadvalni shakllantirish amalga oshiriladi. Har bir jadval ustunlari modelning xususiyatlari mos holda shakllantiriladi. Ushbu yondashuvni tadbiq qilish uchun model va ma’lumotlar konteksti quyidagicha hosil qilinadi:
public class Phone
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public int Price { get; set; }
}
public class Smartphone : Phone
{
public string OS { get; set; }
}
class MobileContext : DbContext
{
public MobileContext()
: base("DefaultConnection")
{ }
public DbSet Phones { get; set; }
public DbSet Smarts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("Phones");
});
modelBuilder.Entity().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("Smarts");
});
}
}
Yuqoridagi kodda Phone klassida kalit sifatida int o’rniga Guid dan foydalanilgan. Ushbu usul kalit bilan bog‘liq bo’lgan ba’zi noqulayliklarni oldini oladi. Shuningdek, model va jadvalni moslashtirishda har bir modelda MapInheritedProperties() metodi chaqirilgan. Ushbu metodda modellar vorislanganda ular o'rtasidagi aloqani qayta aniqlash amalga oshiriladi.
TPC yondashuvi asosida ma’lumotlar bazasi generatsiya qilinganda ma’lumotlar bazasida bir xil tuzilmaga ega ikkita jadval hosil qilinadi:
Yuqorida keltirilgan modeldan quyidagicha foydalanish mumkin:
using (MobileContext db = new MobileContext())
{
db.Phones.Add(new Phone { Name = "Samsung Galaxy S5", Company = "Samsung", Price = 14000 });
db.Phones.Add(new Phone { Name = "Nokia Lumia 630", Company = "Nokia", Price = 8000 });
Smartphone s1 = new Smartphone { Name = "iPhone 6", Company = "Apple", Price = 32000, OS = "iOS" };
db.Smarts.Add(s1);
db.SaveChanges();
foreach (Phone p in db.Phones)
Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
Console.WriteLine();
foreach (Smartphone p in db.Smarts)
Console.WriteLine("{0} ({1}, {2}) - {3}", p.Name, p.Company, p.Price, p.OS);
}
|