我有一個特定的 repo 類,用于使 AWS DynamoDB 資料庫呼叫充滿類似于下一個的方法:
public List<TestType> getTestTypes()
{
List<TestType> scanResult = null;
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
try {
scanResult = awsConnection.getMapper().scan(TestType.class, scanExpression);
} catch (Exception e) {
AWSHelper.logError(e);
}
return scanResult;
}
我使用AWSRepository,它位于 repo 包中。它充滿了 AWS DynamoDB 呼叫(在后臺完成),課程開始如下:
public class AWSRepository implements IAWSDAO
{
private static AWSRepository instance;
static AWSConnection awsConnection;
private AWSRepository()
{
awsConnection = new AWSConnection();
}
public static synchronized AWSRepository getInstance()
{
if(instance == null) instance = new AWSRepository();
return instance;
}
...AWS call methods...
}
然后是 AWSConnection 類:
class AWSConnection
{
private DynamoDBMapper mapper;
private AmazonDynamoDBClient ddbClient;
private static String poolId;
private static final Regions databaseRegion = Regions.EU_WEST_1;
AWSConnection()
{
Context context = AppSettings.getAppContext();
Cryptography crypto = new Cryptography();
String cryptedPoolId = "xxx";
poolId = crypto.decrypt(cryptedPoolId, TMSecurity.getCryptoParams());
this.setMapper(initializeAWSIdentityPool_DynamoDBMapper(context));
this.setDdbClient(initializeAWSIdentityPool_AmazonDynamoDBClient(context));
}
DynamoDBMapper getMapper()
{
return mapper;
}
private void setMapper(DynamoDBMapper mapper)
{
this.mapper = mapper;
}
AmazonDynamoDBClient getDdbClient()
{
return ddbClient;
}
private void setDdbClient(AmazonDynamoDBClient ddbClient)
{
this.ddbClient = ddbClient;
}
// initialize connection
private static DynamoDBMapper initializeAWSIdentityPool_DynamoDBMapper(Context context)
{
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
context,
poolId,
databaseRegion
);
AmazonDynamoDBClient ddbClient = Region.getRegion(databaseRegion)
.createClient(
AmazonDynamoDBClient.class,
credentialsProvider,
new ClientConfiguration()
);
return DynamoDBMapper.builder().dynamoDBClient(ddbClient).build();
}
private static AmazonDynamoDBClient initializeAWSIdentityPool_AmazonDynamoDBClient(Context context)
{
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
context,
poolId,
databaseRegion
);
return Region.getRegion(databaseRegion)
.createClient(
AmazonDynamoDBClient.class,
credentialsProvider,
new ClientConfiguration()
);
}
}
build.gradle
...
api group: 'com.amazonaws', name: 'aws-android-sdk-cognito', version: '2.20.1'
api group: 'com.amazonaws', name: 'aws-android-sdk-core', version: '2.33.0'
api group: 'com.amazonaws', name: 'aws-android-sdk-ddb', version: '2.33.0'
api group: 'com.amazonaws', name: 'aws-android-sdk-ddb-mapper', version: '2.33.0'
...
這作業得很好,但我聽說每當您需要進行此類呼叫時,使用 REST 客戶端(例如 Retrofit)是一個很好的做法,我正在嘗試將所有可能的最佳實踐包含在我的應用程式中,所以我對我的案子有疑問。
我感到困惑是因為我問過 AWS 技術人員,他們告訴我我可以像現在一樣離開它,沒有休息客戶端完全沒有問題,AWS 已經準備好以這種方式作業(沒有休息客戶端) 所以我的問題是下一個:
Should I really include a REST client like Retrofit?
What would be the real benefits to invest the necessary time?
Could I leave it just like it is now or am I "violating" any best practice this way with AWS?
uj5u.com熱心網友回復:
我真的應該包括像 Retrofit 這樣的 REST 客戶端嗎?
否,因為您使用的是適用于 Android 的 AWS 開發工具包。
當您有可用的開發工具包時,使用 REST 客戶端呼叫 AWS REST API 實際上是一種倒退。
除非沒有適合您的目標語言的 SDK,否則使用官方 Amazon SDK 始終是最佳選擇(定期更新、檔案、本機集成等)。
根據您的用例,SDK的GitHub 自述檔案可能會建議對新專案使用Amplify 框架。
對于新專案,我們建議使用 Amplify 框架與 AWS 互動。
這取決于您的專案,但也許使用Amplify DataStore可能會更好,例如它具有非常酷的功能,例如實時更新、離線資料訪問等。
我會按原樣保留您的專案并查看 Amplify,看看您是否喜歡它為遷移或新應用程式功能提供的任何廣泛功能。
如果沒有,那么您已經通過使用 SDK 遵循了最佳實踐。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319049.html
標籤:java amazon-web-services amazon-dynamodb retrofit
上一篇:改變所選專案的顏色-離子選擇
