iT邦幫忙

DAY 3
1

Kuick Hands on Labs系列 第 3

Kuick Hands on Labs -- 3. 欄位定義與資料關連

  • 分享至 

  • twitterImage
  •  

昨日範例程式裡的Entity只是列出資料欄位,今天讓我們開始在欄位上增加一些商業邏輯,首先是資料之間的關連。
首先,請下載今天的範例程式碼 Labs-2013-10-03

1. 使用 ColumnRefer Attribute 定義資料之間的關連
ColumnRefer 是用來定義資料表之間邏輯上的外部鍵,下面例出這份範例裡包含的邏輯關連:
1). 「商品.品牌代碼」 參照 「品牌.品牌代碼」
ProductEntity.BrandID --> BrandEntity.BrandID

[DataMember]
[Description("品牌")]
[ColumnSpec(SpecFlag.NotAllowNull)]
[ColumnRefer(typeof(BrandEntity))] // 「商品.品牌代碼」 參照 「品牌.品牌代碼」
public string BrandID { get; set; }

2). 「訂單.客戶代碼」 參照 「客戶.客戶代碼」
OrderEntity.CustomerID --> CustomerEntity.CustomerID

[DataMember]
[Description("客戶")]
[ColumnSpec(SpecFlag.NotAllowNull)]
[ColumnRefer(typeof(CustomerEntity))] // 「訂單.客戶代碼」 參照 「客戶.客戶代碼」
public string CustomerID { get; set; }

3). 「訂單商品.訂單代碼」 參照 「訂單.訂單代碼」
OrderProductEntity.OrderID --> OrderEntity.OrderID

[DataMember]
[Description("訂單")]
[ColumnSpec(SpecFlag.NotAllowNull)]
[ColumnRefer(typeof(OrderEntity))] // 「訂單商品.訂單代碼」 參照 「訂單.訂單代碼」
public string OrderID { get; set; }

4). 「訂單商品.商品代碼」 參照 「商品.商品代碼」
OrderProductEntity.ProductID --> ProductEntity.ProductID

[DataMember]
[Description("商品")]
[ColumnSpec(SpecFlag.NotAllowNull)]
[ColumnRefer(typeof(ProductEntity))] // 「訂單商品.商品代碼」 參照 「商品.商品代碼」
public string ProductID { get; set; }

2. 使用Entity CRUD事件,維護資料之間的關連
商業邏輯:如果還有商品參照品牌資料,此品牌資料不可刪除
實作事件:BrandEntity.BeforeRemove

public BrandEntity()
	: base()
{
	base.BeforeRemove += new EntityEventHandler(BrandEntity_BeforeRemove);
}

private void BrandEntity_BeforeRemove(IEntity sender, EntityEventArgs e)
{
	if(!e.Success) { return; }
 
	if(ProductEntity.Exists(x => x.BrandID == this.BrandID)) {
		e.Add(
			new Result(false, "刪除品牌資料前,需先移除所有商品的參照。")
		);
	}
}

商業邏輯:曾建立過訂單的客戶資料不可刪除
實作事件:CustomerEntity.BeforeRemove

public CustomerEntity()
	: base()
{
	base.BeforeRemove += new EntityEventHandler(CustomerEntity_BeforeRemove);
}

private void CustomerEntity_BeforeRemove(IEntity sender, EntityEventArgs e)
{
	if(!e.Success) { return; }
 
	if(OrderEntity.Exists(x => x.CustomerID == this.CustomerID)) {
		e.Add(
			new Result(false, "曾建立過訂單的客戶資料不可刪除,如有需要請改設定成停用。")
		);
	}
}

商業邏輯:刪除訂單之前,先刪除訂單商品資料
實作事件:OrderEntity.BeforeRemove

public OrderEntity()
	: base()
{
	base.BeforeRemove += new EntityEventHandler(OrderEntity_BeforeRemove);
}

private void OrderEntity_BeforeRemove(IEntity sender, EntityEventArgs e)
{
	if(!e.Success) { return; }
 
	OrderProductEntity.Remove(x => x.OrderID == this.OrderID);
}

商業邏輯:曾出售過的商品資料不可刪除
實作事件:ProductEntity.BeforeRemove

public ProductEntity()
	: base()
{
	base.BeforeRemove += new EntityEventHandler(ProductEntity_BeforeRemove);
}

private void ProductEntity_BeforeRemove(IEntity sender, EntityEventArgs e)
{
	if(!e.Success) { return; }
 
	if(OrderProductEntity.Exists(x => x.ProductID == this.ProductID)) {
		e.Add(
			new Result(false, "曾出售過的商品資料不可刪除,如有需要請改設定成停用。")
		);
	}
}

上一篇
Kuick Hands on Labs -- 2. ER-Diagram
下一篇
Kuick Hands on Labs -- 4. Many-to-Many 資料多對多關連
系列文
Kuick Hands on Labs30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言