Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up dotnet core with RavenDB

I'm currently facing a problem with setting up RavenDB with dotnet core for multiple environments.

In StartUp class I have configured Raven as a Singleton and used the IOptions pattern to bind the settings Raven to the RavenSettings object.

 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);
 }

This is my default app settings.

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

This is the class that I have attempted to bind the setting from appsettings to ...

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

The class below follows the Raven documents in generating the Raven document store. Because I am using a singleton, I am not hitting the constructor to inject the settings. Can anyone advise a way around this?

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;
    }
}
like image 866
HelloAmigo Avatar asked Mar 10 '23 11:03

HelloAmigo


1 Answers

Building on Set's answer, I was able to resolve it by creating an IDocumentStoreHolder interface, and registering that in the 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;
}
like image 120
ChaoticNadirs Avatar answered Apr 05 '23 11:04

ChaoticNadirs