ObjectSpaces這個ORM框架可能已經被大家聽過N次了
ObjectSpaces的流傳版本也是甚多
從版本號的變更就可以看出
進入正題
我們這裡將IBuySpy的Announcement模塊的DAL用ObjectSpaces重新寫過
一
DAL
界面控件頁面
裡面都是最最標准的寫法
二
新創建一個Announcement類和一個AnnouncementCollection集合類
三
ObjectSpaces的核心就是用來描述Schema的三個xml文件
一個描述數據庫表結構的Relational Schema Definition
用ObjectSpace最主要(也是最煩人)的工作就是把這三個Schema寫出來
AnnouncementRSD
AnnouncementOSD
AnnouncementMSD
四
創建一個新的類文件AnnouncementOSDB
根據moduleId返回Announcement集合
public AnnouncementCollection GetAnnouncements(int moduleId)
{
ObjectSpace os = new ObjectSpace(_sMapFilePath
// 條件是ModuleID等於參數moduleId
ObjectQuery query = new ObjectQuery(typeof(Announcement)
// 取數據
ObjectReader reader = os
AnnouncementCollection result = new AnnouncementCollection();
// 從ObjectReader中取值不需要另外造型
foreach (Announcement ann in reader)
{
result
}
return result;
}
根據參數返回一個Announcement
public Announcement GetSingleAnnouncement(int itemId)
{
ObjectSpace os = new ObjectSpace(_sMapFilePath
ObjectQuery query = new ObjectQuery(typeof(Announcement)
return (Announcement) os
}
[NextPage]
根據參數刪除一個Announcement:
public void DeleteAnnouncement(int itemId)
{
ObjectSpace os = new ObjectSpace(_sMapFilePath
ObjectQuery query = new ObjectQuery(typeof(Announcement)
Announcement ann = (Announcement) os
os
os
}
新增一個Announcement
public void AddAnnouncement(int moduleId
{
Announcement ann = new Announcement();
ann
ann
ann
ann
…
ObjectSpace os = new ObjectSpace(_sMapFilePath
os
os
}
修改一個Announcement
public void UpdateAnnouncement(int moduleId
{
ObjectSpace os = new ObjectSpace(_sMapFilePath
ObjectQuery query = new ObjectQuery(typeof(Announcement)
Announcement ann = (Announcement) os
ann
ann
…
os
}
五
原本界面層是把AnnouncementDB返回的DataSet綁定到Web Control上
比如
原本將數據取出並綁定的代碼(在Announcemenets
AnnouncementsDB announcements = new AnnouncementsDB();
myDataList
myDataList
只要將第一句改成
AnnouncementOSDB announcements = new AnnouncementOSDB();
實際上就是改從哪個DAL取數據就OK了
六
ObjectSpaces的架構是這樣的
ObjectSpace類管理數據映射
我上面的代碼演示目的是為了展示ObjectSpaces
不過如果你也試著用ObjectSpaces來重建你的項目中的DAL
Kaneboy
From:http://tw.wingwit.com/Article/program/ASP/201311/21831.html