Skip to content

FriendlyAgent/RavenDB.AspNetCore.DependencyInjection

Repository files navigation

RavenDB.AspNetCore.DependencyInjection

Dependency Injection package for using RavenDB with ASP.NET Core.

Docker Stars Docker Pulls

This package handles the injection of DocumentSession (or AsyncDocumentSession) for you and while keeping track and managing the DocumentStore(s) for you.

Getting Started:

Install the RavenDB.AspNetCore.DependencyInjection library through NuGet.

    Install-Package RavenDB.AspNetCore.DependencyInjection
    
    Or
    
    Install-Package RavenDB.AspNetCore.DependencyInjection -Pre

Usage:

You can now configure the RavenManager service in your Startup.cs:

Use default options from configuration for single servers

Pass in a IConfiguration to automatically map the values to a RavenStoreOptions object.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
	
  services.AddRavenManagerWithDefaultServer(Configuration.GetSection("Raven"))
    .AddScopedAsyncSession();
	
...
}

You can specify the default options via configuration, for example in your appsettings.json files:

{
    "Raven": {
        "Url": "{server url}",
        "Database": "{default database}"
    }
}

Specify default single server configuration

If you're only using one Raven server, you can configure a single server's options.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
	
  services.AddRavenManagerWithDefaultServer(options => {
      options.Url = "{server url}";
      options.Database = "{database name}";
  })
    .AddScopedAsyncSession();
	
...
}

Use default options from configuration for multiple servers

Pass in a IConfiguration to automatically map the values to a RavenManagerOptions object.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
	
  services.AddRavenManager(Configuration.GetSection("Raven"))
    .AddScopedAsyncSession();
	
...
}

You can specify the default options via configuration, for example in your appsettings.json files:

{
    "Raven": {
        "Servers": {
            "Main": {
                "Url": "{server url}",
                "Database": "{default database}"
            }
        }
    }
}

Specify manager and options for multiple servers

If you need complete control over the RavenManager, you can configure its options.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
	
  services.AddRavenManager(
      options =>
      {
          options.DefaultServer = "Main";
          options.AddServer("Main", new RavenServerOptions()
          {
              Url = "{server url}",
              Database = "{database name}"
          });
      }).AddScopedAsyncSession();
	
...
}

Now you can use the standard injection syntax to get you're session or the raven manager:

public class HomeController
      : Controller
  {
      private readonly IAsyncDocumentSession _session;
      private readonly IRavenManager _ravenManager;

      public HomeController(
          IAsyncDocumentSession session,
          IRavenManager ravenManager)
      {
          _session = session;
          _ravenManager = ravenManager;
      }
  }

Session Services

AddScopedSession()

This will add the ability to request IDocumentSession with a request-scoped lifetime.

AddScopedAsyncSession()

This will add the ability to request IAsyncDocumentSession with a request-scoped lifetime.

Options

RavenManagerOptions

You can configure a list of RavenDB servers with friendly names and options.

  • DefaultServer (optional) - The default name of the Raven server to connect to. If null, will use first server name in Servers dictionary.
  • DefaultConventions - Default RavenDB document conventions, see Document Conventions
  • Servers - A dictionary of server names and server options
  • AddServer(string serverName, RavenServerOptions options) - Add a new server with a friendly name and options

RavenStoreOptions

  • Url - The URL to connect to the Raven server
  • DefaultDatabase - The database name to connect to
  • Conventions - Any override document conventions

User Feedback

Issues

If you have any problems with or questions about this package, please contact us through a GitHub issue.

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages