Giter Club home page Giter Club logo

cosmos-db-sql-api-repository's Introduction

Azure Cosmos DB Sql Api Repository

A generic implementation of Repository Pattern in C# to Cosmos DB SQL API

Build status

Introduction

This project implements and demonstrates how to use the repository pattern with Azure Cosmos DB SQL API.

Repository Pattern

Martin Fowler describes in the book Patterns of Enterprise Application Architecture a repository as follows:

A repository performs the tasks of an intermediary between the domain model layers and data mapping, acting in a similar way to a set of domain objects in memory. Client objects declaratively build queries and send them to the repositories for answers. Conceptually, a repository encapsulates a set of objects stored in the database and operations that can be performed on them, providing a way that is closer to the persistence layer. Repositories, also, support the purpose of separating, clearly and in one direction, the dependency between the work domain and the data allocation or mapping.

Benefits of Repository Pattern

  1. Centralizes data, business and service logic.
  2. Unit tests independent off the database layer.
  3. Modify the data access logic or business access logic without change the repository logic.

Generic Repository Pattern

Generic repository pattern is aimed at reducing repetition replacing it with abstractions or using data normalization - Don’t repeat yourself

Do you really need it?

I've separated some articles for you to decide for yourself.

Azure Cosmos DB

Azure Cosmos DB is Microsoft's globally distributed, multi-model database service and enables you to using your favorite API among SQL, MongoDB, Cassandra, Tables, or Gremlin.

Global scale with Azure Cosmos DB

Getting Started

  1. Installation process

To install Cosmos.Db.Sql.Api.Repository, run the following command in the Package Manager Console

PM> Install-Package Cosmos.Db.Sql.Api.Repository -Version 1.0.0-preview

Cosmos.Db.Sql.Api.Repository has GenericRepository. Through CosmosClient (preview) GenericRepository class implement base CRUD methods (AddAsync, UpdateAsync, DeleteAsync, GetByIdAsync, GetAllAsyn).

  1. Software dependencies

Cosmos.Db.Sql.Api.Repository have Cosmos.Db.Sql.Api.Domain dependency. The domain layer have IGenericRepository and Entity base class.

public abstract class Entity
{
   /// <summary>
   /// Default document entity identifier
   /// </summary>
   [JsonProperty(PropertyName = "id")]
   public string Id { get; set; }
   
   /// <summary>
   /// Data time to live
   /// </summary>
   [JsonProperty(PropertyName = "ttl")]
   public int Ttl { get; set; }
   
   /// <summary>
   /// Entity
   /// </summary>
   /// <param name="generateId">Generate id</param>
   public Entity(bool generateId)
   {
   	SetDefaultTimeToLive();
   
   	if (generateId)
   		this.Id = Guid.NewGuid().ToString();
   }
   
   /// <summary>
   /// Set a default data time to live
   /// </summary>
   public virtual void SetDefaultTimeToLive()
   {
   	Ttl = -1;
   }
}

Usage

After install Cosmos.Db.Core.Repository you need:

  1. Create Entity

Entity is a Document (JSON) data. Ex:

public class Foo : Entity
{
    public Foo()
        : base(true)
    {
    }

    public string City { get; set; }
    public string Neighborhood { get; set; }
}
  1. Create Repository interface
public interface IFooRepository : IGenericRepository<Foo>
{
}
  1. Implement Repository
public class FooRepository : GenericRepository<Domain.Entities.Foo>, IFooRepository
{
    public readonly CosmosClient _cosmosClient;

    public FooRepository(CosmosClient cosmosClient) :
        base(cosmosClient)
    {
        _cosmosClient = cosmosClient;
    }

    public override string DatabaseId => "Foo";
    public override string ContainerId => "Foo";
}
  1. Configure Dependecy Injection
public void ConfigureServices(IServiceCollection services)
{
    services
        .AddScoped<IFooRepository>(x => new FooRepository(new CosmosClient(
            Configuration.GetConnectionString("DefaultConnection"))));
}

See the Foo Project sample which contains implementation. Run Foo.Ui.Api and execute postman (postman-collection.json).

License

This software is open source, licensed under the MIT License.
See LICENSE for details.

cosmos-db-sql-api-repository's People

Contributors

brunobrandes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.