自完成了用dataset序列化的VO以后已经有几天,之所以迟迟没有贴出如何生成DO对像的模板是因为一直没有想好如何去实现这个DO。DO主要负责数据访问,但需要有哪些查询和操作方法我还是没有想太好,不过我还是把它贴出来和大家一起太讨一下。
对于DO的访问方法这里实现了:
1,查询数据
-- 获取所有数据
-- 按列查询
-- 其他的组合查询可以通过修改XML的Query节点来实现
2,新增数据。
-- 按列插入
-- 其他复杂的数据新增,需要修改XML的Insert节点来实现
3,修改数据。
4,删除数据。
-- 目前只要按照主键来删除数据。
要实现上面的操作,首先要按照 使用XML + XSLT进行实体类代码生成 (一) 得实现方法修改生成的XML格式。
1<?xmlversion="1.0"encoding="utf-16"?>
2<Schema>
3<TableNamevalue="Authors"/>
4<FIELDS>
5<FIELDName="PKId"Type="String"Identity="True"/>
6<FIELDName="Name"Type="String"/>
7</FIELDS>
8<PrimaryKeys>
9<FIELDName="PKId"Type="String"Identity="True"/>
10</PrimaryKeys>
11<Query>
12<CommandName="SelectAll"ReturnMode="Multiple"/>
13<CommandName="SelectByPKId"ReturnMode="Multiple">
14<FIELDName="PKId"Type="String"Identity="True"/>
15</Command>
16<CommandName="SelectByName"ReturnMode="Multiple">
17<FIELDName="Name"Type="String"/>
18</Command>
19</Query>
20<Insert>
21<CommandName="InsertAuthors">
22<FIELDName="PKId"Type="String"Identity="True"/>
23<FIELDName="Name"Type="String"/>
24</Command>
25</Insert>
26<Update>
27<CommandName="UpdateAuthors">
28<FIELDName="PKId"Type="String"Identity="True"/>
29<FIELDName="Name"Type="String"/>
30</Command>
31</Update>
32<Delete>
33<CommandName="DeleteAuthors">
34<FIELDName="PKId"Type="String"Identity="True"/>
35</Command>
36</Delete>
37</Schema>
然后,调用DO实现模板:
1<?xmlversion="1.0"encoding="UTF-8"?>
2<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3<xsl:outputmethod="xml"omit-xml-declaration="yes"indent="yes"/>
4<xsl:templatematch="/Schema">ImportsSystem.Data.SqlClient
5ImportsAPJ.Common.Data
6
7NamespaceModules.YourModule
8PublicClassdo<xsl:apply-templatesselect="//TableName"/>
9ImplementsIDisposable
10
11#Region"feildsconstants"
12<xsl:for-eachselect="FIELDS/FIELD">PublicConstFIELD_<xsl:value-ofselect="@Name"/>AsString="<xsl:value-ofselect="@Name"/>"
13</xsl:for-each>#EndRegion
14
15#Region"Commandconstants"
16<xsl:for-eachselect="Query/Command">PrivateConstCommand_<xsl:value-ofselect="@Name"/>AsString="<xsl:value-ofselect="@Name"/>"
17</xsl:for-each>
18<xsl:for-eachselect="Insert/Command">PrivateConstCommand_<xsl:value-ofselect="@Name"/>AsString="<xsl:value-ofselect="@Name"/>"
19</xsl:for-each>
20<xsl:for-eachselect="Update/Command">PrivateConstCommand_<xsl:value-ofselect="@Name"/>AsString="<xsl:value-ofselect="@Name"/>"
21</xsl:for-each>
22<xsl:for-eachselect="Delete/Command">PrivateConstCommand_<xsl:value-ofselect="@Name"/>AsString="<xsl:value-ofselect="@Name"/>"
23</xsl:for-each>#EndRegion
24
25#Region"Constructor"
26'----------------------------------------------------------------
27'SubNew:
28'Constructor.
29'----------------------------------------------------------------
30PublicSubNew()
31EndSub
32
33'----------------------------------------------------------------
34'SubDispose:
35'Disposeofthisobject'sresources.
36'----------------------------------------------------------------
37PublicSubDispose()ImplementsIDisposable.Dispose
38GC.SuppressFinalize(True)'asaservicetothosewhomightinheritfromus
39EndSub
40#EndRegion
41
42#Region"Read<xsl:apply-templatesselect="//TableName"/>"<xsl:for-eachselect="Query/Command">
43<xsl:choose>
44<xsl:whentest="@ReturnMode='Multiple'">
45PublicFunctionget<xsl:apply-templatesselect="//TableName"/>
46<xsl:choose>
47<xsl:whentest="count(*)>0">By</xsl:when>
48<xsl:otherwise>All</xsl:otherwise>
49</xsl:choose>
50<xsl:for-eachselect="FIELD">
51<xsl:value-ofselect="@Name"/>
52</xsl:for-each>(<xsl:for-eachselect="FIELD">
53<xsl:iftest="position()!=count(../*)">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/>,</xsl:if>
54<xsl:iftest="position()=count(../*)">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/></xsl:if>
55</xsl:for-each>)AsdsEntity<xsl:apply-templatesselect="//TableName"/>
56Dimds<xsl:apply-templatesselect="//TableName"/>AsNewdsEntity<xsl:apply-templatesselect="//TableName"/>
57
58'getthesqlstatement
59DimsqlAsSqlType
60
61sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_<xsl:value-ofselect="@Name"/>)
62<xsl:for-eachselect="FIELD">sql.setValue(FIELD_<xsl:value-ofselect="@Name"/>,p<xsl:value-ofselect="@Name"/>)
63</xsl:for-each>
64SQLDBObject.RunProcedureByRef(sql,sql.getMapping(),ds<xsl:apply-templatesselect="//TableName"/>,ds<xsl:apply-templatesselect="//TableName"/>.Table_Name)
65
66Returnds<xsl:apply-templatesselect="//TableName"/>
67EndFunction
68</xsl:when>
69<xsl:otherwise>
70PublicFunctionget<xsl:apply-templatesselect="//TableName"/>
71<xsl:choose>
72<xsl:whentest="count(*)>0">By</xsl:when>
73<xsl:otherwise>All</xsl:otherwise>
74</xsl:choose>
75<xsl:for-eachselect="FIELD">
76<xsl:value-ofselect="@Name"/>
77</xsl:for-each>(<xsl:for-eachselect="FIELD">
78<xsl:iftest="position()!=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/>,</xsl:if>
79<xsl:iftest="position()=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/></xsl:if>
80</xsl:for-each>)AsEntity<xsl:apply-templatesselect="//TableName"/>
81Dimi<xsl:apply-templatesselect="//TableName"/>AsNewEntity<xsl:apply-templatesselect="//TableName"/>
82
83'getthesqlstatement
84DimsqlAsSqlType
85
86sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_<xsl:value-ofselect="@Name"/>)
87<xsl:for-eachselect="FIELD">sql.setValue(FIELD_<xsl:value-ofselect="@Name"/>,p<xsl:value-ofselect="@Name"/>)
88</xsl:for-each>
89i<xsl:apply-templatesselect="//TableName"/>=SQLDBObject.RunProcedureReturnObject(sql,sql.getMapping(),GetType(Entity<xsl:apply-templatesselect="//TableName"/>))
90
91Returni<xsl:apply-templatesselect="//TableName"/>
92EndFunction
93</xsl:otherwise>
94</xsl:choose>
95</xsl:for-each>#EndRegion
96
97#Region"Insert<xsl:apply-templatesselect="//TableName"/>"<xsl:for-eachselect="Insert/Command">
98PublicSub<xsl:value-ofselect="@Name"/>(<xsl:for-eachselect="FIELD">
99<xsl:iftest="position()!=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/>,</xsl:if>
100<xsl:iftest="position()=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/></xsl:if>
101</xsl:for-each>)
102'getthesqlstatement
103DimsqlAsSqlType
104sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_<xsl:value-ofselect="@Name"/>)
105<xsl:for-eachselect="FIELD">sql.setValue(FIELD_<xsl:value-ofselect="@Name"/>,p<xsl:value-ofselect="@Name"/>)
106</xsl:for-each>
107SQLDBObject.RunProcedure(sql,sql.getMapping())
108EndSub
109</xsl:for-each>#EndRegion
110
111#Region"Update<xsl:apply-templatesselect="//TableName"/>"<xsl:for-eachselect="Update/Command">
112PublicSub<xsl:value-ofselect="@Name"/>(<xsl:for-eachselect="FIELD">
113<xsl:iftest="position()!=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/>,</xsl:if>
114<xsl:iftest="position()=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/></xsl:if>
115</xsl:for-each>)
116'getthesqlstatement
117DimsqlAsSqlType
118sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_<xsl:value-ofselect="@Name"/>)
119<xsl:for-eachselect="FIELD">sql.setValue(FIELD_<xsl:value-ofselect="@Name"/>,p<xsl:value-ofselect="@Name"/>)
120</xsl:for-each>
121SQLDBObject.RunProcedure(sql,sql.getMapping())
122EndSub
123</xsl:for-each>#EndRegion
124
125#Region"Delete<xsl:apply-templatesselect="//TableName"/>"<xsl:for-eachselect="Delete/Command">
126PublicSub<xsl:value-ofselect="@Name"/>(<xsl:for-eachselect="FIELD">
127<xsl:iftest="position()!=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/>,</xsl:if>
128<xsl:iftest="position()=last()">Byvalp<xsl:value-ofselect="@Name"/>as<xsl:value-ofselect="@Type"/></xsl:if>
129</xsl:for-each>)
130'getthesqlstatement
131DimsqlAsSqlType
132sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_<xsl:value-ofselect="@Name"/>)
133<xsl:for-eachselect="FIELD">sql.setValue(FIELD_<xsl:value-ofselect="@Name"/>,p<xsl:value-ofselect="@Name"/>)
134</xsl:for-each>
135SQLDBObject.RunProcedure(sql,sql.getMapping())
136EndSub
137</xsl:for-each>#EndRegion
138EndClass
139EndNamespace</xsl:template>
140<xsl:templatematch="//TableName">
141<xsl:value-ofselect="@value"/>
142</xsl:template>
143</xsl:stylesheet>
144
运行效果:
运行结果:
1ImportsSystem.Data.SqlClient
2ImportsAPJ.Common.Data
3
4NamespaceModulesNamespaceModules.YourModule
5PublicClassdoAuthorsClassdoAuthors
6ImplementsIDisposable
7
8feildsconstants#Region"feildsconstants"
9PublicConstFIELD_PKIdAsString="PKId"
10PublicConstFIELD_NameAsString="Name"
11#EndRegion
12
13Commandconstants#Region"Commandconstants"
14PrivateConstCommand_SelectAllAsString="SelectAll"
15PrivateConstCommand_SelectByPKIdAsString="SelectByPKId"
16PrivateConstCommand_SelectByNameAsString="SelectByName"
17PrivateConstCommand_InsertAuthorsAsString="InsertAuthors"
18PrivateConstCommand_UpdateAuthorsAsString="UpdateAuthors"
19PrivateConstCommand_DeleteAuthorsAsString="DeleteAuthors"
20#EndRegion
21
22Constructor#Region"Constructor"
23'----------------------------------------------------------------
24'SubNew:
25'Constructor.
26'----------------------------------------------------------------
27PublicSubNew()SubNew()
28EndSub
29
30'----------------------------------------------------------------
31'SubDispose:
32'Disposeofthisobject'sresources.
33'----------------------------------------------------------------
34PublicSubDispose()SubDispose()ImplementsIDisposable.Dispose
35GC.SuppressFinalize(True)'asaservicetothosewhomightinheritfromus
36EndSub
37#EndRegion
38
39ReadAuthors#Region"ReadAuthors"
40PublicFunctiongetAuthorsAll()FunctiongetAuthorsAll()AsdsEntityAuthors
41DimdsAuthorsAsNewdsEntityAuthors
42
43'getthesqlstatement
44'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
45'DimsqlAsSqlType
46'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_SelectAll)
47
48'Youcanuseyourownsqlhelperhere.
49'SQLDBObject.RunProcedureByRef(sql,sql.getMapping(),dsAuthors,dsAuthors.Table_Name)
50
51ReturndsAuthors
52EndFunction
53
54PublicFunctiongetAuthorsByPKId()FunctiongetAuthorsByPKId(ByvalpPKIdasString)AsdsEntityAuthors
55DimdsAuthorsAsNewdsEntityAuthors
56
57'getthesqlstatement
58'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
59'DimsqlAsSqlType
60'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_SelectByPKId)
61sql.setValue(FIELD_PKId,pPKId)
62
63'Youcanuseyourownsqlhelperhere.
64'SQLDBObject.RunProcedureByRef(sql,sql.getMapping(),dsAuthors,dsAuthors.Table_Name)
65
66ReturndsAuthors
67EndFunction
68
69PublicFunctiongetAuthorsByName()FunctiongetAuthorsByName(ByvalpNameasString)AsdsEntityAuthors
70DimdsAuthorsAsNewdsEntityAuthors
71
72'getthesqlstatement
73'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
74'DimsqlAsSqlType
75'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_SelectByName)
76sql.setValue(FIELD_Name,pName)
77
78'Youcanuseyourownsqlhelperhere.
79'SQLDBObject.RunProcedureByRef(sql,sql.getMapping(),dsAuthors,dsAuthors.Table_Name)
80
81ReturndsAuthors
82EndFunction
83#EndRegion
84
85InsertAuthors#Region"InsertAuthors"
86PublicSubInsertAuthors()SubInsertAuthors(ByvalpPKIdasString,ByvalpNameasString)
87'getthesqlstatement
88'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
89'DimsqlAsSqlType
90'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_InsertAuthors)
91'Passtheparameterhere
92'sql.setValue(FIELD_PKId,pPKId)
93'sql.setValue(FIELD_Name,pName)
94
95'Youcanuseyourownsqlhelperhere.
96'SQLDBObject.RunProcedure(sql,sql.getMapping())
97EndSub
98#EndRegion
99
100UpdateAuthors#Region"UpdateAuthors"
101PublicSubUpdateAuthors()SubUpdateAuthors(ByvalpPKIdasString,ByvalpNameasString)
102'getthesqlstatement
103'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
104'DimsqlAsSqlType
105'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_UpdateAuthors)
106'Passtheparameterhere
107'sql.setValue(FIELD_PKId,pPKId)
108'sql.setValue(FIELD_Name,pName)
109
110'Youcanuseyourownsqlhelperhere.
111'SQLDBObject.RunProcedure(sql,sql.getMapping())
112EndSub
113#EndRegion
114
115DeleteAuthors#Region"DeleteAuthors"
116PublicSubDeleteAuthors()SubDeleteAuthors(ByvalpPKIdasString)
117'getthesqlstatement
118'Yourcanreplacethefollowingfunctionwithyourownmethodtogetthesqlstatmentorthestoredprocedure
119'DimsqlAsSqlType
120'sql=DataAccessConfiguration.GetSQL(Me.GetType.Name,Command_DeleteAuthors)
121'Passtheparameterhere
122'sql.setValue(FIELD_PKId,pPKId)
123
124'Youcanuseyourownsqlhelperhere.
125'SQLDBObject.RunProcedure(sql,sql.getMapping())
126EndSub
127#EndRegion
128EndClass
129EndNamespace
相关链接:使用XML + XSLT进行实体类代码生成 (一)
使用XML + XLST进行实体类代码生成 (二)
使用XML + XLST进行实体类代码生成 (三)
使用XML + XLST进行实体类代码生成 (四)
优质内容筛选与推荐>>
1、win10下安装python2、[笔记]Makefile wildcard3、SQL Server分区表基础理论篇4、python子类调用父类的方法5、UVA 382 - Perfection