Git Product home page Git Product logo

dynamodb-baserepository's Introduction

DynamoDB base repository

.NET Core Github Actions badge Coverage Status

C# .NET Core implementation of the repository pattern using DynamoDB as data store using single table and hierarchical data modelling approach overloading the partition and sort key as well secondary index.

This implementation aims to solve the most common data persistence use cases ranging from independent entities to more complex data models.

Key features:

  • Ready to use CRUD operations.
  • Ready to use batch operations.
  • Generic design for flexibility of data types.
  • One to many relationship.
  • Many to many relationships

Content

Quick usage guide

Data model assumptions

Usage - Independent entities

Usage - One to many

Usage - Many to many

Example: CRUD operations

Example: Batch operations

Quick usage guide

  1. Define your entity as a POCO, no interface or annotations required.
    public class Person
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Email { get; set; }

        public int Age { get; set; }
    }
  1. Inherit from SimpleRepository<TKey, TEntity> abstract class.
    public class PersonRepository : SimpleRepository<int, Person>
    {
    }
  1. Define partition key prefix and sort key prefix in constructor.
    public PersonRepository(string tableName, string serviceUrl = null) : base(tableName, serviceUrl)
    {
        PKPrefix = "PERSON";
        SKPrefix = "METADATA";
    }
  1. Override TKey GetEntityKey(TEntity item) abstract method to return the entity identifier.
    protected override int GetEntityKey(Person item)
    {
        return item.Id;
    }
  1. Override DynamoDBItem ToDynamoDb(TEntity item) abstract method to map the entity object to a DynamoDB attribute dictionary.
     protected override DynamoDBItem ToDynamoDb(Person item)
    {
        var dbItem = new DynamoDBItem();
        dbItem.AddNumber("Id", item.Id);
        dbItem.AddString("Name", item.Name);
        dbItem.AddString("Email", item.Email);
        dbItem.AddNumber("Age", item.Age);
        return dbItem;
    }
  1. Override TEntity FromDynamoDb(DynamoDBItem item) abstract method to map the DynamoDB attribute dictionary to an entity object.
    protected override Person FromDynamoDb(DynamoDBItem item)
    {
        var result = new Person();
        result.Id = item.GetInt32("Id");
        result.Name = item.GetString("Name");
        result.Email = item.GetString("Email");
        result.Age = item.GetInt32("Age");
        return result;
    }
  1. Use the available methods either directly or through the interface.
    public static async Task TestCRUD_PersonRepository()
    {
        // Create a new PersonRepository
        ISimpleRepository<int, Person> repo = new PersonRepository(_tableName);

        // Prepare a Person instance
        var p1 = new Person
        {
            Id = 1,
            Name = "personA",
            Email = "[email protected]",
            Age = 35
        };

        Console.WriteLine("* Adding Person 1");
        // Add a new person
        await repo.Add(p1);

        Console.WriteLine("* Getting the list");
        // Get the full list
        var list = await repo.GetList();
        foreach (var item in list)
        {
            Console.WriteLine(JsonSerializer.Serialize(item));
        }

        Console.ReadKey();

        Console.WriteLine("* Getting Person 1");
        // Get an individual Person by its Id
        var found1 = await repo.Get(p1.Id);
        Console.WriteLine(JsonSerializer.Serialize(found1));

        Console.WriteLine("* Deleting Person 1");
        // Delete an individual Person by its Id
        await repo.Delete(p1.Id);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.