【问题标题】:Creating AWS initialization class创建 AWS 初始化类
【发布时间】:2018-08-08 03:35:16
【问题描述】:

我有一个连接到我的发电机数据库的 API。我的 API 有很多用于 GET、POST、Delete 等的端点。我正在使用以下代码:

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 调用中重复。

这是我的静态助手类:

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 中使用这个帮助类来初始化 AWS。

有没有更好的方法来初始化它?

【问题讨论】:

  • 这是 Asp.Net,还是可以使用依赖注入?如果没有,您能否将您的 awsdbClientawsContext 设为静态,这样您只需在应用程序启动时创建一次?
  • 这是asp.net核心
  • 如何在此处使用依赖注入并将其添加到我的启动类中
  • aws 为一切提供了一堆接口

标签: c# asp.net-core .net-core-2.0


【解决方案1】:

让我们利用 ASP.Net 的内置依赖注入。

我们需要制作一个快速界面来公开您需要的值。

public interface IDynamoDbClientAccessor
{
    DynamoDBContext GetContext();
}

还有一个我们稍后会用到的设置类。

public class DynamoDbClientAccessorSettings
{
    public string Id { get; set; }
    public string Password { get; set; }
    public string Region { get; set; }
}

现在是具体的类。

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 类中连接所有这些

services.AddSingleton<IDynamoDbClientAccessor, DynamoDbClientAccessor>();
services.Configure<DynamoDbClientAccessorSettings>(c =>
{
    c.Id = "YOUR ID";
    c.Password = "YOUR PASSWORD";
    c.Region = "YOUR REGION";
});

现在在您的控制器或其他 DI 服务中,您在构造函数中请求 IDynamoDbClientAccessor 实例。

一旦您对依赖注入更加熟悉,您就可以将更多的东西分解成它们自己的依赖服务。正如Daniel 所说,AWS SDK 甚至提供了一些接口供您使用,这些接口也可以提供帮助。

【讨论】:

  • 感谢@gunr2171 的详细解释,这对我很有帮助。实际上,我发现 AWS 文档不太详细。可能我需要深入研究一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多