eXpressApp Framework 之DC技术入门系列(2)DC中的实体关系之一对多关系
在现实世界里面,我们遇到很多种错综复杂的关系,这些关系概括起来主要分为以下几种关系:
一对多关系,多对多关系,一对一关系,这些关系是在关系型数据库应用系统中应用广泛,DC作为一种ORM技术,它又是如何实现的呢?这里的一对一关系很简单,我就不介绍了,重点介绍复杂的一对多和多对多关系的实现,我在上一篇文章中只实现了货品资料这一个实体,作为一个完整的采购系统,还要涉及到采购询价,采购单,采购收货以及供应商资料等,这些实现DC的方法请参考上一篇文章:eXpressApp Framework 之DC技术入门系列(1)用DC实现现实中的实体,下面我就不多描述了,接下来我直接把代码先贴出来,然后再来介绍这里包含的上面两种关系,
为了实现我们后续的工作,我们先实现三个DC:iSupplier,iPurchaseHeaderBase,iPurchaseDetailBase,这三个DC的作用是有区别的,
iSupplier是供应商资料,因此需要注册在xaf应用中,而iPurchaseHeaderBase和iPurchaseDetailBase仅利用了inherite特性来简化后续的DC代码量,提高工作效率,同时也方便代码维护之用,因此不需要在xaf应用中进行注册,这三个DC的具体代码如下:
VB代码:
'iSupplier代码:
1 <DomainComponent()> _
2 <NavigationItem("基本资料")> _
3 <XafDisplayName("供应商资料")> _
4 <XafDefaultProperty("SupplierName")> _
5 Public Interface iSupplier
6 <XafDisplayName("供应商编号")> _
7 Property SupplierCode As String
8 <XafDisplayName("供应商名称")> _
9 Property SupplierName As String
10
11 End Interface
2 <NavigationItem("基本资料")> _
3 <XafDisplayName("供应商资料")> _
4 <XafDefaultProperty("SupplierName")> _
5 Public Interface iSupplier
6 <XafDisplayName("供应商编号")> _
7 Property SupplierCode As String
8 <XafDisplayName("供应商名称")> _
9 Property SupplierName As String
10
11 End Interface
' iPurchaseHeaderBase代码:
1 <DomainComponent()> _
2 Public Interface iPurchaseHeaderBase
3 <XafDisplayName("供应商")> _
4 Property Supplier As iSupplier
5 <XafDisplayName("单据编号")> _
6 Property BillCode As String
7 <XafDisplayName("开单日期")> _
8 Property BillDate As DateTime
9 <XafDisplayName("备注")> _
10 Property BillRemark As String
11
12 End Interface
2 Public Interface iPurchaseHeaderBase
3 <XafDisplayName("供应商")> _
4 Property Supplier As iSupplier
5 <XafDisplayName("单据编号")> _
6 Property BillCode As String
7 <XafDisplayName("开单日期")> _
8 Property BillDate As DateTime
9 <XafDisplayName("备注")> _
10 Property BillRemark As String
11
12 End Interface
iPurchaseDetailBase代码:
1 <DomainComponent()> _
2 Public Interface iPurchaseDetailBase
3
4 <XafDisplayName("编码")> _
5 Property GoodsCode As iGoodsInfo
6 <XafDisplayName("品名")> _
7 ReadOnly Property GoodsName As String
8 <XafDisplayName("规格")> _
9 ReadOnly Property GoodsSpec As String
10 <XafDisplayName("数量")> _
11 Property Qty As Double
12
13 End Interface
2 Public Interface iPurchaseDetailBase
3
4 <XafDisplayName("编码")> _
5 Property GoodsCode As iGoodsInfo
6 <XafDisplayName("品名")> _
7 ReadOnly Property GoodsName As String
8 <XafDisplayName("规格")> _
9 ReadOnly Property GoodsSpec As String
10 <XafDisplayName("数量")> _
11 Property Qty As Double
12
13 End Interface
好了,我们建立了这三个DC后,我们就可以建立其他的实体DC了,我们先来实现采购询价单,看看是什么样子,采购询价单基本由两部分组成iPurchaseQuoteHeader和iPurchaseQuoteDetail两部分组成,分别代表采购询价单单据头和单据体,代码如下:
1 <DomainComponent()> _
2 <NavigationItem("采购系统")> _
3 <XafDisplayName("采购询价单")> _
4 <XafDefaultProperty("BillCode")> _
5 Public Interface iPurchaseQuoteHeader
6 Inherits iPurchaseHeaderBase
7 <DevExpress.ExpressApp.DC.Aggregated()> _
8 <XafDisplayName("采购询价单明细项")> _
9 ReadOnly Property PurchaseQuoteDetail As IList(Of iPurchaseQuoteDetail)
10 End Interface
11 <DomainComponent()> _
12 <XafDisplayName("采购询价单明细项")> _
13 Public Interface iPurchaseQuoteDetail
14 Inherits iPurchaseDetailBase
15 <XafDisplayName("采购询价单")> _
16 Property PurchaseQuote As iPurchaseQuoteHeader
17 End Interface
2 <NavigationItem("采购系统")> _
3 <XafDisplayName("采购询价单")> _
4 <XafDefaultProperty("BillCode")> _
5 Public Interface iPurchaseQuoteHeader
6 Inherits iPurchaseHeaderBase
7 <DevExpress.ExpressApp.DC.Aggregated()> _
8 <XafDisplayName("采购询价单明细项")> _
9 ReadOnly Property PurchaseQuoteDetail As IList(Of iPurchaseQuoteDetail)
10 End Interface
11 <DomainComponent()> _
12 <XafDisplayName("采购询价单明细项")> _
13 Public Interface iPurchaseQuoteDetail
14 Inherits iPurchaseDetailBase
15 <XafDisplayName("采购询价单")> _
16 Property PurchaseQuote As iPurchaseQuoteHeader
17 End Interface
细心的读者一定发现,我这里的两个DC都采用了继承自基础DC的方法,这样做,就可以实现在一个地方维护多个地方的代码,实现代码简洁,高效和高可维护性,这个是interface的特点,呵呵,切入正题,大家可能犯嘀咕了:这里的主详细表的一对多的关系是怎么定义的呢?这里的机关就在 <DevExpress.ExpressApp.DC.Aggregated()> _
这句上,这句就定义了当前集合property要用一对多的关系和详细表管了,这里的管理是自动关联到iPurchaseQuoteDetail中的PurchaseQuote这个property,我们也可以不定义它,系统会自动创建关联,但是为了后面用到这个字段,因此这里显性定义了它.
这句上,这句就定义了当前集合property要用一对多的关系和详细表管了,这里的管理是自动关联到iPurchaseQuoteDetail中的PurchaseQuote这个property,我们也可以不定义它,系统会自动创建关联,但是为了后面用到这个字段,因此这里显性定义了它.
下面我们把需要的实体DC注册在xaf应用模块中来运行一下,看看实际效果吧,
注册代码如下:
1 Public Overrides Sub Setup(ByVal application As XafApplication)
2 If (Not XafTypesInfo.IsInitialized) Then
3 XafTypesInfo.Instance.AddEntityToGenerate("货品资料", GetType(iGoodsInfo), GetType(BaseObject))
4 XafTypesInfo.Instance.AddEntityToGenerate("供应商资料", GetType(iSupplier), GetType(BaseObject))
5 XafTypesInfo.Instance.AddEntityToGenerate("采购询价单", GetType(iPurchaseQuoteHeader), GetType(BaseObject))
6 XafTypesInfo.Instance.AddEntityToGenerate("采购询价单明细项", GetType(iPurchaseQuoteDetail), GetType(BaseObject))
7 End If
8 MyBase.Setup(application)
9 End Sub
2 If (Not XafTypesInfo.IsInitialized) Then
3 XafTypesInfo.Instance.AddEntityToGenerate("货品资料", GetType(iGoodsInfo), GetType(BaseObject))
4 XafTypesInfo.Instance.AddEntityToGenerate("供应商资料", GetType(iSupplier), GetType(BaseObject))
5 XafTypesInfo.Instance.AddEntityToGenerate("采购询价单", GetType(iPurchaseQuoteHeader), GetType(BaseObject))
6 XafTypesInfo.Instance.AddEntityToGenerate("采购询价单明细项", GetType(iPurchaseQuoteDetail), GetType(BaseObject))
7 End If
8 MyBase.Setup(application)
9 End Sub
运行效果图如下:
怎么样,DC的开发效率是不是很震撼呢?呵呵