Skip to content

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

License

Notifications You must be signed in to change notification settings

brunobrandes/cosmos-db-sql-api-repository

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages