上一篇文章我介绍了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里面实现实体之间的关系,
典型的主详细表结构,采购单