创建 AWS 初始化 class
Creating AWS initialization class
我有一个 API 连接到我的发电机数据库。我的 API 有很多用于 GET、POST、删除等的端点。我正在使用以下代码:
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();
我的代码的前三行,即设置 awsCredentials、awsdbClient 和 awsContext 在我的每个 WEB API 调用中重复。
这是我的静态助手 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;
}
}
我在 API 中使用这个助手 class 来初始化 AWS。
有没有更好的初始化方法?
让我们利用 ASP.Net 的内置依赖注入。
我们需要制作一个快速界面来公开您需要的值。
public interface IDynamoDbClientAccessor
{
DynamoDBContext GetContext();
}
以及我们稍后会用到的设置 class。
public class DynamoDbClientAccessorSettings
{
public string Id { get; set; }
public string Password { get; set; }
public string Region { get; set; }
}
现在具体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;
}
}
将所有这些都连接到您的 Startup class
services.AddSingleton<IDynamoDbClientAccessor, DynamoDbClientAccessor>();
services.Configure<DynamoDbClientAccessorSettings>(c =>
{
c.Id = "YOUR ID";
c.Password = "YOUR PASSWORD";
c.Region = "YOUR REGION";
});
现在在您的控制器或其他 DI 服务中,您在构造函数中请求一个 IDynamoDbClientAccessor
实例。
一旦您更加熟悉依赖注入,您将能够将更多的东西分解成它们自己的依赖服务。正如 所说,AWS SDK 甚至提供了一些接口供您使用,这对您也有帮助。
我有一个 API 连接到我的发电机数据库。我的 API 有很多用于 GET、POST、删除等的端点。我正在使用以下代码:
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();
我的代码的前三行,即设置 awsCredentials、awsdbClient 和 awsContext 在我的每个 WEB API 调用中重复。
这是我的静态助手 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;
}
}
我在 API 中使用这个助手 class 来初始化 AWS。
有没有更好的初始化方法?
让我们利用 ASP.Net 的内置依赖注入。
我们需要制作一个快速界面来公开您需要的值。
public interface IDynamoDbClientAccessor
{
DynamoDBContext GetContext();
}
以及我们稍后会用到的设置 class。
public class DynamoDbClientAccessorSettings
{
public string Id { get; set; }
public string Password { get; set; }
public string Region { get; set; }
}
现在具体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;
}
}
将所有这些都连接到您的 Startup class
services.AddSingleton<IDynamoDbClientAccessor, DynamoDbClientAccessor>();
services.Configure<DynamoDbClientAccessorSettings>(c =>
{
c.Id = "YOUR ID";
c.Password = "YOUR PASSWORD";
c.Region = "YOUR REGION";
});
现在在您的控制器或其他 DI 服务中,您在构造函数中请求一个 IDynamoDbClientAccessor
实例。
一旦您更加熟悉依赖注入,您将能够将更多的东西分解成它们自己的依赖服务。正如