以下文档显示了如何为 .net 核心项目配置 cosmonaut。
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));
});
旧的 webpi 项目怎么办?
Web Api 2 没有开箱即用的依赖注入,您可以使用 Autofac 和 Ninject 等第三方依赖注入包,或者如果您根本不想使用依赖注入,也可以创建 Cosmonaut 的单例类来使用.
注意:根据他们的文档,Cosmonaut 实例应用作每个实体的单例实例。
更新
通用单例类的实现,其中 T 是您要求实例的实体类型,
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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句