0

The following documentation shows how to configure cosmonaut for a .net core project.

https://github.com/Elfocrash/Cosmonaut

Registering the CosmosStores in ServiceCollection for DI support

 var cosmosSettings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");

serviceCollection.AddCosmosStore<Book>(cosmosSettings);

//or just by using the Action extension

serviceCollection.AddCosmosStore<Book>("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>", settings =>
{
    settings.ConnectionPolicy = connectionPolicy;
    settings.DefaultCollectionThroughput = 5000;
    settings.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.Number, -1),
        new RangeIndex(DataType.String, -1));
});

How do I do for an old webpi project?

Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • 1
    From the docs: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection – Styxxy Feb 24 '19 at 17:30

1 Answers1

2

Web Api 2 doesn't come with out of box dependency injection, you can use 3rd party dependency injection packages like Autofac and Ninject etc, or you can create Cosmonaut's singleton class for use too if you don't want to use dependency injection at all.

Note: As per their docs, Cosmonaut instance should be used as singleton instances per entity.

UPDATE

Implementation of a generic Singleton class where T is the type of entity you are asking for the instance of,

public sealed class CosmosStoreSingleton<T>
{
    private static ICosmosStore<T> instance = null;

    public static ICosmosStore<T> Instance
    {
        get
        {
            if (instance==null)
            {
                var cosmosSettings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");
                instance = new CosmosStore<T>(cosmosSettings);
            }

            return instance;
        }
    }
}
Riaz Raza
  • 382
  • 1
  • 14
  • 1
    I'll be glad to provide you with sample code if you want for resolving it through Autofac or custom Singleton class. – Riaz Raza Feb 24 '19 at 19:46
  • 1
    You should use locking when creating the singleton instance. – Maarten Feb 25 '19 at 10:21
  • Yeah one should use that when the instance is not thread safe, but this cosmos store instance seems to be thread safe according to their documentation. – Riaz Raza Feb 25 '19 at 10:23