Veri tabanını modelini bir onceki yazımızda oluşturduğumuza göre veri tabanı görünümün için yeni bir sınıf oluşturalım ve DBView.cs olarak adlandıralım. Bu sınıf artık oluşturduğumuz model sınıfını kullanır,ileriki bölümlerde oluşturacağımız tüm sayfalarda Model sınıfını kullanır bunun için sınıfımıza using MyShopping.Model; ifadesini ekleriz.
ObservableCollection ‘lar
oluşturulur.Örnek bir koleksiyon tanımlaması aşağıdaki gibidir.
Oluşturulan sınıf burada :)
DBView de
data context tanımlaması ve datacontext nesnesinin üretilmesi için DBView
yapılandırıcısında aşağıdaki kodlar yer alır.
// LINQ to SQL data context for the
local database.
public DBDataContext
DBShop;
// Class constructor, create the data context object.
public DBView(string
DBConnectionString)
{
DBShop = new DBDataContext(DBConnectionString);
}
private ObservableCollection<TProduct> _AllProductItems;
public ObservableCollection<TProduct> AllProductItems
{
get { return
_AllProductItems; }
set
{
_AllProductItems = value;
NotifyPropertyChanged("AllProductItems");
}
}
Bu şekilde gerekli olan koleksiyonlar
tanımlanarak t-sql sorguları yardımı ile koleksiyonlar oluşturulur.Örneğin AllProductItems
koleksiyonu aşağıdaki şekilde bir Lınq to SQL sorgusu ile ilişkilendirip
oluşturabiliriz.
var ProducsInDB = from TProduct product in
DBShop.Products join TList
listr in DBShop.PLists on
product._PListId equals listr.Id select product;
AllProductItems = new
ObservableCollection<TProduct>(ProducsInDB);
Ürünler koleksiyonu oluşturulurken yabancı anahtar ilişkisi
unutulmamalıdır dolaysı ile t-sql sorgusunda da buna önem verilmelidir.Diğer
Lınq to SQL sorgularında yabancı anahtar ilişkisi olmamakta ve daha sade bir
sorgulama yer almaktadır.
var ListsInDB = from TList list in
DBShop.PLists
select list;
var StoresInDB = from TStore store in
DBShop.Stores
select store;
ProductLists = new ObservableCollection<TList>(ListsInDB);
StoreLists = new ObservableCollection<TStore>(StoresInDB);
Bu koleksiyonları kullanarak Add Product,List,Store ya da Delete
Product,List,Store metotları yazabiliriz.
public void AddList(TList newList)
{
// Add new shop list to the data context.
DBShop.PLists.InsertOnSubmit(newList);
// Save changes to the database.
DBShop.SubmitChanges();
// Add new shop list to
observable collection.
ProductLists.Add(newList);
}
public void DeleteList(TList ListDelete)
{
// Remove shop list from the observable collection.
ProductLists.Remove(ListDelete);
// Remove shop list from the data context.
DBShop.PLists.DeleteOnSubmit(ListDelete);
// Save changes to the database.
DBShop.SubmitChanges();
}
Hiç yorum yok:
Yorum Gönderme