@@ -79,8 +79,9 @@ public override Topic Load(string? topicKey = null, bool isRecursive = true) {
79
79
/*------------------------------------------------------------------------------------------------------------------------
80
80
| Establish database connection
81
81
\-----------------------------------------------------------------------------------------------------------------------*/
82
- var connection = new SqlConnection ( _connectionString ) ;
83
- var command = new SqlCommand ( "GetTopicID" , connection ) ;
82
+ using var connection = new SqlConnection ( _connectionString ) ;
83
+ using var command = new SqlCommand ( "GetTopicID" , connection ) ;
84
+
84
85
var topicId = - 1 ;
85
86
86
87
command . CommandType = CommandType . StoredProcedure ;
@@ -110,14 +111,6 @@ public override Topic Load(string? topicKey = null, bool isRecursive = true) {
110
111
throw new TopicRepositoryException ( $ "Topic(s) failed to load: '{ exception . Message } '", exception ) ;
111
112
}
112
113
113
- /*------------------------------------------------------------------------------------------------------------------------
114
- | Close connection
115
- \-----------------------------------------------------------------------------------------------------------------------*/
116
- finally {
117
- command ? . Dispose ( ) ;
118
- connection . Close ( ) ;
119
- }
120
-
121
114
/*------------------------------------------------------------------------------------------------------------------------
122
115
| Validate results
123
116
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -139,12 +132,12 @@ public override Topic Load(int topicId, bool isRecursive = true) {
139
132
| Establish database connection
140
133
\-----------------------------------------------------------------------------------------------------------------------*/
141
134
var topic = ( Topic ? ) null ;
142
- var connection = new SqlConnection ( _connectionString ) ;
143
- var command = new SqlCommand ( "GetTopics" , connection ) {
135
+
136
+ using var connection = new SqlConnection ( _connectionString ) ;
137
+ using var command = new SqlCommand ( "GetTopics" , connection ) {
144
138
CommandType = CommandType . StoredProcedure ,
145
139
CommandTimeout = 120
146
140
} ;
147
- var reader = ( SqlDataReader ? ) null ;
148
141
149
142
/*------------------------------------------------------------------------------------------------------------------------
150
143
| Establish query parameters
@@ -157,7 +150,7 @@ public override Topic Load(int topicId, bool isRecursive = true) {
157
150
\-----------------------------------------------------------------------------------------------------------------------*/
158
151
try {
159
152
connection . Open ( ) ;
160
- reader = command . ExecuteReader ( ) ;
153
+ using var reader = command . ExecuteReader ( ) ;
161
154
topic = reader . LoadTopicGraph ( ) ;
162
155
}
163
156
@@ -168,15 +161,6 @@ public override Topic Load(int topicId, bool isRecursive = true) {
168
161
throw new TopicRepositoryException ( $ "Topics failed to load: '{ exception . Message } '", exception ) ;
169
162
}
170
163
171
- /*------------------------------------------------------------------------------------------------------------------------
172
- | Close connection
173
- \-----------------------------------------------------------------------------------------------------------------------*/
174
- finally {
175
- reader ? . Dispose ( ) ;
176
- command ? . Dispose ( ) ;
177
- connection . Close ( ) ;
178
- }
179
-
180
164
/*------------------------------------------------------------------------------------------------------------------------
181
165
| Validate results
182
166
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -212,12 +196,12 @@ public override Topic Load(int topicId, DateTime version) {
212
196
| Establish database connection
213
197
\-----------------------------------------------------------------------------------------------------------------------*/
214
198
var topic = ( Topic ? ) null ;
215
- var connection = new SqlConnection ( _connectionString ) ;
216
- var command = new SqlCommand ( "GetTopicVersion" , connection ) {
199
+
200
+ using var connection = new SqlConnection ( _connectionString ) ;
201
+ using var command = new SqlCommand ( "GetTopicVersion" , connection ) {
217
202
CommandType = CommandType . StoredProcedure ,
218
203
CommandTimeout = 120
219
204
} ;
220
- var reader = ( SqlDataReader ? ) null ;
221
205
222
206
command . CommandType = CommandType . StoredProcedure ;
223
207
@@ -232,7 +216,7 @@ public override Topic Load(int topicId, DateTime version) {
232
216
\-----------------------------------------------------------------------------------------------------------------------*/
233
217
try {
234
218
connection . Open ( ) ;
235
- reader = command . ExecuteReader ( ) ;
219
+ using var reader = command . ExecuteReader ( ) ;
236
220
topic = reader . LoadTopicGraph ( false ) ;
237
221
}
238
222
@@ -243,15 +227,6 @@ public override Topic Load(int topicId, DateTime version) {
243
227
throw new TopicRepositoryException ( $ "Topics failed to load: '{ exception . Message } '", exception ) ;
244
228
}
245
229
246
- /*------------------------------------------------------------------------------------------------------------------------
247
- | Close connection
248
- \-----------------------------------------------------------------------------------------------------------------------*/
249
- finally {
250
- reader ? . Dispose ( ) ;
251
- command ? . Dispose ( ) ;
252
- connection . Close ( ) ;
253
- }
254
-
255
230
/*------------------------------------------------------------------------------------------------------------------------
256
231
| Return objects
257
232
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -270,7 +245,8 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
270
245
\-----------------------------------------------------------------------------------------------------------------------*/
271
246
var version = new SqlDateTime ( DateTime . Now ) ;
272
247
var unresolvedTopics = new List < Topic > ( ) ;
273
- var connection = new SqlConnection ( _connectionString ) ;
248
+
249
+ using var connection = new SqlConnection ( _connectionString ) ;
274
250
275
251
connection . Open ( ) ;
276
252
@@ -290,7 +266,6 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
290
266
| Return value
291
267
\-----------------------------------------------------------------------------------------------------------------------*/
292
268
connection . Close ( ) ;
293
- connection . Dispose ( ) ;
294
269
return topicId ;
295
270
296
271
}
@@ -373,7 +348,7 @@ SqlDateTime version
373
348
/*------------------------------------------------------------------------------------------------------------------------
374
349
| Add indexed attributes that are dirty
375
350
\-----------------------------------------------------------------------------------------------------------------------*/
376
- var attributeValues = new AttributeValuesDataTable ( ) ;
351
+ using var attributeValues = new AttributeValuesDataTable ( ) ;
377
352
378
353
foreach ( var attributeValue in indexedAttributeList ) {
379
354
attributeValues . AddRow ( attributeValue . Key , attributeValue . Value ) ;
@@ -418,7 +393,8 @@ SqlDateTime version
418
393
| Establish database connection
419
394
\-----------------------------------------------------------------------------------------------------------------------*/
420
395
var procedureName = ! topic . IsSaved ? "CreateTopic" : "UpdateTopic" ;
421
- var command = new SqlCommand ( procedureName , connection ) {
396
+
397
+ using var command = new SqlCommand ( procedureName , connection ) {
422
398
CommandType = CommandType . StoredProcedure
423
399
} ;
424
400
@@ -477,21 +453,12 @@ SqlDateTime version
477
453
| Catch exception
478
454
\-----------------------------------------------------------------------------------------------------------------------*/
479
455
catch ( SqlException exception ) {
480
- connection ? . Dispose ( ) ;
481
456
throw new TopicRepositoryException (
482
457
$ "Failed to save Topic '{ topic . Key } ' ({ topic . Id } ) via '{ _connectionString } ': '{ exception . Message } '",
483
458
exception
484
459
) ;
485
460
}
486
461
487
- /*------------------------------------------------------------------------------------------------------------------------
488
- | Close connection
489
- \-----------------------------------------------------------------------------------------------------------------------*/
490
- finally {
491
- command ? . Dispose ( ) ;
492
- attributeValues . Dispose ( ) ;
493
- }
494
-
495
462
/*------------------------------------------------------------------------------------------------------------------------
496
463
| Return value
497
464
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -526,8 +493,8 @@ public override void Move(Topic topic, Topic target, Topic? sibling) {
526
493
/*------------------------------------------------------------------------------------------------------------------------
527
494
| Establish database connection
528
495
\-----------------------------------------------------------------------------------------------------------------------*/
529
- var connection = new SqlConnection ( _connectionString ) ;
530
- var command = new SqlCommand ( "MoveTopic" , connection ) {
496
+ using var connection = new SqlConnection ( _connectionString ) ;
497
+ using var command = new SqlCommand ( "MoveTopic" , connection ) {
531
498
CommandType = CommandType . StoredProcedure
532
499
} ;
533
500
@@ -560,14 +527,6 @@ public override void Move(Topic topic, Topic target, Topic? sibling) {
560
527
) ;
561
528
}
562
529
563
- /*------------------------------------------------------------------------------------------------------------------------
564
- | Close connection
565
- \-----------------------------------------------------------------------------------------------------------------------*/
566
- finally {
567
- command ? . Dispose ( ) ;
568
- connection . Dispose ( ) ;
569
- }
570
-
571
530
/*------------------------------------------------------------------------------------------------------------------------
572
531
| Reset dirty status
573
532
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -589,8 +548,8 @@ public override void Delete(Topic topic, bool isRecursive = false) {
589
548
/*------------------------------------------------------------------------------------------------------------------------
590
549
| Delete from database
591
550
\-----------------------------------------------------------------------------------------------------------------------*/
592
- var connection = new SqlConnection ( _connectionString ) ;
593
- var command = new SqlCommand ( "DeleteTopic" , connection ) {
551
+ using var connection = new SqlConnection ( _connectionString ) ;
552
+ using var command = new SqlCommand ( "DeleteTopic" , connection ) {
594
553
CommandType = CommandType . StoredProcedure
595
554
} ;
596
555
@@ -617,14 +576,6 @@ public override void Delete(Topic topic, bool isRecursive = false) {
617
576
) ;
618
577
}
619
578
620
- /*------------------------------------------------------------------------------------------------------------------------
621
- | Close connection
622
- \-----------------------------------------------------------------------------------------------------------------------*/
623
- finally {
624
- command ? . Dispose ( ) ;
625
- connection ? . Dispose ( ) ;
626
- }
627
-
628
579
}
629
580
630
581
/*==========================================================================================================================
@@ -644,7 +595,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
644
595
if ( ! topic . Relationships . Keys . Any ( ) ) {
645
596
return ;
646
597
}
647
- var command = ( SqlCommand ? ) null ;
648
598
649
599
try {
650
600
@@ -653,12 +603,12 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
653
603
\---------------------------------------------------------------------------------------------------------------------*/
654
604
foreach ( var key in topic . Relationships . Keys ) {
655
605
656
- var targetIds = new TopicListDataTable ( ) ;
657
606
var relatedTopics = topic . Relationships . GetTopics ( key ) ;
658
607
var topicId = topic . Id . ToString ( CultureInfo . InvariantCulture ) ;
659
608
var savedTopics = relatedTopics . Where ( t => t . IsSaved ) . Select < Topic , int > ( m => m . Id ) ;
660
609
661
- command = new SqlCommand ( "UpdateRelationships" , connection ) {
610
+ using var targetIds = new TopicListDataTable ( ) ;
611
+ using var command = new SqlCommand ( "UpdateRelationships" , connection ) {
662
612
CommandType = CommandType . StoredProcedure
663
613
} ;
664
614
@@ -673,8 +623,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
673
623
674
624
command . ExecuteNonQuery ( ) ;
675
625
676
- targetIds . Dispose ( ) ;
677
-
678
626
//Reset isDirty, assuming there aren't any unresolved references
679
627
relatedTopics . IsDirty = savedTopics . Count ( ) < relatedTopics . Count ;
680
628
@@ -692,14 +640,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
692
640
) ;
693
641
}
694
642
695
- /*------------------------------------------------------------------------------------------------------------------------
696
- | Close connection
697
- \-----------------------------------------------------------------------------------------------------------------------*/
698
- finally {
699
- command ? . Dispose ( ) ;
700
- //Since the SQL connection is being passed in, do not close connection; this allows connection pooling.
701
- }
702
-
703
643
/*------------------------------------------------------------------------------------------------------------------------
704
644
| Return
705
645
\-----------------------------------------------------------------------------------------------------------------------*/
0 commit comments