-
Notifications
You must be signed in to change notification settings - Fork 873
feat: add a DynamoDBContextBuilder to construct a DynamoDBContext #3430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
|
|
||
| namespace Amazon.DynamoDBv2.DataModel | ||
| { | ||
| /// <inheritdoc cref="IDynamoDBContextBuilder" /> | ||
| #if NET8_0_OR_GREATER | ||
| [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Amazon.DynamoDBv2.Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)] | ||
| #endif | ||
| public class DynamoDBContextBuilder : IDynamoDBContextBuilder | ||
| { | ||
| /// <summary> | ||
| /// The <see cref="DynamoDBContextConfig"/> object that is built and then supplied to the returned <see cref="DynamoDBContext"/> | ||
| /// </summary> | ||
| private DynamoDBContextConfig _config; | ||
|
|
||
| /// <summary> | ||
| /// A factory method for creating a <see cref="IAmazonDynamoDB"/> client | ||
| /// </summary> | ||
| private Func<IAmazonDynamoDB> _clientFactory; | ||
|
|
||
| /// <summary> | ||
| /// Creates a builder object to construct a <see cref="DynamoDBContext"/> | ||
| /// </summary> | ||
| public DynamoDBContextBuilder() | ||
| { | ||
| _config = new DynamoDBContextConfig(); | ||
| _config.DisableFetchingTableMetadata = true; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IDynamoDBContextBuilder ConfigureContext(Action<DynamoDBContextConfig> configure) | ||
| { | ||
| configure(_config); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IDynamoDBContextBuilder WithDynamoDBClient(Func<IAmazonDynamoDB> factory) | ||
| { | ||
| _clientFactory = factory; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public DynamoDBContext Build() | ||
| { | ||
| IAmazonDynamoDB client; | ||
| bool ownClient; | ||
|
|
||
| if (_clientFactory is null) | ||
| { | ||
| client = new AmazonDynamoDBClient(); | ||
| ownClient = true; | ||
| } | ||
| else | ||
| { | ||
| client = _clientFactory.Invoke(); | ||
| ownClient = false; | ||
| } | ||
|
|
||
| return new DynamoDBContext(client, ownClient, _config); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
sdk/src/Services/DynamoDBv2/Custom/DataModel/IDynamoDBContextBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
|
|
||
| namespace Amazon.DynamoDBv2.DataModel | ||
| { | ||
| /// <summary> | ||
| /// Interface for a builder that constructs a <see cref="DynamoDBContext"/> | ||
| /// Using <see cref="IDynamoDBContextBuilder"/> to construct a <see cref="DynamoDBContext"/> will implicitly set | ||
| /// <see cref="DynamoDBContextConfig.DisableFetchingTableMetadata"/> to true which avoids the DescribeTable call | ||
| /// and relies entirely on the DynamoDB attributes set on the .NET classes. Alternatively, you can register the | ||
| /// table definition using <see cref="DynamoDBContext.RegisterTableDefinition(DocumentModel.Table)"/>. | ||
| /// If needed, you can revert back to the previous behavior by setting <see cref="DynamoDBContextConfig.DisableFetchingTableMetadata"/> | ||
| /// to false using <see cref="IDynamoDBContextBuilder.ConfigureContext(Action{DynamoDBContextConfig})"/> as such: | ||
| /// <code> | ||
| /// var context = new DynamoDBContextBuilder() | ||
| /// .ConfigureContext(x => | ||
| /// { | ||
| /// x.DisableFetchingTableMetadata = false; | ||
| /// }) | ||
| /// .Build(); | ||
| /// </code> | ||
| /// </summary> | ||
| public interface IDynamoDBContextBuilder | ||
| { | ||
| /// <summary> | ||
| /// Supplies a factory method for creating a <see cref="IAmazonDynamoDB"/> client. | ||
| /// If a factory method is not provided, a new <see cref="IAmazonDynamoDB"/> client | ||
| /// will be created using the environment to search for credentials and region configuration. | ||
| /// </summary> | ||
| /// <param name="factory">Factory method for creating a <see cref="IAmazonDynamoDB"/> client</param> | ||
| IDynamoDBContextBuilder WithDynamoDBClient(Func<IAmazonDynamoDB> factory); | ||
|
|
||
| /// <summary> | ||
| /// Configures the <see cref="DynamoDBContext"/> that is being constructed | ||
| /// </summary> | ||
| /// <param name="configure">The configuration applied to the constructed <see cref="DynamoDBContext"/></param> | ||
| IDynamoDBContextBuilder ConfigureContext(Action<DynamoDBContextConfig> configure); | ||
|
|
||
| /// <summary> | ||
| /// Call at the end to retrieve the new <see cref="DynamoDBContext"/> | ||
| /// </summary> | ||
| /// <returns>Built <see cref="DynamoDBContext"/></returns> | ||
| DynamoDBContext Build(); | ||
normj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add would suggest giving more context for users upgrading about how this newer approach disables the
DescribeTablemechanism. And explain how the can use theConfigureContextmethod to revert to pervious behavior if they need it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more docs in the interface and inheritor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a line that says alternatively to attributes on the class you can register the table definition using the
RegisterTableDefinitionmethod on the context object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done