表子產品和領域模型比,有兩個顯著差別:
1:表子產品中的類和資料庫表基本一一對應,而領域模型則無此要求;
2:表子產品中的類的對象處理表中的所有記錄,而領域模型的一個對象代表表中的一行記錄;
一般情況下,我們可以基于第二點來嚴格區分你的設計是表子產品的,還是領域模型的。如:如果我們有許多訂單,則領域模型的每一個訂單都有一個對象,而表子產品隻有一個對象來處理所有訂單(注意,這裡的類,都是指業務邏輯層的類,而不是實體類。表子產品的類的對象和正常的領域模型的對象很類似,但是關鍵差別是:它沒有辨別符來标出它所代表的實體對象)。舉例來說,如果要查詢某個訂單,表子產品會像這樣進行編碼:
anOrderModule.GetOrder(string orderId);
因為表子產品隻有一個對象來處理所有訂單,是以表子產品可以是一個執行個體,也可以是一個隻包含靜态方法的靜态類。
表子產品 的代碼和 事務腳本類似:
class TableModel { protected DataTable table; protected TableModel(DataSet ds, string tableName) { table = ds.Tables[tableName]; } } class Contract : TableModel public Contract(DataSet ds) : base (ds, "Contracts") public DataRow this[long id] get { string filter = "ID=" + id; return table.Select(filter)[0]; } public void CalculateRecognitions(long contactId) DataRow contractRow = this[contactId]; double amount = (double)contractRow["amount"]; RevenueRecognition rr = new RevenueRecognition(table.DataSet); Product prod = new Product(table.DataSet); long prodId = GetProductId(contactId); if(prod.GetProductType(prodId) == "W") rr.Insert(contactId, amount, (DateTime)GetWhenSigned(contactId)); }else if(prod.GetProductType(prodId) == "S") // 電子表格類 // the sql "INSERT INTO REVENUECONGNITIONS (CONTRACT,AMOUNT,RECOGNIZEDON) VALUES (?,?,?)" DateTime dateSigned = (DateTime)GetWhenSigned(contactId); rr.Insert(contactId, amount / 3, dateSigned); rr.Insert(contactId, amount / 3, dateSigned.AddDays(60)); rr.Insert(contactId, amount / 3, dateSigned.AddDays(90)); }else if(prod.GetProductType(prodId) == "D") // 資料庫 { rr.Insert(contactId, amount / 3, dateSigned.AddDays(30)); private long GetProductId(long contactId) return (long)this[contactId]["ProductId"]; private DateTime GetWhenSigned(long contactId) return (DateTime)this[contactId]["DateSigned"]; class RevenueRecognition : TableModel public RevenueRecognition(DataSet ds) : base (ds, "RevenueRecognitions") public long Insert(long contactId, double amount, DateTime whenSigned) DataRow newRow = table.NewRow(); long id = GetNextId(); newRow["Id"] = id; newRow["ContactId"] = contactId; newRow["Amount"] = amount; newRow["DateSigned"] = whenSigned; table.Rows.Add(newRow); return id; // 得到哪天前入賬了多少 public double RecognizedRevenue(long contractNumber, DateTime asOf) // the sql "SELECT AMOUNT FROM REVENUECONGNITIONS WHERE CONTRACT=? AND RECOGNIZEDON <=?"; string filter = string.Format("ContactId={0} AND date <=#{1:d}", contractNumber, asOf); DataRow[] rows = table.Select(filter); double r = 0.0; foreach(DataRow dr in rows) r += (double)dr["AMOUNT"]; return r; private long GetNextId() throw new Exception(); class Product : TableModel public Product(DataSet ds) : base (ds, "Products") public string GetProductType(long productId) return (string)this[productId]["Type"]; 本文轉自最課程陸敏技部落格園部落格,原文連結:http://www.cnblogs.com/luminji/p/3703836.html,如需轉載請自行聯系原作者