在RavenDB.net中将IDocumentStore设置为静态变量



我有一个继承RavenTestDriver的类。我第一次需要初始化IDocumentStore。因为出于某种原因,我只需要这个类的一个对象。这是我的代码:

public sealed class mystatic : RavenTestDriver
{

public static IDocumentStore store;
// here something like this store= GetDocumentStore()
public static IHostBuilder host = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
}).ConfigureAppConfiguration(config =>
{
//config.AddJsonFile("appsettings.json", optional: true);
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<ICacheStorage>(provider =>
{
return new Mock<ICacheStorage>().Object;
});
services.AddTransient<IAsyncDocumentSession>((c) =>
{
return store.OpenAsyncSession();
});
});
public static IHost cli = host.Start();
}

我的问题是如何初始化存储变量??或者如何在静态类中使用GetDocumentStore((初始化存储?

public class DocumentStoreHolder : RavenTestDriver
{
private IDocumentStore _store;
public IDocumentStore Store => _store;
public  DocumentStoreHolder()
{
_store = GetDocumentStore();
}
}
public sealed class mystatic 
{
public static readonly IDocumentStore _store;
static mystatic()
{
var storeHolder = new DocumentStoreHolder();
_store = storeHolder.Store;
}
}

最新更新