44
55namespace MoviesAppSample . DataAccess
66{
7- internal sealed class PersonDal : SQLiteDalBase
7+ internal sealed class PersonDal ( Func < SQLiteDbConnection > createConnection ) : SQLiteDalBase ( createConnection )
88 {
9- private static readonly string [ ] s_columns = { "Id" , "Name" } ;
9+ private static readonly string [ ] s_columns = [ "Id" , "Name" ] ;
1010
11- private static readonly string [ ] s_idColumns = { "Id" } ;
11+ private static readonly string [ ] s_idColumns = [ "Id" ] ;
1212
13- private static readonly string [ ] s_updateColumns = { "Name" } ;
13+ private static readonly string [ ] s_updateColumns = [ "Name" ] ;
1414
15- private static readonly string [ ] s_relationshipColumns = { "MovieId" , "PersonId" } ;
15+ private static readonly string [ ] s_relationshipColumns = [ "MovieId" , "PersonId" ] ;
1616
17- private static readonly string [ ] s_relationshipTables = { "MovieCasts" , "MovieDirectors" , "MovieWriters" } ;
18-
19- public PersonDal ( Func < SQLiteDbConnection > createConnection ) : base ( createConnection )
20- {
21- }
17+ private static readonly string [ ] s_relationshipTables = [ "MovieCasts" , "MovieDirectors" , "MovieWriters" ] ;
2218
2319 #region Properties
2420
@@ -40,7 +36,7 @@ public ValueTask<bool> DeleteMoviePersonsAsync(SQLiteDbContext context, long mov
4036 {
4137 using var command = ctx . Connection . CreateCommandDelete ( tableName , "WHERE MovieId=@MovieId" ,
4238 DbType . Int64 . CreateInputParam ( "@MovieId" , movieId ) ) ;
43- affected += ctx . Connection . ExecuteNonQuery ( command ) ;
39+ affected += ctx . Connection . ExecuteNonQuery ( command , cancellationToken ) ;
4440 }
4541 return affected > 0 ;
4642 } , cancellationToken ) ;
@@ -78,13 +74,12 @@ private ValueTask<List<PersonDto>> LoadMoviePersonsAsync(SQLiteDbContext context
7874 var list = new List < PersonDto > ( ) ;
7975 void Read ( SQLiteDataReader reader )
8076 {
81- cancellationToken . ThrowIfCancellationRequested ( ) ;
8277 list . Add ( new PersonDto (
8378 reader . GetInt64 ( 0 ) ,
8479 reader . GetString ( 1 )
8580 ) ) ;
8681 }
87- int count = ctx . Connection . ExecuteReader ( command , Read ) ;
82+ int count = ctx . Connection . ExecuteReader ( command , Read , cancellationToken ) ;
8883 Debug . Assert ( list . Count == count ) ;
8984 return list ;
9085 } , cancellationToken ) ;
@@ -119,7 +114,7 @@ private ValueTask<bool> SaveMoviePersonsAsync(SQLiteDbContext context, string ta
119114 cancellationToken . ThrowIfCancellationRequested ( ) ;
120115 return TryExecuteInDbContextAsync ( context , ctx =>
121116 {
122- var ( adapter , table ) = ctx . Connection . SelectForUpdate ( tableName , s_relationshipColumns , "WHERE MovieId=@MovieId" , true ,
117+ var ( adapter , table ) = ctx . Connection . SelectForUpdate ( tableName , s_relationshipColumns , "WHERE MovieId=@MovieId" , true , cancellationToken ,
123118 DbType . Int64 . CreateInputParam ( "@MovieId" , movieId ) ) ;
124119 //table.PrimaryKey = new[] { table.Columns["MovieId"], table.Columns["PersonId"] };
125120 using ( adapter )
@@ -146,7 +141,7 @@ private ValueTask<bool> SaveMoviePersonsAsync(SQLiteDbContext context, string ta
146141 int num = 0 ;
147142 if ( table . HasChanges ( ) )
148143 {
149- num = ctx . Connection . Update ( adapter , table ) ;
144+ num = ctx . Connection . Update ( adapter , table , cancellationToken ) ;
150145 }
151146 table . ClearAndDispose ( ) ;
152147 return num > 0 ;
@@ -176,11 +171,11 @@ public ValueTask<bool> SavePersonsAsync(SQLiteDbContext context, List<PersonDto>
176171 {
177172 var dto = dtos [ i ] ;
178173 commandInsert . Parameters [ "@Name" ] . Value = dto . Name ;
179- var id = ctx . Connection . ExecuteScalar ( commandInsert ) ;
174+ var id = ctx . Connection . ExecuteScalar ( commandInsert , cancellationToken ) ;
180175 if ( id is null ) //person alreasy exists
181176 {
182177 commandSelect . Parameters [ "@Name" ] . Value = dto . Name ;
183- id = ctx . Connection . ExecuteScalar ( commandSelect ) ;
178+ id = ctx . Connection . ExecuteScalar ( commandSelect , cancellationToken ) ;
184179 Debug . Assert ( id is not null ) ;
185180 }
186181 Debug . Assert ( id is long ) ;
0 commit comments