使用RavenDB设置dotnet核心

时间:2017-03-30 10:43:21

标签: dependency-injection .net-core ravendb

我目前面临着在多个环境中使用dotnet核心设置RavenDB的问题。

StartUp 类中,我将Raven配置为Singleton并使用 IOptions 模式将 Raven 设置绑定到 RavenSettings 对象。

 public virtual void ConfigureServices(IServiceCollection services)
 {
     Services.AddMvc()

     //Add functionality to inject IOptions<T> 
     services.AddOptions();

     // App Settings
     services.Configure<RavenSettings>(Configuration.GetSection("Raven"));
     //services.Configure<RavenSettings>(settings => Configuration.GetSection("Raven").Bind(settings));

     // .NET core built in IOC
     services.AddSingleton(DocumentStoreHolder.Store);
     services.AddSingleton<IConfiguration>(Configuration);
 }

这是我的默认应用设置。

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
      }
  },
  "Raven": {
    "Url": "x",
    "DefaultDatabase": "x"
  }
}

这是我尝试将设置从 appsettings 绑定到...

的类
public class RavenSettings
{
    public string Url { get; set; }
    public string DefaultDatabase { get; set; }
}

下面的类跟随生成Raven文档存储的Raven文档。因为我使用的是单例,所以我没有按构造函数来注入设置。任何人都可以建议解决这个问题吗?

public sealed class DocumentStoreHolder
{
    private static RavenSettings _ravenSettings;

    public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
    {
        _ravenSettings = ravenSettings.Value;
    }

    public static IDocumentStore Store => DocStore.Value;

    private static readonly Lazy<IDocumentStore> DocStore = new Lazy<IDocumentStore>(CreateStore);

    private static IDocumentStore CreateStore()
    {
        var store = new DocumentStore
        {
            Url = _ravenSettings.Url,
            DefaultDatabase = _ravenSettings.DefaultDatabase
        }.Initialize();

        return store;
    }
}

2 个答案:

答案 0 :(得分:4)

根据Set的回答,我能够通过创建一个IDocumentStoreHolder接口并在IOC中注册它来解决它。

DocumentStoreHolder.cs

public class DocumentStoreHolder : IDocumentStoreHolder
{
    public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
    {
        var settings = ravenSettings.Value;

        Store = new DocumentStore
        {
            Url = settings.Url,
            DefaultDatabase = settings.DefaultDatabase
        }.Initialize();
    }

    public IDocumentStore Store { get; }
}

IDocumentStoreHolder.cs

public interface IDocumentStoreHolder
{
    IDocumentStore Store { get; }
}

Startup.cs(ConfigureServices)

services.AddSingleton<IDocumentStoreHolder, DocumentStoreHolder>();

SomeClass.cs

private readonly IDocumentStore _store;

public SomeClass(IDocumentStoreHolder documentStoreHolder)
{
    _store = documentStoreHolder.Store;
}

答案 1 :(得分:0)

ASP.NET Core DI已经使用singleton延迟加载:

  

Singleton生命周期服务是在第一次请求时创建的(或者当您在那里指定实例时运行ConfigureServices时),然后每个后续请求将使用相同的实例。

而且:

  

如果您的应用程序需要单例行为,建议允许服务容器管理服务的生命周期,而不是实现单例设计模式并自己在类中管理对象的生命周期。

因此,您可以这样做:

services.AddSingleton<DocumentStoreHolder>();

DocumentStoreHolder

中删除您自己的单例实现
public sealed class DocumentStoreHolder
{
    public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
    {
        var _ravenSettings = ravenSettings.Value;

        Store = new DocumentStore
        {
            Url = _ravenSettings.Url,
            DefaultDatabase = _ravenSettings.DefaultDatabase
        }.Initialize();

    }

    public IDocumentStore Store {get; private set;}
}