Skip to content

Instantly share code, notes, and snippets.

@oguzhancagliyan
Created September 30, 2020 00:10
Show Gist options
  • Save oguzhancagliyan/ac2a2446c0e0c82d46f418d528ca2bac to your computer and use it in GitHub Desktop.
Save oguzhancagliyan/ac2a2446c0e0c82d46f418d528ca2bac to your computer and use it in GitHub Desktop.
public class DynamoDbSeeder
{
public static void Seed(AmazonDynamoDBClient client)
{
var installers = typeof(DynamoDbMovieSeeder).Assembly.ExportedTypes
.Where(m => typeof(IDynamoDbSeeder).IsAssignableFrom(m) && !m.IsInterface && !m.IsAbstract)
.Select(Activator.CreateInstance)
.Cast<IDynamoDbSeeder>()
.ToList();
installers.ForEach(m => m.Seed(client));
}
public static void CreateTable(AmazonDynamoDBClient client)
{
var installers = typeof(DynamoDbMovieSeeder).Assembly.ExportedTypes
.Where(m => typeof(IDynamoDbSeeder).IsAssignableFrom(m) && !m.IsInterface && !m.IsAbstract)
.Select(Activator.CreateInstance)
.Cast<IDynamoDbSeeder>()
.ToList();
installers.ForEach(m => m.CreateTable(client));
}
public static void Add<TModel>(AmazonDynamoDBClient client, TModel model)
{
DynamoDBContext context = new DynamoDBContext(client);
var table = context.GetTargetTable<TModel>();
JsonSerializerOptions options = new JsonSerializerOptions
{
IgnoreNullValues = true
};
var modelJson = JsonSerializer.Serialize(model, options);
Document item = Document.FromJson(modelJson);
table.PutItemAsync(item).GetAwaiter().GetResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment