上一篇文章我介紹了DC並引入了官方對DC描述和特性,下面開始,我就來以實際的代碼項目來逐步介紹一個用DC實現的採購系統,
首先,我們先用微軟IDE工具VS2005/2008/2010來建立一個XAF的應用程式,我這裡用vs2010來建立一個xaf的winform傳統型應用程式MyPurchaseSystem,如:
為了方便組織DC檔案,我這裡把DC檔案統一放入Entities檔案夾內,下面我們開始來設計定義DC,在開始之前,我們先來瞭解DC的構成,我們這裡先來定義一個"貨品資訊"DC:
C#代碼
1 [DomainComponent()]
2 [NavigationItem("基本資料")]
3 [XafDisplayName("貨品資料")]
4 [XafDefaultProperty("GoodsCode")]
5 public interface iGoodsInfo
6 {
7 [RuleUniqueValue("", DefaultContexts.Save)]
8 [RuleRequiredField("", DefaultContexts.Save)]
9 [VisibleInLookupListView(true)]
10 [XafDisplayName("貨品編碼")]
11 string GoodsCode { get; set; }
12 [XafDisplayName("貨品名稱")]
13 [RuleRequiredField("", DefaultContexts.Save)]
14 string GoodsName { get; set; }
15 [XafDisplayName("貨品規格")]
16 string GoodsSpec { get; set; }
17 }
VB代碼
1 <DomainComponent()> _
2 <NavigationItem("基本資料")> _
3 <XafDisplayName("貨品資料")> _
4 <XafDefaultProperty("GoodsCode")> _
5 Public Interface iGoodsInfo
6 <RuleUniqueValue("", DefaultContexts.Save)> _
7 <RuleRequiredField("", DefaultContexts.Save)> _
8 <VisibleInLookupListView(True)> _
9 <XafDisplayName("貨品編碼")> _
10 Property GoodsCode() As String
11 <XafDisplayName("貨品名稱")> _
12 <RuleRequiredField("", DefaultContexts.Save)> _
13 Property GoodsName() As String
14 <XafDisplayName("貨品規格")> _
15 Property GoodsSpec() As String
16 End Interface
上面的代碼大家會發現,這其實就是一個interface,但是在interface的基礎上有了一些特殊的attribute,
DomainComponent定義了這個interface是一個DC,
<NavigationItem("基本資料")> 定義了這個DC在UI中的導航組為"基本資料"
XafDisplayName定義DC的可本地化顯示名稱(包括成員名稱
Property .... 定義DC的成員(表欄位)
RuleUniqueValue定義了該欄位的唯一性驗證規則
RuleRequiredField 定義了該欄位必須輸入字元
下面的代碼對當前定義的DC在XAF的Module中進行註冊,以便DC能夠按照預期效果運行:
Public Overrides Sub Setup(ByVal application As XafApplication)
If (Not XafTypesInfo.IsInitialized) Then
XafTypesInfo.Instance.AddEntityToGenerate("MyComponent", GetType(iGoodsInfo), GetType(BaseObject))
End If
MyBase.Setup(application)
End Sub
現在我們來運行下程式,看看有什麼效果
我們看到程式已經按照我們預期的效果運行了,實現這個效果,只要不超過3分鐘時間,如果我們平時完全自己開發這個效果,大家應該知道要多少時間了,好,這篇文章我們瞭解了DC的基本定義方法,下一篇,我們將瞭解在DC裡面實現實體之間的關係,
典型的主詳細表結構,採購單