Giter VIP home page Giter VIP logo

cosmosdb-repo's Introduction

Simple repository pattern for Cosmos DB

Nuget

Installation

Use NuGet to install the package.

PM> Install-Package DocumentDB.Repository

Getting started

Step 1: Get DocumentDB client

Before you can play with your DocumentDB database you need to get the DocumentDB Client by passing your endopointUrl and authorizationKey (primary).

internal class Program
{
    public static IReliableReadWriteDocumentClient Client { get; set; }

	private static void Main(string[] args)
	{
		IDocumentDbInitializer init = new DocumentDbInitializer();

		string endpointUrl = ConfigurationManager.AppSettings["azure.documentdb.endpointUrl"];
		string authorizationKey = ConfigurationManager.AppSettings["azure.documentdb.authorizationKey"];

		// get the Azure DocumentDB client
		Client = init.GetClient(endpointUrl, authorizationKey);

		// Run demo
		Task t = MainAsync(args);
		t.Wait();
	}
}    

Step 2: Create Repository and use it with your POCO objects

With the client in place create a repository providing database id (will be created if it doesn't exist). Now it's really easy to do the CRUD operations:

private static async Task MainAsync(string[] args)
{
	string databaseId = ConfigurationManager.AppSettings["azure.documentdb.databaseId"];

	// create repository for persons
	DocumentDbRepository<Person> repo = new DocumentDbRepository<Person>(Client, databaseId);

	// create a new person
	Person matt = new Person
	{
		FirstName = "m4tt",
		LastName = "TBA",
		BirthDayDateTime = new DateTime(1990, 10, 10),
		PhoneNumbers =
			new Collection<PhoneNumber>
			{
				new PhoneNumber {Number = "555", Type = "Mobile"},
				new PhoneNumber {Number = "777", Type = "Landline"}
			}
	};

	// add person to database's collection (if collection doesn't exist it will be created and named as class name -it's a convenction, that can be configured during initialization of the repository)
	matt = await repo.AddOrUpdateAsync(matt);

	// create another person
	Person jack = new Person
	{
		FirstName = "Jack",
		LastName = "Smith",
		BirthDayDateTime = new DateTime(1990, 10, 10),
		PhoneNumbers = new Collection<PhoneNumber>()
	};

	// add jack to collection
	jack = await repo.AddOrUpdateAsync(jack);

	// update first name
	matt.FirstName = "Matt";

	// add last name
	matt.LastName = "Smith";

	// remove landline phone number
	matt.PhoneNumbers.RemoveAt(1);

	// should update person
	await repo.AddOrUpdateAsync(matt);

	// get Matt by his Id
	Person justMatt = await repo.GetByIdAsync(matt.Id);

	// ... or by his first name
	Person firstMatt = await repo.FirstOrDefaultAsync(p => p.FirstName.Equals("matt", StringComparison.OrdinalIgnoreCase));

	// query all the smiths
	var smiths = (await repo.WhereAsync(p => p.LastName.Equals("Smith", StringComparison.OrdinalIgnoreCase))).ToList();
	
	// count all persons
        var personsCount = await repo.CountAsync();

        // count all jacks
        var jacksCount = await repo.CountAsync(p => p.FirstName == "Jack");
	
	// remove matt from collection
	await repo.RemoveAsync(matt.Id);

	// remove jack from collection
	await repo.RemoveAsync(jack.Id);
}

Full example can be found here.

License

cosmosdb-repo is provided under the MIT license.

cosmosdb-repo's People

Contributors

aarondcoleman avatar powelgithub avatar spoofi avatar

Watchers

 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.