Creating AWS initialization class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Creating AWS initialization class



I have a API which connects to my dynamo db. My API has quite a few endpoints for GET, POST, Delete etc. I am using the following code:


var awsCredentials = Helper.AwsCredentials(id, password);
var awsdbClient = Helper.DbClient(awsCredentials, "us-east-2");
var awsContext = Helper.DynamoDbContext(awsdbClient);

List<ScanCondition> conditions = new List<ScanCondition>();
var response = await context.ScanAsync<MyData>(conditions).GetRemainingAsync();

return response.ToList();



The first three lines of my code ie setting awsCredentials, awsdbClient & awsContext are repeated in each of my WEB API call.



And this is my static helper class:


public static class Helper

public static BasicAWSCredentials AwsCredentials(string id, string password)

var credentials = new BasicAWSCredentials(id, password);
return credentials;

public static AmazonDynamoDBClient DynamoDbClient(BasicAWSCredentials credentials, RegionEndpoint region)

var client = new DBClient(credentials, region);
return client;

public static DynamoDBContext DynamoDbContext(AmazonDynamoDBClient client)

var context = new DynamoDBContext(client);
return context;




I use this helper class in my API to initialize AWS.



Is there a better way to initialize this?





Is this Asp.Net, or are you able to use Dependency Injection? If not, could you make your awsdbClient or awsContext static so you only need to create those once when the application boots?
– gunr2171
Aug 7 at 19:18


awsdbClient


awsContext





This is asp.net core
– kaka1234
Aug 7 at 19:19





How can I use dependency injection here and add that in my start up class
– kaka1234
Aug 7 at 19:21





aws provides a bunch of interfaces for everything
– Daniel A. White
Aug 7 at 19:26




1 Answer
1



Let's take advantage of ASP.Net's built-in Dependency Injection.



We need to make a quick interface to expose the values you need.


public interface IDynamoDbClientAccessor

DynamoDBContext GetContext();



And a settings class that we'll use in a bit.


public class DynamoDbClientAccessorSettings

public string Id get; set;
public string Password get; set;
public string Region get; set;



Now the concrete class.


public class DynamoDbClientAccessor : IDynamoDbClientAccessor

private readonly DynamoDbClientAccessorSettings settings;

public DynamoDbClientAccessor(IOptions<DynamoDbClientAccessorSettings> options)

settings = options?.Value ?? throw new ArgumentNullException(nameof(options));


public DynamoDBContext GetContext()

// You have the option to alter this if you don't
// want to create a new context each time.
// Have a private variable at the top of this class
// of type DynamoDBContext. If that variable is not null,
// return the value. If it is null, create a new value,
// set the variable, and return it.

var awsCredentials = Helper.AwsCredentials(settings.Id, settings.Password);
var awsdbClient = Helper.DbClient(awsCredentials, settings.Region);
var awsContext = Helper.DynamoDbContext(awsdbClient);

return awsContext;




Hook all of this up in your Startup class


services.AddSingleton<IDynamoDbClientAccessor, DynamoDbClientAccessor>();
services.Configure<DynamoDbClientAccessorSettings>(c =>

c.Id = "YOUR ID";
c.Password = "YOUR PASSWORD";
c.Region = "YOUR REGION";
);



Now in your controller or other DI service you ask for a IDynamoDbClientAccessor instance in the constructor.


IDynamoDbClientAccessor



Once you get more familar with Dependency Injection you'll be able to break apart more things into their own dependent services. As Daniel says, the AWS SDK even provides some interfaces for you to use which can help as well.





Thanks @gunr2171 for the detailed explanation, this really helped me. I actually found AWS documentation to be less detailed. May be I need to dig more into that.
– kaka1234
Aug 8 at 2:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS