ServiceStack.Azure.CosmosDb

Package Description


Keywords
ServiceStack, AutoQuery, IQueryable, Azure, CosmosDb
License
Apache-2.0
Install
Install-Package ServiceStack.Azure.CosmosDb -Version 1.0.0

Documentation

License Build status

NuGet

NuGet

NuGet

NuGet

ServiceStack.QueryableDataSource

An collection of IQueryable based DataSource libraries for the ServiceStack AutoQuery feature

Overview

This project makes it possible to expose data with the ServiceStack AutoQuery from providers that leverage the IQueryable interface.

There are several data providers that implement the IQueryable interface for querying. The most popular is Microsoft's Entity Framework. However, ServiceStack's SQL provider can already handle querying from many of the data providers that Entity Framework targets. So this library will initially focus on Document Database providers (i.e. No-SQL).

Data providers will include:

How it works

The secret sauce in making this work is the use of the System.Linq.Dynamic.Core library which dynamically transforms string based queries to LINQ expressions. This allows the library to construct the query with less regard to the strong-typed nature of C#.

Using ServiceStack.Azure.CosmosDB

Install from NuGet:

Install-Package ServiceStack.Azure.CosmosDB

Simply add the AutoQueryDataFeature in your AppHost.Configure() method:

public override void Configure(Container container)
{
    // Get Data Provider Settings from Configuration 
    var endpointUrl = AppSettings.Get<string>("CosmosDb.EndPointUrl");
    var authorizationKey = AppSettings.Get<string>("CosmosDb.AuthorizationKey");
    var databaseId = AppSettings.Get<string>("CosmosDb.DatabaseId");
    var collectionId = AppSettings.Get<string>("CosmosDb.CollectionId");

    // Create a Document Client 
    var docClient = new DocumentClient(
        new Uri(endpointUrl),
        authorizationKey);

    // Register the Document Client into the IOC
    container.Register<IDocumentClient>(c => docClient);

    var requestOptions = new RequestOptions { ConsistencyLevel = ConsistencyLevel.Session };

    // Add AuthQueryDataFeature plugin with a DataSource 
    // you will need to add a data source for each TDocument type
    Plugins.Add(new AutoQueryDataFeatureFeature()
        .AddDataSource(ctx => ctx.CosmosDBDataSource<TDocument>(docClient, databaseId, collectionId, requestOptions)));
}

Using ServiceStack.MongoDB

Install from NuGet:

Install-Package ServiceStack.MongoDB

Simply add the AutoQueryDataFeature in your AppHost.Configure() method:

public override void Configure(Container container)
{
    // Get Data Provider Settings from Configuration 
    var connectionString = AppSettings.Get<string>("MongoDB.ConnectionString");
    var databaseId = AppSettings.Get<string>("MongoDB.DatabaseId");
    var collectionId = AppSettings.Get<string>("MongoDB.CollectionId");
            
    // Create a Mongo Client 
    var mongoClient = new MongoClient(
        new MongoUrl(connectionString));

    // Register the Document Client into the IOC
    container.Register<IMongoClient>(c => mongoClient);

    // Add AuthQueryDataFeature plugin with a DataSource
    Plugins.Add(new AutoQueryDataFeatureFeature()
        .AddDataSource(ctx => ctx.MongoDBDataSource<TDocument>(mongoClient, databaseId, collectionId)));
}

Documentation

More documentation about how the AutoQueryFeature works, how to target non-SQL data sources, and how to customize it are available in here

Contribute?

Want to get involved in this project? or want to help improve this capability for your services? just send us a message or pull-request!