Skip to content

Commit f85aab2

Browse files
committed
Migrated to C# 8's new inline using syntax
With C# 8, we no longer need to nest `IDisposable`s inside a `using {}` block or manually call `Dispose()` as part of e.g. a `finally {}` block. Instead, by prefixing the `IDisposable`'s declaration with `using`, they will automatically be disposed of when the code execution exits the current block. This allows us to entirely remove all of the `finally {}` blocks. Because `using var variable` doesn't align neatly with `var variable`, I've added a line break between the declaration of `using var` and `var` blocks. I'm not sure if this is a best practice yet—it's a gap in our style guide for now—but I _think_ it's a preference. We'll keep evaluating this as we implement the pattern more frequently.
1 parent 80bcdd7 commit f85aab2

File tree

1 file changed

+22
-82
lines changed

1 file changed

+22
-82
lines changed

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 22 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ public override Topic Load(string? topicKey = null, bool isRecursive = true) {
7979
/*------------------------------------------------------------------------------------------------------------------------
8080
| Establish database connection
8181
\-----------------------------------------------------------------------------------------------------------------------*/
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+
8485
var topicId = -1;
8586

8687
command.CommandType = CommandType.StoredProcedure;
@@ -110,14 +111,6 @@ public override Topic Load(string? topicKey = null, bool isRecursive = true) {
110111
throw new TopicRepositoryException($"Topic(s) failed to load: '{exception.Message}'", exception);
111112
}
112113

113-
/*------------------------------------------------------------------------------------------------------------------------
114-
| Close connection
115-
\-----------------------------------------------------------------------------------------------------------------------*/
116-
finally {
117-
command?.Dispose();
118-
connection.Close();
119-
}
120-
121114
/*------------------------------------------------------------------------------------------------------------------------
122115
| Validate results
123116
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -139,12 +132,12 @@ public override Topic Load(int topicId, bool isRecursive = true) {
139132
| Establish database connection
140133
\-----------------------------------------------------------------------------------------------------------------------*/
141134
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) {
144138
CommandType = CommandType.StoredProcedure,
145139
CommandTimeout = 120
146140
};
147-
var reader = (SqlDataReader?)null;
148141

149142
/*------------------------------------------------------------------------------------------------------------------------
150143
| Establish query parameters
@@ -157,7 +150,7 @@ public override Topic Load(int topicId, bool isRecursive = true) {
157150
\-----------------------------------------------------------------------------------------------------------------------*/
158151
try {
159152
connection.Open();
160-
reader = command.ExecuteReader();
153+
using var reader = command.ExecuteReader();
161154
topic = reader.LoadTopicGraph();
162155
}
163156

@@ -168,15 +161,6 @@ public override Topic Load(int topicId, bool isRecursive = true) {
168161
throw new TopicRepositoryException($"Topics failed to load: '{exception.Message}'", exception);
169162
}
170163

171-
/*------------------------------------------------------------------------------------------------------------------------
172-
| Close connection
173-
\-----------------------------------------------------------------------------------------------------------------------*/
174-
finally {
175-
reader?.Dispose();
176-
command?.Dispose();
177-
connection.Close();
178-
}
179-
180164
/*------------------------------------------------------------------------------------------------------------------------
181165
| Validate results
182166
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -212,12 +196,12 @@ public override Topic Load(int topicId, DateTime version) {
212196
| Establish database connection
213197
\-----------------------------------------------------------------------------------------------------------------------*/
214198
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) {
217202
CommandType = CommandType.StoredProcedure,
218203
CommandTimeout = 120
219204
};
220-
var reader = (SqlDataReader?)null;
221205

222206
command.CommandType = CommandType.StoredProcedure;
223207

@@ -232,7 +216,7 @@ public override Topic Load(int topicId, DateTime version) {
232216
\-----------------------------------------------------------------------------------------------------------------------*/
233217
try {
234218
connection.Open();
235-
reader = command.ExecuteReader();
219+
using var reader = command.ExecuteReader();
236220
topic = reader.LoadTopicGraph(false);
237221
}
238222

@@ -243,15 +227,6 @@ public override Topic Load(int topicId, DateTime version) {
243227
throw new TopicRepositoryException($"Topics failed to load: '{exception.Message}'", exception);
244228
}
245229

246-
/*------------------------------------------------------------------------------------------------------------------------
247-
| Close connection
248-
\-----------------------------------------------------------------------------------------------------------------------*/
249-
finally {
250-
reader?.Dispose();
251-
command?.Dispose();
252-
connection.Close();
253-
}
254-
255230
/*------------------------------------------------------------------------------------------------------------------------
256231
| Return objects
257232
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -270,7 +245,8 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
270245
\-----------------------------------------------------------------------------------------------------------------------*/
271246
var version = new SqlDateTime(DateTime.Now);
272247
var unresolvedTopics = new List<Topic>();
273-
var connection = new SqlConnection(_connectionString);
248+
249+
using var connection = new SqlConnection(_connectionString);
274250

275251
connection.Open();
276252

@@ -290,7 +266,6 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
290266
| Return value
291267
\-----------------------------------------------------------------------------------------------------------------------*/
292268
connection.Close();
293-
connection.Dispose();
294269
return topicId;
295270

296271
}
@@ -373,7 +348,7 @@ SqlDateTime version
373348
/*------------------------------------------------------------------------------------------------------------------------
374349
| Add indexed attributes that are dirty
375350
\-----------------------------------------------------------------------------------------------------------------------*/
376-
var attributeValues = new AttributeValuesDataTable();
351+
using var attributeValues = new AttributeValuesDataTable();
377352

378353
foreach (var attributeValue in indexedAttributeList) {
379354
attributeValues.AddRow(attributeValue.Key, attributeValue.Value);
@@ -418,7 +393,8 @@ SqlDateTime version
418393
| Establish database connection
419394
\-----------------------------------------------------------------------------------------------------------------------*/
420395
var procedureName = !topic.IsSaved? "CreateTopic" : "UpdateTopic";
421-
var command = new SqlCommand(procedureName, connection) {
396+
397+
using var command = new SqlCommand(procedureName, connection) {
422398
CommandType = CommandType.StoredProcedure
423399
};
424400

@@ -477,21 +453,12 @@ SqlDateTime version
477453
| Catch exception
478454
\-----------------------------------------------------------------------------------------------------------------------*/
479455
catch (SqlException exception) {
480-
connection?.Dispose();
481456
throw new TopicRepositoryException(
482457
$"Failed to save Topic '{topic.Key}' ({topic.Id}) via '{_connectionString}': '{exception.Message}'",
483458
exception
484459
);
485460
}
486461

487-
/*------------------------------------------------------------------------------------------------------------------------
488-
| Close connection
489-
\-----------------------------------------------------------------------------------------------------------------------*/
490-
finally {
491-
command?.Dispose();
492-
attributeValues.Dispose();
493-
}
494-
495462
/*------------------------------------------------------------------------------------------------------------------------
496463
| Return value
497464
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -526,8 +493,8 @@ public override void Move(Topic topic, Topic target, Topic? sibling) {
526493
/*------------------------------------------------------------------------------------------------------------------------
527494
| Establish database connection
528495
\-----------------------------------------------------------------------------------------------------------------------*/
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) {
531498
CommandType = CommandType.StoredProcedure
532499
};
533500

@@ -560,14 +527,6 @@ public override void Move(Topic topic, Topic target, Topic? sibling) {
560527
);
561528
}
562529

563-
/*------------------------------------------------------------------------------------------------------------------------
564-
| Close connection
565-
\-----------------------------------------------------------------------------------------------------------------------*/
566-
finally {
567-
command?.Dispose();
568-
connection.Dispose();
569-
}
570-
571530
/*------------------------------------------------------------------------------------------------------------------------
572531
| Reset dirty status
573532
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -589,8 +548,8 @@ public override void Delete(Topic topic, bool isRecursive = false) {
589548
/*------------------------------------------------------------------------------------------------------------------------
590549
| Delete from database
591550
\-----------------------------------------------------------------------------------------------------------------------*/
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) {
594553
CommandType = CommandType.StoredProcedure
595554
};
596555

@@ -617,14 +576,6 @@ public override void Delete(Topic topic, bool isRecursive = false) {
617576
);
618577
}
619578

620-
/*------------------------------------------------------------------------------------------------------------------------
621-
| Close connection
622-
\-----------------------------------------------------------------------------------------------------------------------*/
623-
finally {
624-
command?.Dispose();
625-
connection?.Dispose();
626-
}
627-
628579
}
629580

630581
/*==========================================================================================================================
@@ -644,7 +595,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
644595
if (!topic.Relationships.Keys.Any()) {
645596
return;
646597
}
647-
var command = (SqlCommand?)null;
648598

649599
try {
650600

@@ -653,12 +603,12 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
653603
\---------------------------------------------------------------------------------------------------------------------*/
654604
foreach (var key in topic.Relationships.Keys) {
655605

656-
var targetIds = new TopicListDataTable();
657606
var relatedTopics = topic.Relationships.GetTopics(key);
658607
var topicId = topic.Id.ToString(CultureInfo.InvariantCulture);
659608
var savedTopics = relatedTopics.Where(t => t.IsSaved).Select<Topic, int>(m => m.Id);
660609

661-
command = new SqlCommand("UpdateRelationships", connection) {
610+
using var targetIds = new TopicListDataTable();
611+
using var command = new SqlCommand("UpdateRelationships", connection) {
662612
CommandType = CommandType.StoredProcedure
663613
};
664614

@@ -673,8 +623,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
673623

674624
command.ExecuteNonQuery();
675625

676-
targetIds.Dispose();
677-
678626
//Reset isDirty, assuming there aren't any unresolved references
679627
relatedTopics.IsDirty = savedTopics.Count() < relatedTopics.Count;
680628

@@ -692,14 +640,6 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
692640
);
693641
}
694642

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-
703643
/*------------------------------------------------------------------------------------------------------------------------
704644
| Return
705645
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)