22
33namespace TemporalTableSample ;
44
5- public class Runner
5+ public class Runner ( BooksContext booksContext )
66{
7- private readonly BooksContext _booksContext ;
8- public Runner ( BooksContext booksContext )
9- {
10- _booksContext = booksContext ;
11- }
12-
137 public async Task CreateTheDatabaseAsync ( )
148 {
15- bool created = await _booksContext . Database . EnsureCreatedAsync ( ) ;
9+ bool created = await booksContext . Database . EnsureCreatedAsync ( ) ;
1610 string creationInfo = created ? "created" : "exists" ;
1711 Console . WriteLine ( $ "database { creationInfo } ") ;
1812 }
@@ -23,7 +17,7 @@ public async Task DeleteDatabaseAsync()
2317 string ? input = Console . ReadLine ( ) ;
2418 if ( input ? . ToLower ( ) == "y" )
2519 {
26- bool deleted = await _booksContext . Database . EnsureDeletedAsync ( ) ;
20+ bool deleted = await booksContext . Database . EnsureDeletedAsync ( ) ;
2721 string deletionInfo = deleted ? "deleted" : "not deleted" ;
2822 Console . WriteLine ( $ "database { deletionInfo } ") ;
2923 }
@@ -32,8 +26,8 @@ public async Task DeleteDatabaseAsync()
3226 public async Task AddBookAsync ( string title , string publisher )
3327 {
3428 Book book = new ( title , publisher ) ;
35- await _booksContext . Books . AddAsync ( book ) ;
36- int records = await _booksContext . SaveChangesAsync ( ) ;
29+ await booksContext . Books . AddAsync ( book ) ;
30+ int records = await booksContext . SaveChangesAsync ( ) ;
3731 Console . WriteLine ( $ "{ records } record added with id { book . BookId } ") ;
3832
3933 Console . WriteLine ( ) ;
@@ -45,8 +39,8 @@ public async Task AddBooksAsync()
4539 Book b2 = new ( "Professional C# 6 and .NET Core 1.0" , "Wrox Press" ) ;
4640 Book b3 = new ( "Professional C# 5 and .NET 4.5.1" , "Wrox Press" ) ;
4741 Book b4 = new ( "Essential Algorithms" , "Wiley" ) ;
48- await _booksContext . Books . AddRangeAsync ( b1 , b2 , b3 , b4 ) ;
49- int records = await _booksContext . SaveChangesAsync ( ) ;
42+ await booksContext . Books . AddRangeAsync ( b1 , b2 , b3 , b4 ) ;
43+ int records = await booksContext . SaveChangesAsync ( ) ;
5044 Console . WriteLine ( $ "{ records } records added") ;
5145
5246 Console . WriteLine ( ) ;
@@ -55,10 +49,10 @@ public async Task AddBooksAsync()
5549 public async Task ReadBooksAsync ( CancellationToken token = default )
5650 {
5751#if DEBUG
58- string query = _booksContext . Books . ToQueryString ( ) ;
52+ string query = booksContext . Books . ToQueryString ( ) ;
5953 Console . WriteLine ( query ) ;
6054#endif
61- List < Book > books = await _booksContext . Books . ToListAsync ( token ) ;
55+ List < Book > books = await booksContext . Books . ToListAsync ( token ) ;
6256 foreach ( var b in books )
6357 {
6458 Console . WriteLine ( $ "{ b . Title } { b . Publisher } ") ;
@@ -69,9 +63,9 @@ public async Task ReadBooksAsync(CancellationToken token = default)
6963
7064 public async Task QueryBooksAsync ( CancellationToken token = default )
7165 {
72- string query = _booksContext . Books . Where ( b => b . Publisher == "Wrox Press" ) . ToQueryString ( ) ;
66+ string query = booksContext . Books . Where ( b => b . Publisher == "Wrox Press" ) . ToQueryString ( ) ;
7367 Console . WriteLine ( query ) ;
74- await _booksContext . Books
68+ await booksContext . Books
7569 . Where ( b => b . Publisher == "Wrox Press" )
7670 . ForEachAsync ( b =>
7771 {
@@ -83,14 +77,14 @@ await _booksContext.Books
8377
8478 public async Task UpdateBookAsync ( )
8579 {
86- Book ? book = await _booksContext . Books . FindAsync ( 1 ) ;
80+ Book ? book = await booksContext . Books . FindAsync ( 1 ) ;
8781 Console . WriteLine ( "Just a short delay before updating..." ) ;
8882 await Task . Delay ( TimeSpan . FromSeconds ( 5 ) ) ;
8983
9084 if ( book != null )
9185 {
9286 book . Title = "Professional C# and .NET - 2021 Edition" ;
93- int records = await _booksContext . SaveChangesAsync ( ) ;
87+ int records = await booksContext . SaveChangesAsync ( ) ;
9488 Console . WriteLine ( $ "{ records } record updated") ;
9589 }
9690 Console . WriteLine ( ) ;
@@ -99,13 +93,13 @@ public async Task UpdateBookAsync()
9993
10094 public async Task TemporalPointInTimeQueryAsync ( )
10195 {
102- Book ? book = await _booksContext . Books . FindAsync ( 1 ) ;
96+ Book ? book = await booksContext . Books . FindAsync ( 1 ) ;
10397 if ( book is null ) return ;
10498 // read shadow property for time
105- if ( _booksContext . Entry ( book ) . CurrentValues [ "PeriodStart" ] is DateTime periodStart )
99+ if ( booksContext . Entry ( book ) . CurrentValues [ "PeriodStart" ] is DateTime periodStart )
106100 {
107101 DateTime previousTime = periodStart . AddSeconds ( - 4 ) ;
108- var previousBook = await _booksContext . Books
102+ var previousBook = await booksContext . Books
109103 . TemporalAsOf ( previousTime )
110104 . TagWith ( "temporalasof" )
111105 . SingleOrDefaultAsync ( b => b . BookId == book . BookId ) ;
@@ -121,21 +115,21 @@ public async Task TemporalPointInTimeQueryAsync()
121115
122116 public async Task TemporalAllQueryAsync ( )
123117 {
124- await _booksContext . Books
118+ await booksContext . Books
125119 . TemporalAll ( )
126120 . TagWith ( "temporalall" )
127121 . ForEachAsync ( b =>
128122 {
129- var entry = _booksContext . Entry ( b ) ;
123+ var entry = booksContext . Entry ( b ) ;
130124 Console . WriteLine ( $ "{ b . Title } { entry . State } ") ;
131125 } ) ;
132126 }
133127
134128 public async Task DeleteBooksAsync ( )
135129 {
136- List < Book > books = await _booksContext . Books . ToListAsync ( ) ;
137- _booksContext . Books . RemoveRange ( books ) ;
138- int records = await _booksContext . SaveChangesAsync ( ) ;
130+ List < Book > books = await booksContext . Books . ToListAsync ( ) ;
131+ booksContext . Books . RemoveRange ( books ) ;
132+ int records = await booksContext . SaveChangesAsync ( ) ;
139133 Console . WriteLine ( $ "{ records } records deleted") ;
140134
141135 Console . WriteLine ( ) ;
0 commit comments