Giter Club home page Giter Club logo

cosmosdbexamplewithcqrs's Introduction

Cosmos DB with CQRS pattern example

This repository is an example of Microsoft Cosmos DB (NoSQL database). The presented implementation uses the CQRS pattern (no event sourcing).

Introduction

Cosmos DB available on Azure Cloud is a NoSQL database - a document-based database with key-value pairs that do not have a schema. In general, like most NoSQL offers:

  1. Scalability: Like SQL databases, NoSQL databases are easily scalable. However, they scale horizontally (not vertically like SQL), meaning that one must add more servers to existing NoSQL databases. Still, NoSQL usually scales better for more extensive and more powerful applications in any case.
  2. Community: NoSQL is relatively new compared to SQL databases meaning that sometimes there will be less documented support for utilizing the database. Au contraire, the popularity of NoSQL is rapidly growing in the industry.
  3. Flexibility: with a NoSQL database, there is more flexibility to store the data without a pre-defined structure, which is applicable depending on the application.

As for Cosmos DB, advantages:

  1. Performance.
  2. High Availability.
  3. Elasticity in scale.
  4. Injects unstructured data at a very high speed.
  5. No real need for administrative work; it indexes everything on its own.

Disadvantages:

  1. ANSI SQL support.
  2. Can be very expensive.
  3. No enough insights into how many resources (DTUs) an individual query uses.

More here: Introduction to Azure Cosmos DB.

Tech-Stack

  1. .NET Core 3.1.
  2. C# language.
  3. Web API.
  4. SwaggerUI.
  5. MediatR.
  6. Azure Cosmos DB.

No tests are provided this time.

ICosmosDbService

This interface provides basic CRUD functionality for given item model:

public interface ICosmosDbService
{
    Task<HttpStatusCode> CreateDatabase(string ADatabaseName, CancellationToken ACancellationToken = default);
    Task<HttpStatusCode> CreateContainer(string ADatabaseName, string AContainerName, Guid AId, CancellationToken ACancellationToken = default);
    Task<HttpStatusCode> IsItemExists<T>(Guid AId, CancellationToken ACancellationToken = default) where T : class;
    Task<T> GetItem<T>(Guid AId, CancellationToken ACancellationToken = default) where T : class;
    Task<IEnumerable<T>> GetItems<T>(string AQueryString, CancellationToken ACancellationToken = default) where T : class;
    Task<HttpStatusCode> AddItem<T>(Guid AId, T AItem, CancellationToken ACancellationToken = default);
    Task<HttpStatusCode> UpdateItem<T>(Guid AId, T AItem, CancellationToken ACancellationToken = default);
    Task<HttpStatusCode> DeleteItem<T>(Guid AId, CancellationToken ACancellationToken = default);
}

All methods are asynchronous. The usage is very straightforward:

public async Task<Unit> Handle(AddNewArticleCommand ARequest, CancellationToken ACancellationToken)
{
    var LNewGuid = Guid.NewGuid();
    
    await FCosmosDbService.AddItem(LNewGuid, new Articles
    {
        Id = ARequest.Id,
        Title = ARequest.Title,
        Desc = ARequest.Desc,
        Status = ARequest.Status,
        Likes = ARequest.Likes,
        ReadCount = ARequest.ReadCount
    }, ACancellationToken);
    
    return await Task.FromResult(Unit.Value);
}

After the call, one should check returned HttpStatusCode (not provided in the examples).

How to run example

Prerequisites

  1. Install Azure Cosmos DB Emulator.
  2. Run it; by default, it will be accessible at locahost:8081.
  3. Configure secrets.json:
{
  "CosmosDb": 
  {
    "DatabaseName": "<database_name>",
    "Account": "<uri>",
    "Key": "<primary_key>"
  }
}

From the Quickstart tab, copy:

  1. URI to Account.
  2. Primary Key to Key.

Run

In Visual Studio or JetBrains Rider, build and run. Navigate to Swagger, and have fun!

cosmosdbexamplewithcqrs's People

Contributors

tomaszkandula avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cj99

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.