99
1010namespace DynamoDBActions ;
1111
12- public class DynamoDbMethods
12+ /// <summary>
13+ /// Methods of this class perform Amazon DynamoDB operations.
14+ /// </summary>
15+ public class DynamoDbWrapper
1316{
17+ private readonly IAmazonDynamoDB _amazonDynamoDB ;
18+
19+ /// <summary>
20+ /// Constructor for the DynamoDbWrapper class.
21+ /// </summary>
22+ /// <param name="amazonDynamoDB">The injected DynamoDB client.</param>
23+ public DynamoDbWrapper ( IAmazonDynamoDB amazonDynamoDB )
24+ {
25+ _amazonDynamoDB = amazonDynamoDB ;
26+ }
1427 // snippet-start:[DynamoDB.dotnetv4.dynamodb-basics.CreateTable]
1528
1629 /// <summary>
@@ -20,11 +33,11 @@ public class DynamoDbMethods
2033 /// <param name="client">An initialized Amazon DynamoDB client object.</param>
2134 /// <param name="tableName">The name of the table to create.</param>
2235 /// <returns>A Boolean value indicating the success of the operation.</returns>
23- public static async Task < bool > CreateMovieTableAsync ( AmazonDynamoDBClient client , string tableName )
36+ public async Task < bool > CreateMovieTableAsync ( string tableName )
2437 {
2538 try
2639 {
27- var response = await client . CreateTableAsync ( new CreateTableRequest
40+ var response = await _amazonDynamoDB . CreateTableAsync ( new CreateTableRequest
2841 {
2942 TableName = tableName ,
3043 AttributeDefinitions = new List < AttributeDefinition > ( )
@@ -72,7 +85,7 @@ public static async Task<bool> CreateMovieTableAsync(AmazonDynamoDBClient client
7285 {
7386 Thread . Sleep ( sleepDuration ) ;
7487
75- var describeTableResponse = await client . DescribeTableAsync ( request ) ;
88+ var describeTableResponse = await _amazonDynamoDB . DescribeTableAsync ( request ) ;
7689 status = describeTableResponse . Table . TableStatus ;
7790
7891 Console . Write ( "." ) ;
@@ -110,7 +123,7 @@ public static async Task<bool> CreateMovieTableAsync(AmazonDynamoDBClient client
110123 /// the movie to add to the table.</param>
111124 /// <param name="tableName">The name of the table where the item will be added.</param>
112125 /// <returns>A Boolean value that indicates the results of adding the item.</returns>
113- public static async Task < bool > PutItemAsync ( AmazonDynamoDBClient client , Movie newMovie , string tableName )
126+ public async Task < bool > PutItemAsync ( Movie newMovie , string tableName )
114127 {
115128 try
116129 {
@@ -126,7 +139,7 @@ public static async Task<bool> PutItemAsync(AmazonDynamoDBClient client, Movie n
126139 Item = item ,
127140 } ;
128141
129- await client . PutItemAsync ( request ) ;
142+ await _amazonDynamoDB . PutItemAsync ( request ) ;
130143 return true ;
131144 }
132145 catch ( ResourceNotFoundException ex )
@@ -160,8 +173,7 @@ public static async Task<bool> PutItemAsync(AmazonDynamoDBClient client, Movie n
160173 /// information that will be changed.</param>
161174 /// <param name="tableName">The name of the table that contains the movie.</param>
162175 /// <returns>A Boolean value that indicates the success of the operation.</returns>
163- public static async Task < bool > UpdateItemAsync (
164- AmazonDynamoDBClient client ,
176+ public async Task < bool > UpdateItemAsync (
165177 Movie newMovie ,
166178 MovieInfo newInfo ,
167179 string tableName )
@@ -195,7 +207,7 @@ public static async Task<bool> UpdateItemAsync(
195207 TableName = tableName ,
196208 } ;
197209
198- await client . UpdateItemAsync ( request ) ;
210+ await _amazonDynamoDB . UpdateItemAsync ( request ) ;
199211 return true ;
200212 }
201213 catch ( ResourceNotFoundException ex )
@@ -228,7 +240,7 @@ public static async Task<bool> UpdateItemAsync(
228240 /// <param name="tableName">The name of the table containing the movie.</param>
229241 /// <returns>A Dictionary object containing information about the item
230242 /// retrieved.</returns>
231- public static async Task < Dictionary < string , AttributeValue > > GetItemAsync ( AmazonDynamoDBClient client , Movie newMovie , string tableName )
243+ public async Task < Dictionary < string , AttributeValue > > GetItemAsync ( Movie newMovie , string tableName )
232244 {
233245 try
234246 {
@@ -244,7 +256,7 @@ public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(Amazon
244256 TableName = tableName ,
245257 } ;
246258
247- var response = await client . GetItemAsync ( request ) ;
259+ var response = await _amazonDynamoDB . GetItemAsync ( request ) ;
248260 return response . Item ;
249261 }
250262 catch ( ResourceNotFoundException ex )
@@ -274,7 +286,7 @@ public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(Amazon
274286 /// </summary>
275287 /// <param name="movieFileName">The full path to the JSON file.</param>
276288 /// <returns>A generic list of movie objects.</returns>
277- public static List < Movie > ImportMovies ( string movieFileName )
289+ public List < Movie > ImportMovies ( string movieFileName )
278290 {
279291 var moviesList = new List < Movie > ( ) ;
280292 if ( ! File . Exists ( movieFileName ) )
@@ -307,8 +319,7 @@ public static List<Movie> ImportMovies(string movieFileName)
307319 /// the JSON file containing movie data.</param>
308320 /// <returns>A long integer value representing the number of movies
309321 /// imported from the JSON file.</returns>
310- public static async Task < long > BatchWriteItemsAsync (
311- AmazonDynamoDBClient client ,
322+ public async Task < long > BatchWriteItemsAsync (
312323 string movieFileName , string tableName )
313324 {
314325 try
@@ -322,7 +333,7 @@ public static async Task<long> BatchWriteItemsAsync(
322333
323334 var context = new DynamoDBContextBuilder ( )
324335 // Optional call to provide a specific instance of IAmazonDynamoDB
325- . WithDynamoDBClient ( ( ) => client )
336+ . WithDynamoDBClient ( ( ) => _amazonDynamoDB )
326337 . Build ( ) ;
327338
328339 var movieBatch = context . CreateBatchWrite < Movie > (
@@ -368,8 +379,7 @@ public static async Task<long> BatchWriteItemsAsync(
368379 /// year of the movie to delete.</param>
369380 /// <returns>A Boolean value indicating the success or failure of the
370381 /// delete operation.</returns>
371- public static async Task < bool > DeleteItemAsync (
372- AmazonDynamoDBClient client ,
382+ public async Task < bool > DeleteItemAsync (
373383 string tableName ,
374384 Movie movieToDelete )
375385 {
@@ -383,7 +393,7 @@ public static async Task<bool> DeleteItemAsync(
383393
384394 var request = new DeleteItemRequest { TableName = tableName , Key = key , } ;
385395
386- await client . DeleteItemAsync ( request ) ;
396+ await _amazonDynamoDB . DeleteItemAsync ( request ) ;
387397 return true ;
388398 }
389399 catch ( ResourceNotFoundException ex )
@@ -416,11 +426,11 @@ public static async Task<bool> DeleteItemAsync(
416426 /// <param name="year">The release year for which we want to
417427 /// view movies.</param>
418428 /// <returns>The number of movies that match the query.</returns>
419- public static async Task < int > QueryMoviesAsync ( AmazonDynamoDBClient client , string tableName , int year )
429+ public async Task < int > QueryMoviesAsync ( string tableName , int year )
420430 {
421431 try
422432 {
423- var movieTable = new TableBuilder ( client , tableName )
433+ var movieTable = new TableBuilder ( _amazonDynamoDB , tableName )
424434 . AddHashKey ( "year" , DynamoDBEntryType . Numeric )
425435 . AddRangeKey ( "title" , DynamoDBEntryType . String )
426436 . Build ( ) ;
@@ -481,8 +491,7 @@ public static async Task<int> QueryMoviesAsync(AmazonDynamoDBClient client, stri
481491 // snippet-end:[DynamoDB.dotnetv4.dynamodb-basics.QueryItems]
482492
483493 // snippet-start:[DynamoDB.dotnetv4.dynamodb-basics.ScanTable]
484- public static async Task < int > ScanTableAsync (
485- AmazonDynamoDBClient client ,
494+ public async Task < int > ScanTableAsync (
486495 string tableName ,
487496 int startYear ,
488497 int endYear )
@@ -512,7 +521,7 @@ public static async Task<int> ScanTableAsync(
512521 var response = new ScanResponse ( ) ;
513522 do
514523 {
515- response = await client . ScanAsync ( request ) ;
524+ response = await _amazonDynamoDB . ScanAsync ( request ) ;
516525 foundCount += response . Items . Count ;
517526 response . Items . ForEach ( i => DisplayItem ( i ) ) ;
518527 request . ExclusiveStartKey = response . LastEvaluatedKey ;
@@ -540,7 +549,7 @@ public static async Task<int> ScanTableAsync(
540549 // snippet-end:[DynamoDB.dotnetv4.dynamodb-basics.ScanTable]
541550
542551 // snippet-start:[DynamoDB.dotnetv4.dynamodb-basics.DeleteTableExample]
543- public static async Task < bool > DeleteTableAsync ( AmazonDynamoDBClient client , string tableName )
552+ public async Task < bool > DeleteTableAsync ( string tableName )
544553 {
545554 try
546555 {
@@ -549,7 +558,7 @@ public static async Task<bool> DeleteTableAsync(AmazonDynamoDBClient client, str
549558 TableName = tableName ,
550559 } ;
551560
552- var response = await client . DeleteTableAsync ( request ) ;
561+ var response = await _amazonDynamoDB . DeleteTableAsync ( request ) ;
553562
554563 Console . WriteLine ( $ "Table { response . TableDescription . TableName } successfully deleted.") ;
555564 return true ;
@@ -578,7 +587,7 @@ public static async Task<bool> DeleteTableAsync(AmazonDynamoDBClient client, str
578587 /// Displays a DynamoDB document on the console.
579588 /// </summary>
580589 /// <param name="document">The DynamoDB document to display.</param>
581- public static void DisplayDocument ( Document document )
590+ public void DisplayDocument ( Document document )
582591 {
583592 Console . WriteLine ( $ "{ document [ "year" ] } \t { document [ "title" ] } ") ;
584593 }
@@ -587,7 +596,7 @@ public static void DisplayDocument(Document document)
587596 /// Displays a DynamoDB item on the console.
588597 /// </summary>
589598 /// <param name="item">The DynamoDB item to display.</param>
590- public static void DisplayItem ( Dictionary < string , AttributeValue > item )
599+ public void DisplayItem ( Dictionary < string , AttributeValue > item )
591600 {
592601 Console . WriteLine ( $ "{ item [ "year" ] . N } \t { item [ "title" ] . S } ") ;
593602 }
0 commit comments