Created
September 30, 2020 00:10
-
-
Save oguzhancagliyan/ac2a2446c0e0c82d46f418d528ca2bac to your computer and use it in GitHub Desktop.
This file contains 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
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