@@ -93,8 +93,8 @@ public class BlogPostRepository(MySqlDataSource database)
93
93
{
94
94
public async Task <BlogPost ?> FindOneAsync (int id )
95
95
{
96
- using var connection = await database .OpenConnectionAsync ();
97
- using var command = connection .CreateCommand ();
96
+ await using var connection = await database .OpenConnectionAsync ();
97
+ await using var command = connection .CreateCommand ();
98
98
command .CommandText = @" SELECT `Id`, `Title`, `Content` FROM `BlogPost` WHERE `Id` = @id" ;
99
99
command .Parameters .AddWithValue (" @id" , id );
100
100
var result = await ReadAllAsync (await command .ExecuteReaderAsync ());
@@ -103,24 +103,24 @@ public class BlogPostRepository(MySqlDataSource database)
103
103
104
104
public async Task <IReadOnlyList <BlogPost >> LatestPostsAsync ()
105
105
{
106
- using var connection = await database .OpenConnectionAsync ();
107
- using var command = connection .CreateCommand ();
106
+ await using var connection = await database .OpenConnectionAsync ();
107
+ await using var command = connection .CreateCommand ();
108
108
command .CommandText = @" SELECT `Id`, `Title`, `Content` FROM `BlogPost` ORDER BY `Id` DESC LIMIT 10;" ;
109
109
return await ReadAllAsync (await command .ExecuteReaderAsync ());
110
110
}
111
111
112
112
public async Task DeleteAllAsync ()
113
113
{
114
- using var connection = await database .OpenConnectionAsync ();
115
- using var command = connection .CreateCommand ();
114
+ await using var connection = await database .OpenConnectionAsync ();
115
+ await using var command = connection .CreateCommand ();
116
116
command .CommandText = @" DELETE FROM `BlogPost`" ;
117
117
await command .ExecuteNonQueryAsync ();
118
118
}
119
119
120
120
public async Task InsertAsync (BlogPost blogPost )
121
121
{
122
- using var connection = await database .OpenConnectionAsync ();
123
- using var command = connection .CreateCommand ();
122
+ await using var connection = await database .OpenConnectionAsync ();
123
+ await using var command = connection .CreateCommand ();
124
124
command .CommandText = @" INSERT INTO `BlogPost` (`Title`, `Content`) VALUES (@title, @content);" ;
125
125
BindParams (command , blogPost );
126
126
await command .ExecuteNonQueryAsync ();
@@ -129,8 +129,8 @@ public class BlogPostRepository(MySqlDataSource database)
129
129
130
130
public async Task UpdateAsync (BlogPost blogPost )
131
131
{
132
- using var connection = await database .OpenConnectionAsync ();
133
- using var command = connection .CreateCommand ();
132
+ await using var connection = await database .OpenConnectionAsync ();
133
+ await using var command = connection .CreateCommand ();
134
134
command .CommandText = @" UPDATE `BlogPost` SET `Title` = @title, `Content` = @content WHERE `Id` = @id;" ;
135
135
BindParams (command , blogPost );
136
136
BindId (command , blogPost );
@@ -139,8 +139,8 @@ public class BlogPostRepository(MySqlDataSource database)
139
139
140
140
public async Task DeleteAsync (BlogPost blogPost )
141
141
{
142
- using var connection = await database .OpenConnectionAsync ();
143
- using var command = connection .CreateCommand ();
142
+ await using var connection = await database .OpenConnectionAsync ();
143
+ await using var command = connection .CreateCommand ();
144
144
command .CommandText = @" DELETE FROM `BlogPost` WHERE `Id` = @id;" ;
145
145
BindId (command , blogPost );
146
146
await command .ExecuteNonQueryAsync ();
0 commit comments