public void SetUp()
    {
        var credentialsOptions  = new CredentialsOptions();
        var credentialsProvider = new CredentialsProvider(credentialsOptions);
        var options             = new DynamoDbOptions <DynamoDbContextTests>();

        _context = new DynamoDbContext <DynamoDbContextTests>(credentialsProvider, options);
    }
Example #2
0
        public static IServiceCollection AddDynamoDb <T>(
            this IServiceCollection services,
            DynamoDbOptions <T> configuration
            )
        {
            services.TryAddSingleton(configuration);
            services.TryAddSingleton <DynamoDbContext <T> >();

            return(services);
        }
        /// <summary>
        /// Adds a default instance for Amazon.DynamoDBv2.DataModel.IDynamoDBContext with specified options.
        /// </summary>
        /// <param name="services">The service collection, that will service provider be built from.</param>
        /// <param name="dynamoDbOptions">
        /// MSToolKit.Core.Wrappers.DynamoDb.DynamoDbOptions, that configure the default instance for IDynanoDbContext.
        /// </param>
        /// <returns>
        /// The same instane of Microsoft.Extensions.DependencyInjection.IServiceCollection with added DynamoDbContext services.
        /// </returns>
        public static IServiceCollection AddDynamoDbContext(
            this IServiceCollection services, Action <DynamoDbOptions> dynamoDbOptions = null)
        {
            var options = new DynamoDbOptions();

            dynamoDbOptions?.Invoke(options);

            if (options.LocalMode == true)
            {
                var clientConfig = new AmazonDynamoDBConfig
                {
                    ServiceURL = options.ServiceUrl ?? null
                };

                if (clientConfig.ServiceURL == null)
                {
                    clientConfig.RegionEndpoint = options.RegionEndpoint;
                }

                services.AddTransient <IAmazonDynamoDB>(sp =>
                {
                    if (options.AccessKeyId != null && options.SecretAccessKey != null)
                    {
                        return(new AmazonDynamoDBClient(
                                   options.AccessKeyId,
                                   options.SecretAccessKey,
                                   clientConfig));
                    }

                    return(new AmazonDynamoDBClient(clientConfig));
                });
            }
            else
            {
                services.AddAWSService <IAmazonDynamoDB>();
            }

            services.AddTransient <IDynamoDBContext>(
                sp => new DynamoDBContext(sp.GetService <IAmazonDynamoDB>()));

            return(services);
        }
Example #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddMvc();
            services.AddSingleton <IConfiguration>(Configuration);

            services.Configure <Dictionary <string, string> >(op => Configuration.GetSection("AppSettings")?.Bind(op));


            // AWS Options
            var awsOptions = Configuration.GetAWSOptions();

            services.AddDefaultAWSOptions(awsOptions);

            var client          = awsOptions.CreateServiceClient <IAmazonDynamoDB>();
            var dynamoDbOptions = new DynamoDbOptions();

            ConfigurationBinder.Bind(Configuration.GetSection("DynamoDbTables"), dynamoDbOptions);

            // This is where the magic happens
            services.AddScoped <IDynamoDbManager <MyModel> >(provider => new DynamoDbManager <MyModel>(client, dynamoDbOptions.MyModel));
        }
        /// <summary>
        /// The load.
        /// </summary>
        public override void Load()
        {
            var options = new DynamoDbOptions();

            this.OptionsAction(options);

            var awsRegion = RegionEndpoint.GetBySystemName(options.AwsRegion);
            var client    = new AmazonDynamoDBClient(awsRegion);
            var table     = Table.LoadTable(client, options.TableName);

            var item = table.GetItemAsync(options.Id).GetAwaiter().GetResult();

            var config     = item.ToJson();
            var jsonObject = JObject.Parse(config);
            var jTokens    = jsonObject.Descendants().Where(p => p.Count() == 0);
            var results    = jTokens.Aggregate(new Dictionary <string, string>(), (properties, jToken) =>
            {
                properties.Add(jToken.Path.Replace(".", ":").Replace("[", ":").Replace("]", string.Empty), jToken.ToString());
                return(properties);
            });

            this.Data = results;
        }