Lily.Core组件是在Vs2003+Sqlserver2000下的开发和进行测试的。
Lily.Core组件是一个轻量级的和数据库打交道的并不是一个真正意义的是ORM组件。它只不是一多对或多对多的关系,必须靠手动维护各实体之间的关系。
Lily.Core中的实体对应于数据库的一个表,一个视图或一段查询语句。
Lily.Core对于事务的控制很弱,使用了Ado.Net中事务对象的传递来完成对事务的控制。
Lily.Core的设计只是针对于SqlServer2000数据库,不对Access,Orcal,MySql等其它的数据库提供支持。
好下面,我们看一个简单的例子:
数据库中有表结构如下:
IMA_物品
3 物品编号 int 4 00 物品名称 nvarchar 50 0
0 物品型号 nvarchar 50 0
0 单位 nvarchar 50 0
0 生产厂家 nvarchar 50 0
0 备注 nvarchar 50 0
注:物品编号是自动增长字段,如果需要对实体对象进行添加,删除,更新必须要定义一个自动增长字段。
![](https://www.cnblogs.com/images/cnblogs_com/zqonline/1.jpg)
一个数据表对应了一个描述数据表的实体类,对应一实体的容器,对应一操作实体的对象。
假设置我们在IMA_物品表中:物品名称+物品型号+生产厂家不能存在有相同的记录,生成的实体对象如下:
1
Imports Lily
2
Imports Lily.Core
3
Imports System.ComponentModel
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class IMA_物品Class IMA_物品
5
Inherits EntityBase
6![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
类实例化#Region "类实例化"
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
8
MyBase.new()
9
End Sub
10
#End Region
11![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
实体字段描述#Region "实体字段描述"
12
Private f_物品编号 As IEntityField
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
<Browsable(False)> _
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _物品编号()Property _物品编号() As IEntityField
16
Get
17
If f_物品编号 Is Nothing Then
18
f_物品编号 = CoreFactory.GetEntityField(Me, "物品编号", "物品编号", True, False, DbType.Int32, 4)
19
End If
20
Return f_物品编号
21
End Get
22
End Property
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
Private f_物品名称 As IEntityField
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
<Browsable(False)> _
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _物品名称()Property _物品名称() As IEntityField
28
Get
29
If f_物品名称 Is Nothing Then
30
f_物品名称 = CoreFactory.GetEntityField(Me, "物品名称", "物品名称", False, False, DbType.String, 50, True)
31
End If
32
Return f_物品名称
33
End Get
34
End Property
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
Private f_物品型号 As IEntityField
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
<Browsable(False)> _
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _物品型号()Property _物品型号() As IEntityField
40
Get
41
If f_物品型号 Is Nothing Then
42
f_物品型号 = CoreFactory.GetEntityField(Me, "物品型号", "物品型号", False, False, DbType.String, 50, True)
43
End If
44
Return f_物品型号
45
End Get
46
End Property
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
Private f_单位 As IEntityField
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
<Browsable(False)> _
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _单位()Property _单位() As IEntityField
52
Get
53
If f_单位 Is Nothing Then
54
f_单位 = CoreFactory.GetEntityField(Me, "单位", "单位", False, False, DbType.String, 50)
55
End If
56
Return f_单位
57
End Get
58
End Property
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
Private f_生产厂家 As IEntityField
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
<Browsable(False)> _
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _生产厂家()Property _生产厂家() As IEntityField
64
Get
65
If f_生产厂家 Is Nothing Then
66
f_生产厂家 = CoreFactory.GetEntityField(Me, "生产厂家", "生产厂家", False, False, DbType.String, 50, True)
67
End If
68
Return f_生产厂家
69
End Get
70
End Property
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
Private f_备注 As IEntityField
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
<Browsable(False)> _
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable ReadOnly Property _备注()Property _备注() As IEntityField
76
Get
77
If f_备注 Is Nothing Then
78
f_备注 = CoreFactory.GetEntityField(Me, "备注", "备注", False, False, DbType.String, 50)
79
End If
80
Return f_备注
81
End Get
82
End Property
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
#End Region
86![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
实体属性#Region "实体属性"
87
Private md_物品编号 As Integer
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
<Browsable(True)> _
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 物品编号()Property 物品编号() As Integer
91
Get
92
Return md_物品编号
93
End Get
94
Set(ByVal Value As Integer)
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
md_物品编号 = Value
97
Me.OnPropertyChanged("物品编号")
98
End Set
99
End Property
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
Private md_物品名称 As String = String.Empty
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
<Browsable(True)> _
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 物品名称()Property 物品名称() As String
105
Get
106
Return md_物品名称
107
End Get
108
Set(ByVal Value As String)
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
md_物品名称 = Value
111
Me.OnPropertyChanged("物品名称")
112
End Set
113
End Property
114![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
Private md_物品型号 As String = String.Empty
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
<Browsable(True)> _
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 物品型号()Property 物品型号() As String
119
Get
120
Return md_物品型号
121
End Get
122
Set(ByVal Value As String)
123![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
md_物品型号 = Value
125
Me.OnPropertyChanged("物品型号")
126
End Set
127
End Property
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
Private md_单位 As String = String.Empty
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
<Browsable(True)> _
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 单位()Property 单位() As String
133
Get
134
Return md_单位
135
End Get
136
Set(ByVal Value As String)
137![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
md_单位 = Value
139
Me.OnPropertyChanged("单位")
140
End Set
141
End Property
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
Private md_生产厂家 As String = String.Empty
144![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
<Browsable(True)> _
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 生产厂家()Property 生产厂家() As String
147
Get
148
Return md_生产厂家
149
End Get
150
Set(ByVal Value As String)
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
md_生产厂家 = Value
153
Me.OnPropertyChanged("生产厂家")
154
End Set
155
End Property
156![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
Private md_备注 As String = String.Empty
158![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
159
<Browsable(True)> _
160![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overridable Property 备注()Property 备注() As String
161
Get
162
Return md_备注
163
End Get
164
Set(ByVal Value As String)
165![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
md_备注 = Value
167
Me.OnPropertyChanged("备注")
168
End Set
169
End Property
170![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
171![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
#End Region
173![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
设置实体的属性#Region "设置实体的属性"
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overrides Sub SetValue()Sub SetValue(ByVal attributename As String, ByVal value As Object)
176
Select Case attributename
177![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
178
Case "物品编号"
179
物品编号 = value
180
Case "物品名称"
181
物品名称 = value
182
Case "物品型号"
183
物品型号 = value
184
Case "单位"
185
单位 = value
186
Case "生产厂家"
187
生产厂家 = value
188
Case "备注"
189
备注 = value
190
Case Else
191
MyBase.SetValue(attributename, value)
192
End Select
193
End Sub
194
#End Region
195![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
获取实体的属性#Region "获取实体的属性"
196![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overrides Function GetValue()Function GetValue(ByVal attributename As String) As Object
198
Select Case attributename
199![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
200
Case "物品编号"
201
Return 物品编号
202
Case "物品名称"
203
Return 物品名称
204
Case "物品型号"
205
Return 物品型号
206
Case "单位"
207
Return 单位
208
Case "生产厂家"
209
Return 生产厂家
210
Case "备注"
211
Return 备注
212
Case Else
213
Return MyBase.GetValue(attributename)
214
End Select
215
End Function
216
#End Region
217![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
实体其它属性#Region "实体其它属性"
218![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
219
<Browsable(False)> _
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public ReadOnly Property Guid()Property Guid() As String
221
Get
222
Return "a1ebd1182d4f540e2875efbd38e5b008f"
223
End Get
224
End Property
225![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
226
<Browsable(False)> _
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overrides ReadOnly Property TableName()Property TableName() As String
228
Get
229
Return "IMA_物品"
230
End Get
231
End Property
232![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
233
<Browsable(False)> _
234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overrides ReadOnly Property AutoIncrement()Property AutoIncrement() As IEntityField
235
Get
236
Return _物品编号
237
End Get
238
End Property
239
#End Region
240
End Class
241![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
实体的容器:
1
Imports lily
2
Imports lily.core
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class IMA_物品_ContainerClass IMA_物品_Container
4
Inherits EntityContainer
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
6
MyBase.New()
7
End Sub
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New(ByVal operate As IMA_物品_Operate)
9
MyBase.New(operate)
10
End Sub
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Function Add()Function Add(ByVal entity As IMA_物品) As Integer
12
Return MyBase.Add(entity)
13
End Function
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Default Public Shadows Property Item()Property Item(ByVal index As Integer) As IMA_物品
15
Get
16
Return MyBase.Item(index)
17
End Get
18
Set(ByVal Value As IMA_物品)
19
MyBase.Item(index) = Value
20
End Set
21
End Property
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Function IndexOf()Function IndexOf(ByVal value As IMA_物品) As Integer
23
Return MyBase.IndexOf(value)
24
End Function
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Sub Insert()Sub Insert(ByVal index As Integer, ByVal value As IMA_物品)
26
MyBase.Insert(index, value)
27
End Sub
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Sub Remove()Sub Remove(ByVal value As IMA_物品)
29
MyBase.Remove(value)
30
End Sub
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Sub AcceptChanges()Sub AcceptChanges(ByVal entityhandler As IMA_物品_Operate)
32
MyBase.AcceptChanges(EntityHandler)
33
End Sub
34
End Class
实体的操作类
1
Imports lily
2
Imports lily.core
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class IMA_物品_OperateClass IMA_物品_Operate
4
Inherits EntityHandler
5![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公共方法#Region "公共方法"
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Create()Function Create(ByVal entity As IMA_物品) As Integer
7
Return Me.InsertObject(entity)
8
End Function
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Delete()Function Delete(ByVal entity As IMA_物品) As Integer
10
Return Me.DeleteObject(entity)
11
End Function
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Update()Function Update(ByVal entity As IMA_物品) As Integer
13
Return Me.UpdateObject(entity)
14
End Function
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
#End Region
17![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
重写基类方法#Region "重写基类方法"
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overrides Function InsertObject()Function InsertObject(ByVal entity As Core.IEntityBase) As Integer
19
Dim en As IMA_物品 = entity
20
Return MyBase.InsertObject(en)
21
End Function
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Function DeleteObject()Function DeleteObject(ByVal entity As Core.IEntityBase) As Integer
24
Dim en As IMA_物品 = entity
25
Return MyBase.DeleteObject(en)
26
End Function
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Function UpdateObject()Function UpdateObject(ByVal entity As Core.IEntityBase) As Integer
29
Dim en As IMA_物品 = entity
30
Return MyBase.UpdateObject(en)
31
End Function
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Function GetEntityContainer()Function GetEntityContainer(ByVal command As OQL.ICommand) As IMA_物品_Container
33
Return MyBase.GetEntityContainer(command, GetType(IMA_物品), GetType(IMA_物品_Container))
34
End Function
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shadows Function GetEntity()Function GetEntity(ByVal expression As Lily.OQL.IExpression) As IMA_物品
36
Return MyBase.GetEntity(expression, New IMA_物品)
37
End Function
38
#End Region
39
End Class
好,我们现在就完成了对于一个数据库的映射操作。当然这些代码是通过一个简单的代码生成器完成的。
第一步:获取系统中所有的IMA_物品表的记录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Dim o As New IMA_物品_Operate
Dim en As New IMA_物品
Dim ec As IMA_物品_Container = o.GetEntityContainer(Lily.Core.SQL.Select().From(en))
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
'有时我们并不想在表格里显示所实体对象的所有列,则需要加下以下这段代码
'产生的结果是只有“物品名称”,“物品型号”,“单位”,“生产厂家”才在DataGrid中显示
ec.BindList.Add(en._物品名称)
ec.BindList.Add(en._物品型号)
ec.BindList.Add(en._单位)
ec.BindList.Add(en._生产厂家)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Me.DataGrid1.DataSource = ec
通过上面的语句,我们就可以IMA_物品所有的对象绑定到了DataGrid控件,可以进行编辑,删除,排序,就像把DataTable对象绑室DataGrid一样。当然没有DataTable强大。
绑定到了DataGrid我们就可进行日常的编辑操作,当编辑完成后我们就需要进行提交操作,下面的代码演示了如何把实体对象保存到数据库
If Not Me.DataGrid1.DataSource Is Nothing AndAlso TypeOf Me.DataGrid1.DataSource Is IMA_物品_Container Then
Dim ec As IMA_物品_Container = CType(Me.DataGrid1.DataSource, IMA_物品_Container)
Dim o As New IMA_物品_Operate
ec.AcceptChanges(o)
End If
通过调用容顺的AcceptChanges方法自动完成了对实体对象的更新,删除,添加。
(这也要求每个数据表必须要有自动增长字段的原因。由于是离线操作,如果需要处理更新,删除影响的行数等于0或强制更新,需要手动添加代码进行控制。)
由于我们并没有在数据表中对 物品名称,物品型号,生产厂家设置联合主键,但又要求不允许存在有重复的记录所以在生成实体的字段后需要设置期实体字段的属性Uniqe=True.这样才进行更新和添加时组件会自动检查是否存在有重复的记录,如果存在有重复的记录将抛出异常。
下面是介绍如何设置实体的联合主键.最后一个参数设为True就代表当前实体存在有联合主键。
<Browsable(False)> _
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Overridable ReadOnly Property _物品名称()Property _物品名称() As IEntityField
Get
If f_物品名称 Is Nothing Then
f_物品名称 = CoreFactory.GetEntityField(Me, "物品名称", "物品名称", False, False, DbType.String, 50, True)
End If
Return f_物品名称
End Get
End Property
总结:此小节主要是介绍了如何把一个数据表转换为一个实体对象,并简单的介绍了如何获取多个实体并绑定和如何更新到数据库。下一节,将介绍如何添加,更新,删除,查询实体对象。