@@ -4,10 +4,6 @@ namespace FluentCommand.Generators;
44
55public static class DataReaderFactoryWriter
66{
7- #if DEBUG
8- private static int _counter = 0 ;
9- #endif
10-
117 public static string Generate ( EntityClass entityClass )
128 {
139 if ( entityClass == null )
@@ -22,11 +18,6 @@ public static string Generate(EntityClass entityClass)
2218 . AppendLine ( "using global::FluentCommand.Extensions;" )
2319 . AppendLine ( ) ;
2420
25- #if DEBUG
26- // used to track re-writes for performance
27- codeBuilder . AppendLine ( $ "// Counter: { Interlocked . Increment ( ref _counter ) } ") ;
28- #endif
29-
3021 codeBuilder
3122 . Append ( "namespace " )
3223 . AppendLine ( entityClass . EntityNamespace )
@@ -50,7 +41,7 @@ public static string Generate(EntityClass entityClass)
5041 . AppendLine ( "[global::System.Diagnostics.DebuggerStepThroughAttribute]" )
5142 . Append ( "public static partial class " )
5243 . Append ( entityClass . EntityName )
53- . AppendLine ( "DataReaderFactoryExtensions " )
44+ . AppendLine ( "DataReaderExtensions " )
5445 . AppendLine ( "{" )
5546 . IncrementIndent ( ) ;
5647
@@ -85,8 +76,9 @@ private static void WriteQuerySingleEntityTask(IndentedStringBuilder codeBuilder
8576 . AppendLine ( "\" /> object." )
8677 . AppendLine ( "/// </summary>" )
8778 . AppendLine ( "/// <param name=\" dataQuery\" >The <see cref=\" T:FluentCommand.IDataQueryAsync\" /> for this extension method.</param>" )
79+ . AppendLine ( "/// <param name=\" cancellationToken\" >The cancellation instruction.</param>" )
8880 . AppendLine ( "/// <returns>" )
89- . Append ( "/// A instance of <see cref=\" " )
81+ . Append ( "/// A instance of <see cref=\" T: " )
9082 . Append ( entity . EntityNamespace )
9183 . Append ( "." )
9284 . Append ( entity . EntityName )
@@ -97,17 +89,35 @@ private static void WriteQuerySingleEntityTask(IndentedStringBuilder codeBuilder
9789 . Append ( "." )
9890 . Append ( entity . EntityName )
9991 . Append ( "> QuerySingle" )
100- . Append ( entity . EntityName )
101- . AppendLine ( "Async(this global::FluentCommand.IDataQueryAsync dataQuery)" )
92+ . AppendLine ( "Async<TEntity>(" )
10293 . IncrementIndent ( )
103- . Append ( "=> global::FluentCommand.Internal.DataSequentialReader.QuerySingleAsync<" )
94+ . AppendLine ( "this global::FluentCommand.IDataQueryAsync dataQuery," )
95+ . AppendLine ( "global::System.Threading.CancellationToken cancellationToken = default)" )
96+ . Append ( "where TEntity : " )
10497 . Append ( entity . EntityNamespace )
10598 . Append ( "." )
99+ . AppendLine ( entity . EntityName )
100+ . DecrementIndent ( )
101+ . AppendLine ( "{" )
102+ . IncrementIndent ( )
103+ . Append ( "return dataQuery.QuerySingleAsync<" )
104+ . Append ( entity . EntityNamespace )
105+ . Append ( "." )
106+ . Append ( entity . EntityName )
107+ . AppendLine ( ">(" )
108+ . IncrementIndent ( )
109+ . Append ( "factory: " )
106110 . Append ( entity . EntityName )
107- . Append ( ">(dataQuery, " )
111+ . Append ( "DataReaderExtensions. " )
108112 . Append ( entity . EntityName )
109- . AppendLine ( "Factory);" )
113+ . AppendLine ( "Factory," )
114+ . AppendLine ( "commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |" )
115+ . AppendLine ( " global::System.Data.CommandBehavior.SingleResult |" )
116+ . AppendLine ( " global::System.Data.CommandBehavior.SingleRow," )
117+ . AppendLine ( "cancellationToken: cancellationToken);" )
118+ . DecrementIndent ( )
110119 . DecrementIndent ( )
120+ . AppendLine ( "}" )
111121 . AppendLine ( ) ;
112122 }
113123
@@ -123,6 +133,7 @@ private static void WriteQueryEntityTask(IndentedStringBuilder codeBuilder, Enti
123133 . AppendLine ( "\" /> objects." )
124134 . AppendLine ( "/// </summary>" )
125135 . AppendLine ( "/// <param name=\" dataQuery\" >The <see cref=\" T:FluentCommand.IDataQueryAsync\" /> for this extension method.</param>" )
136+ . AppendLine ( "/// <param name=\" cancellationToken\" >The cancellation instruction.</param>" )
126137 . AppendLine ( "/// <returns>" )
127138 . Append ( "/// An <see cref=\" T:System.Collections.Generic.IEnumerable`1\" /> of <see cref=\" T:" )
128139 . Append ( entity . EntityNamespace )
@@ -134,18 +145,34 @@ private static void WriteQueryEntityTask(IndentedStringBuilder codeBuilder, Enti
134145 . Append ( entity . EntityNamespace )
135146 . Append ( "." )
136147 . Append ( entity . EntityName )
137- . Append ( ">> Query" )
138- . Append ( entity . EntityName )
139- . AppendLine ( "Async(this global::FluentCommand.IDataQueryAsync dataQuery)" )
148+ . AppendLine ( ">> QueryAsync<TEntity>(" )
140149 . IncrementIndent ( )
141- . Append ( "=> global::FluentCommand.Internal.DataSequentialReader.QueryAsync<" )
150+ . AppendLine ( "this global::FluentCommand.IDataQueryAsync dataQuery," )
151+ . AppendLine ( "global::System.Threading.CancellationToken cancellationToken = default)" )
152+ . Append ( "where TEntity : " )
142153 . Append ( entity . EntityNamespace )
143154 . Append ( "." )
155+ . AppendLine ( entity . EntityName )
156+ . DecrementIndent ( )
157+ . AppendLine ( "{" )
158+ . IncrementIndent ( )
159+ . Append ( "return dataQuery.QueryAsync<" )
160+ . Append ( entity . EntityNamespace )
161+ . Append ( "." )
162+ . Append ( entity . EntityName )
163+ . AppendLine ( ">(" )
164+ . IncrementIndent ( )
165+ . Append ( "factory: " )
144166 . Append ( entity . EntityName )
145- . Append ( ">(dataQuery, " )
167+ . Append ( "DataReaderExtensions. " )
146168 . Append ( entity . EntityName )
147- . AppendLine ( "Factory);" )
169+ . AppendLine ( "Factory," )
170+ . AppendLine ( "commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |" )
171+ . AppendLine ( " global::System.Data.CommandBehavior.SingleResult," )
172+ . AppendLine ( "cancellationToken: cancellationToken);" )
148173 . DecrementIndent ( )
174+ . DecrementIndent ( )
175+ . AppendLine ( "}" )
149176 . AppendLine ( ) ;
150177 }
151178
@@ -162,7 +189,7 @@ private static void WriteQuerySingleEntity(IndentedStringBuilder codeBuilder, En
162189 . AppendLine ( "/// </summary>" )
163190 . AppendLine ( "/// <param name=\" dataQuery\" >The <see cref=\" T:FluentCommand.IDataQuery\" /> for this extension method.</param>" )
164191 . AppendLine ( "/// <returns>" )
165- . Append ( "/// A instance of <see cref=\" " )
192+ . Append ( "/// A instance of <see cref=\" T: " )
166193 . Append ( entity . EntityNamespace )
167194 . Append ( "." )
168195 . Append ( entity . EntityName )
@@ -172,18 +199,33 @@ private static void WriteQuerySingleEntity(IndentedStringBuilder codeBuilder, En
172199 . Append ( entity . EntityNamespace )
173200 . Append ( "." )
174201 . Append ( entity . EntityName )
175- . Append ( " QuerySingle" )
176- . Append ( entity . EntityName )
177- . AppendLine ( "(this global::FluentCommand.IDataQuery dataQuery)" )
202+ . AppendLine ( " QuerySingle<TEntity>(" )
178203 . IncrementIndent ( )
179- . Append ( "=> global::FluentCommand.Internal.DataSequentialReader.QuerySingle<" )
204+ . AppendLine ( "this global::FluentCommand.IDataQuery dataQuery)" )
205+ . Append ( "where TEntity : " )
180206 . Append ( entity . EntityNamespace )
181207 . Append ( "." )
208+ . AppendLine ( entity . EntityName )
209+ . DecrementIndent ( )
210+ . AppendLine ( "{" )
211+ . IncrementIndent ( )
212+ . Append ( "return dataQuery.QuerySingle<" )
213+ . Append ( entity . EntityNamespace )
214+ . Append ( "." )
215+ . Append ( entity . EntityName )
216+ . AppendLine ( ">(" )
217+ . IncrementIndent ( )
218+ . Append ( "factory: " )
182219 . Append ( entity . EntityName )
183- . Append ( ">(dataQuery, " )
220+ . Append ( "DataReaderExtensions. " )
184221 . Append ( entity . EntityName )
185- . AppendLine ( "Factory);" )
222+ . AppendLine ( "Factory," )
223+ . AppendLine ( "commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |" )
224+ . AppendLine ( " global::System.Data.CommandBehavior.SingleResult |" )
225+ . AppendLine ( " global::System.Data.CommandBehavior.SingleRow);" )
186226 . DecrementIndent ( )
227+ . DecrementIndent ( )
228+ . AppendLine ( "}" )
187229 . AppendLine ( ) ;
188230 }
189231
@@ -210,18 +252,32 @@ private static void WriteQueryEntity(IndentedStringBuilder codeBuilder, EntityCl
210252 . Append ( entity . EntityNamespace )
211253 . Append ( "." )
212254 . Append ( entity . EntityName )
213- . Append ( "> Query" )
214- . Append ( entity . EntityName )
215- . AppendLine ( "(this global::FluentCommand.IDataQuery dataQuery)" )
255+ . AppendLine ( "> Query<TEntity>(" )
216256 . IncrementIndent ( )
217- . Append ( "=> global::FluentCommand.Internal.DataSequentialReader.Query<" )
257+ . AppendLine ( "this global::FluentCommand.IDataQuery dataQuery)" )
258+ . Append ( "where TEntity : " )
259+ . Append ( entity . EntityNamespace )
260+ . Append ( "." )
261+ . AppendLine ( entity . EntityName )
262+ . DecrementIndent ( )
263+ . AppendLine ( "{" )
264+ . IncrementIndent ( )
265+ . Append ( "return dataQuery.Query<" )
218266 . Append ( entity . EntityNamespace )
219267 . Append ( "." )
220268 . Append ( entity . EntityName )
221- . Append ( ">(dataQuery, " )
269+ . AppendLine ( ">(" )
270+ . IncrementIndent ( )
271+ . Append ( "factory: " )
222272 . Append ( entity . EntityName )
223- . AppendLine ( "Factory);" )
273+ . Append ( "DataReaderExtensions." )
274+ . Append ( entity . EntityName )
275+ . AppendLine ( "Factory," )
276+ . AppendLine ( "commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |" )
277+ . AppendLine ( " global::System.Data.CommandBehavior.SingleResult);" )
278+ . DecrementIndent ( )
224279 . DecrementIndent ( )
280+ . AppendLine ( "}" )
225281 . AppendLine ( ) ;
226282 }
227283
@@ -235,7 +291,7 @@ private static void WriteEntityFactory(IndentedStringBuilder codeBuilder, Entity
235291 . Append ( entity . EntityName )
236292 . AppendLine ( "\" /> objects from the current row in the specified <paramref name=\" dataRecord\" />." )
237293 . AppendLine ( "/// </summary>" )
238- . AppendLine ( "/// <param name=\" dataRecord\" >The open <see cref=\" T:System.Data.IDataRecord \" /> to get the object from.</param>" )
294+ . AppendLine ( "/// <param name=\" dataRecord\" >The open <see cref=\" T:System.Data.IDataReader \" /> to get the object from.</param>" )
239295 . AppendLine ( "/// <returns>" )
240296 . Append ( "/// A instance of <see cref=\" " )
241297 . Append ( entity . EntityNamespace )
@@ -249,7 +305,7 @@ private static void WriteEntityFactory(IndentedStringBuilder codeBuilder, Entity
249305 . Append ( entity . EntityName )
250306 . Append ( " " )
251307 . Append ( entity . EntityName )
252- . AppendLine ( "Factory(this global::System.Data.IDataRecord dataRecord)" )
308+ . AppendLine ( "Factory(this global::System.Data.IDataReader dataRecord)" )
253309 . AppendLine ( "{" )
254310 . IncrementIndent ( )
255311 . AppendLine ( "if (dataRecord == null)" )
0 commit comments