Giter VIP home page Giter VIP logo

documentdbrepository's Introduction

DocumentDbRepository

NuGet version

DocumentDbRepository is a dependency injection friendly repository pattern for Azure DocumentDb. It simplifies the communication with Azure DocumentDb by providing an additional layer on top of Azure DocumentDb Client.

Usage

The most simplified scenario

Create a repository that stores documents of type MyClass. The type has to inherit from type Resource from Azure DocumentDb Client.

public class MyClass : Microsoft.Azure.Documents.Resource
{
    public string Name { get; set; }
    public int Number { get; set; }
}
var documentClient = new Microsoft.Azure.Documents.Client.DocumentClient(...);
var repository = new Repository<MyClass>(documentClient, "myDatabase");

And then simply use the repository

repository.Create(new MyClass
{
    Name = "James",
    Number = 7
});

MyClass james = await repository.Get(mc => mc.Number = 7);

james.Name = "James Bond";

await repository.Update(james);

await repository.Delete(james);

The repository takes care of creating the database and the collection for you using the built-in BasicDatabaseProvider and GenericCollectionProvider.

BasicDatabaseProvider is a simple implementation of IDatabaseProvider that takes the database id from its constructor (in this case "myDatabase") and ensures that the database exists when we want to use it.

GenericCollectionProvider is a simple implementation of ICollectionProvider that handles collections based on the generic type name given to the provider.

Advanced usage

You can create your own ICollectionProvider (and IDatabaseProvider) to customise how the database is created and how the collections are created. In that case, use the other repository constructor, e.g.:

public class MyCollectionProvider : ICollectionProvider
{
    // some implementation
}

public class MyDatabaseProvider : IDatabaseProvider
{
    // some implementation
}

var documentClient = new Microsoft.Azure.Documents.Client.DocumentClient(...);
var repository = new Repository<MyClass>(
    documentClient,
    new MyCollectionProvider(new MyDatabaseProvider()));

Obviously, it is possible to implement ICollectionProvider only.

Usage with Ninject in an Asp.Net MVC app

Somewhere in the galaxy...excuse me, somewhere in the ninject configuration:

kernel.Bind<Microsoft.Azure.Documents.Client.DocumentClient>()
    .ToSelf()
    .InRequestScope()
    .WithConstructorArgument(new Uri(...))
    .WithConstructorArgument("auth key")
    .WithConstructorArgument<ConnectionPolicy>(null)
    .WithConstructorArgument<ConsistencyLevel?>(null);

kernel.Bind<IDbProvider>()
    .To<DbProvider>()
    .InRequestScope()
    .WithConstructorArgument("myDatabase");

kernel.Bind(typeof(Repository<>))
    .ToSelf()
    .InRequestScope();

In a controller:

public class MyController : System.Web.Mvc.Controller
{
    private readonly Repository<MyClass> repository;

    public MyController(Repository<MyClass> repository)
    {
        this.repository = repository;
    }

    public async Task<ActionResult> Index()
    {
        return this.View((await this.repository.GetAll()).ToList());
    }
}

documentdbrepository's People

Contributors

san7hos avatar ronpack 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.