Skip to content

Instantly share code, notes, and snippets.

@oguzhancagliyan
Created September 30, 2020 00:19
Show Gist options
  • Save oguzhancagliyan/0133066d1cc1d77cbd1528851f9b90d9 to your computer and use it in GitHub Desktop.
Save oguzhancagliyan/0133066d1cc1d77cbd1528851f9b90d9 to your computer and use it in GitHub Desktop.
public interface IRepository<TModel>
{
Task<Document> AddAsync(TModel model, CancellationToken token = default);
}
public abstract class Repository<TModel> : IRepository<TModel>
{
protected readonly IDynamoDBContext _context;
protected readonly Table _table;
protected Repository(IDynamoDBContext context)
{
_context = context;
_table = _context.GetTargetTable<TModel>();
}
public Task<Document> AddAsync(TModel model, CancellationToken token = default)
{
JsonSerializerOptions options = new JsonSerializerOptions
{
IgnoreNullValues = true
};
var modelJson = JsonSerializer.Serialize(model, options);
var item = Document.FromJson(modelJson);
var task = _table.PutItemAsync(item, token);
return task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment