Introduction
Cosmos DB is one of the most demanding No SQL databases in recent days. This is a multi-model (Currently available with SQL, Mongo DB, Cassandra, Table, and Graph APIs) and horizontally scalable database. You can enable geo-replication in Cosmos DB which will reduce the latency between different regions. You can refer this document to get more details about Cosmos DB.
Install Cosmos DB Emulator and create database locally


You can give a name to your database and collection id. You must give a Partition key. A Partition key is very important in Cosmos DB in order to store the data.
Create Angular Application in Visual Studio with ASP.NET Core



Create Cosmos DB API Service with Employees Controller
- using Newtonsoft.Json;
- namespace AngularCoreCosmos.Models
- {
- public class Employee
- {
- [JsonProperty(PropertyName = "id")]
- public string Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Gender { get; set; }
- public string Company { get; set; }
- public string Designation { get; set; }
- public string Cityname { get; set; }
- }
- }
I have added all the field names required for our Cosmos DB collection. Also note that I have added a “JsonProperty” attribute for “Id” property. Because, Cosmos DB automatically creates an “id” field for each record.

We can create a “Data” folder and create an “IDocumentDBRepository” interface inside it. This interface contains all the method names for our Cosmos DB repository. We will implement this interface in the “DocumentDBRepository” class.
- using Microsoft.Azure.Documents;
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- namespace AngularCoreCosmos.Data
- {
- public interface IDocumentDBRepository<T> where T : class
- {
- Task<Document> CreateItemAsync(T item, string collectionId);
- Task DeleteItemAsync(string id, string collectionId, string partitionKey);
- Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, string collectionId);
- Task<IEnumerable<T>> GetItemsAsync(string collectionId);
- Task<Document> UpdateItemAsync(string id, T item, string collectionId);
- }
- }
I have added all the methods declaration for CRUD actions for Web API controller in the above interface.
- using Microsoft.Azure.Documents;
- using Microsoft.Azure.Documents.Client;
- using Microsoft.Azure.Documents.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- namespace AngularCoreCosmos.Data
- {
- public class DocumentDBRepository<T> : IDocumentDBRepository<T> where T : class
- {
- private readonly string Endpoint = "https://localhost:8081/";
- private readonly string Key = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
- private readonly string DatabaseId = "SarathCosmosDB";
- private DocumentClient client;
- public DocumentDBRepository()
- {
- client = new DocumentClient(new Uri(Endpoint), Key);
- }
- public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, string collectionId)
- {
- IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
- UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId),
- new FeedOptions { MaxItemCount = -1 })
- .Where(predicate)
- .AsDocumentQuery();
- List<T> results = new List<T>();
- while (query.HasMoreResults)
- {
- results.AddRange(await query.ExecuteNextAsync<T>());
- }
- return results;
- }
- public async Task<IEnumerable<T>> GetItemsAsync(string collectionId)
- {
- IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
- UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId),
- new FeedOptions { MaxItemCount = -1 })
- .AsDocumentQuery();
- List<T> results = new List<T>();
- while (query.HasMoreResults)
- {
- results.AddRange(await query.ExecuteNextAsync<T>());
- }
- return results;
- }
- public async Task<Document> CreateItemAsync(T item, string collectionId)
- {
- return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId), item);
- }
- public async Task<Document> UpdateItemAsync(string id, T item, string collectionId)
- {
- return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, collectionId, id), item);
- }
- public async Task DeleteItemAsync(string id, string collectionId, string partitionKey)
- {
- await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, collectionId, id),
- new RequestOptions() { PartitionKey = new PartitionKey(partitionKey) });
- }
- private async Task CreateDatabaseIfNotExistsAsync()
- {
- try
- {
- await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
- }
- catch (DocumentClientException e)
- {
- if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
- }
- else
- {
- throw;
- }
- }
- }
- private async Task CreateCollectionIfNotExistsAsync(string collectionId)
- {
- try
- {
- await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId));
- }
- catch (DocumentClientException e)
- {
- if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- await client.CreateDocumentCollectionAsync(
- UriFactory.CreateDatabaseUri(DatabaseId),
- new DocumentCollection { Id = collectionId },
- new RequestOptions { OfferThroughput = 1000 });
- }
- else
- {
- throw;
- }
- }
- }
- }
- }
I have implemented all the CRUD actions inside the above class. We can use these methods in our Web API controller.
- using AngularCoreCosmos.Data;
- using AngularCoreCosmos.Models;
- using Microsoft.AspNetCore.Mvc;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace AngularCoreCosmos.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class EmployeesController : ControllerBase
- {
- private readonly IDocumentDBRepository<Employee> Respository;
- private readonly string CollectionId;
- public EmployeesController(IDocumentDBRepository<Employee> Respository)
- {
- this.Respository = Respository;
- CollectionId = "Employee";
- }
- [HttpGet]
- public async Task<IEnumerable<Employee>> Get()
- {
- return await Respository.GetItemsAsync(CollectionId);
- }
- [HttpGet("{id}/{cityname}")]
- public async Task<Employee> Get(string id, string cityname)
- {
- var employees = await Respository.GetItemsAsync(d => d.Id == id && d.Cityname == cityname, CollectionId);
- Employee employee = new Employee();
- foreach (var emp in employees)
- {
- employee = emp;
- break;
- }
- return employee;
- }
- [HttpPost]
- public async Task<bool> Post([FromBody]Employee employee)
- {
- try
- {
- if (ModelState.IsValid)
- {
- employee.Id = null;
- await Respository.CreateItemAsync(employee, CollectionId);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- [HttpPut]
- public async Task<bool> Put([FromBody]Employee employee)
- {
- try
- {
- if (ModelState.IsValid)
- {
- await Respository.UpdateItemAsync(employee.Id, employee, CollectionId);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- [HttpDelete("{id}/{cityname}")]
- public async Task<bool> Delete(string id, string cityname)
- {
- try
- {
- await Respository.DeleteItemAsync(id, CollectionId, cityname);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- }
I have implemented all the action methods inside this controller class using DocumentDBRepository class.

We have successfully created our Web API service. If needed, you can check the API with Postman or any other tool.
Add Employee components in Angular
- <div style="text-align:center;">
- <h1>Angular App with ASP.NET Core and Cosmos DB</h1>
- <p>Welcome to our new single-page application, built with below technologies:</p>
- <img src="../../assets/angular-asp-core-cosmos.png" style="width:700px;" />
- </div>
I have added a beautiful image as background in this file.
- <header>
- <nav class='navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3'>
- <div class="container">
- <a class="navbar-brand" [routerLink]='["/"]'>Employee App</a>
- <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-label="Toggle navigation"
- [attr.aria-expanded]="isExpanded" (click)="toggle()">
- <span class="navbar-toggler-icon"></span>
- </button>
- <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse" [ngClass]='{"show": isExpanded}'>
- <ul class="navbar-nav flex-grow">
- <li class="nav-item" [routerLinkActive]='["link-active"]' [routerLinkActiveOptions]='{ exact: true }'>
- <a class="nav-link text-dark" [routerLink]='["/"]'>Home</a>
- </li>
- <li class="nav-item" [routerLinkActive]='["link-active"]'>
- <a class="nav-link text-dark" [routerLink]='["/employees"]'>Employees</a>
- </li>
- </ul>
- </div>
- </div>
- </nav>
- </header>
- <footer>
- <nav class="navbar navbar-light bg-white mt-5 fixed-bottom">
- <div class="navbar-expand m-auto navbar-text">
- Developed with <i class="fa fa-heart"></i> by <a href="https://codewithsarath.com" target="_blank"><b>Sarathlal Saseendran</b></a>
- </div>
- </nav>
- </footer>
This is the navigation part of the application. I have added an Employee menu in this file.
- import { FormGroup } from '@angular/forms';
- export class GenericValidator {
- constructor(private validationMessages: { [key: string]: { [key: string]: string } }) {
- }
- processMessages(container: FormGroup): { [key: string]: string } {
- const messages = {};
- for (const controlKey in container.controls) {
- if (container.controls.hasOwnProperty(controlKey)) {
- const c = container.controls[controlKey];
- if (c instanceof FormGroup) {
- const childMessages = this.processMessages(c);
- Object.assign(messages, childMessages);
- } else {
- if (this.validationMessages[controlKey]) {
- messages[controlKey] = '';
- if ((c.dirty || c.touched) && c.errors) {
- Object.keys(c.errors).map(messageKey => {
- if (this.validationMessages[controlKey][messageKey]) {
- messages[controlKey] += this.validationMessages[controlKey][messageKey] + ' ';
- }
- });
- }
- }
- }
- }
- }
- return messages;
- }
- }
We can create an “employee” model inside “data-models” and define all the employee properties inside this class. We are creating all employee related components and service inside the “employees” folder.
- export interface Employee {
- id: string,
- name: string,
- address: string,
- gender: string,
- company: string,
- designation: string,
- cityname: string
- }
We can create an employee service inside the services folder. We will write all the methods for CRUD operations inside this service. The methods in this service will be invoked from various components later.
- import { Injectable, Inject } from '@angular/core';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import { Observable, throwError, of } from 'rxjs';
- import { catchError, map } from 'rxjs/operators';
- import { Employee } from '../data-models/employee';
- @Injectable()
- export class EmployeeService {
- private employeesUrl = this.baseUrl + 'api/employees';
- constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { }
- getEmployees(): Observable<Employee[]> {
- return this.http.get<Employee[]>(this.employeesUrl)
- .pipe(
- catchError(this.handleError)
- );
- }
- getEmployee(id: string, cityName: string): Observable<Employee> {
- if (id === '') {
- return of(this.initializeEmployee());
- }
- const url = `${this.employeesUrl}/${id}/${cityName}`;
- return this.http.get<Employee>(url)
- .pipe(
- catchError(this.handleError)
- );
- }
- createEmployee(employee: Employee): Observable<Employee> {
- const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
- return this.http.post<Employee>(this.employeesUrl, employee, { headers: headers })
- .pipe(
- catchError(this.handleError)
- );
- }
- deleteEmployee(id: string, cityname: string): Observable<{}> {
- const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
- const url = `${this.employeesUrl}/${id}/${cityname}`;
- return this.http.delete<Employee>(url, { headers: headers })
- .pipe(
- catchError(this.handleError)
- );
- }
- updateEmployee(employee: Employee): Observable<Employee> {
- const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
- const url = this.employeesUrl;
- return this.http.put<Employee>(url, employee, { headers: headers })
- .pipe(
- map(() => employee),
- catchError(this.handleError)
- );
- }
- private handleError(err) {
- let errorMessage: string;
- if (err.error instanceof ErrorEvent) {
- errorMessage = `An error occurred: ${err.error.message}`;
- } else {
- errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
- }
- console.error(err);
- return throwError(errorMessage);
- }
- private initializeEmployee(): Employee {
- return {
- id: null,
- name: null,
- address: null,
- gender: null,
- company: null,
- designation: null,
- cityname: null
- };
- }
- }
We can create employee list component to list all entire employee data in a grid.
- import { Component, OnInit } from '@angular/core';
- import { Employee } from '../data-models/employee';
- import { EmployeeService } from '../services/employee-service';
- @Component({
- selector: 'app-employee-list',
- templateUrl: './employee-list.component.html',
- styleUrls: ['./employee-list.component.css']
- })
- export class EmployeeListComponent implements OnInit {
- pageTitle = 'Employee List';
- filteredEmployees: Employee[] = [];
- employees: Employee[] = [];
- errorMessage = '';
- _listFilter = '';
- get listFilter(): string {
- return this._listFilter;
- }
- set listFilter(value: string) {
- this._listFilter = value;
- this.filteredEmployees = this.listFilter ? this.performFilter(this.listFilter) : this.employees;
- }
- constructor(private employeeService: EmployeeService) { }
- performFilter(filterBy: string): Employee[] {
- filterBy = filterBy.toLocaleLowerCase();
- return this.employees.filter((employee: Employee) =>
- employee.name.toLocaleLowerCase().indexOf(filterBy) !== -1);
- }
- ngOnInit(): void {
- this.employeeService.getEmployees().subscribe(
- employees => {
- this.employees = employees;
- this.filteredEmployees = this.employees;
- },
- error => this.errorMessage = <any>error
- );
- }
- deleteEmployee(id: string, name: string, cityname: string): void {
- if (id === '') {
- this.onSaveComplete();
- } else {
- if (confirm(`Are you sure want to delete this Employee: ${name}?`)) {
- this.employeeService.deleteEmployee(id, cityname)
- .subscribe(
- () => this.onSaveComplete(),
- (error: any) => this.errorMessage = <any>error
- );
- }
- }
- }
- onSaveComplete(): void {
- this.employeeService.getEmployees().subscribe(
- employees => {
- this.employees = employees;
- this.filteredEmployees = this.employees;
- },
- error => this.errorMessage = <any>error
- );
- }
- }








Srikanth TirunahariPosted Aug 25, 2020, 7:37 AM
What I was about to search for. well detailed written, very good to see this kind of articles , i need on azure devops to push the the data
Mithilesh KumarPosted Jun 21, 2019, 1:01 AM
Really depth and easy..
Sundaram SubramanianPosted May 20, 2019, 9:51 AM
What I was about to search for. well detailed written
Ano MepaniPosted May 20, 2019, 4:14 AM
Very good artice
Jamil MoughalPosted May 20, 2019, 12:32 AM
That's exactly what I was looking for, thanks for sharing Sarathlal Saseendran, a valuable read
Humayun Kabir MamunPosted May 19, 2019, 1:08 AM
Thanks, very good article...