Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JudahGabriel/RavenDB.DependencyInjection
Dependency Injection extensions for using RavenDB with ASP.NET Core.
https://github.com/JudahGabriel/RavenDB.DependencyInjection
Last synced: 5 days ago
JSON representation
Dependency Injection extensions for using RavenDB with ASP.NET Core.
- Host: GitHub
- URL: https://github.com/JudahGabriel/RavenDB.DependencyInjection
- Owner: JudahGabriel
- License: mit
- Created: 2015-09-14T10:44:05.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-13T00:04:10.000Z (10 months ago)
- Last Synced: 2024-08-02T19:33:46.068Z (3 months ago)
- Language: C#
- Homepage:
- Size: 1.21 MB
- Stars: 27
- Watchers: 4
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RavenDB.DependencyInjection
Dependency Injection package for using RavenDB with ASP.NET Core.This package lets you configure a RavenDB `DocumentStore` and create a singleton for it in the dependency injection container. Additionally, you can configure an `IAsyncDocumentSession` (or its synchronous equivalent) to be created per scope.
## Getting Started
Install the [RavenDB.DependencyInjection](https://www.nuget.org/packages/RavenDB.DependencyInjection) library through [NuGet](https://nuget.org).
```
Install-Package RavenDB.DependencyInjection
```## Usage
Add a RavenSettings section to your appsettings.json:
```json
"RavenSettings": {
"Urls": [
"http://live-test.ravendb.net"
],
"DatabaseName": "Demo",
"CertFilePath": "",
"CertPassword": ""
},
```Note that `CertFilePath` and `CertPassword` are optional. If you use a certificate to connect to your database, this should be a path relative to the content root. Is your certificate stored outside your code? See [manual configuration](https://github.com/JudahGabriel/RavenDB.DependencyInjection#manual-configuration).
Then in Startup.cs, tell Raven to use this database and add it to the DI container:
```csharp
public void ConfigureServices(IServiceCollection services)
{
// 1. Add an IDocumentStore singleton. Make sure that RavenSettings section exist in appsettings.json
services.AddRavenDbDocStore();// 2. Add a scoped IAsyncDocumentSession. For the sync version, use .AddRavenSession().
services.AddRavenDbAsyncSession();
}
```Now you're cooking! Your controllers and services can now have `IDocumentStore`, `IAsyncDocumentSession`, or `IDocumentSession` injected into them. 😎
### Configuring Raven conventions
Do you need to configure RavenDB conventions or perform other work before `docStore.Initialize()`? It's simple:
```csharp
services.AddRavenDbDocStore(options =>
{
options.BeforeInitializeDocStore = docStore => docStore.Conventions.IdentityPartsSeparator = "-";
}
```### Manual configuration
Is your Raven information stored outside of your code, such as environment variables or Azure Key Vault? If so, you can configure your doc store like this:```csharp
services.AddRavenDbDocStore(options =>
{
// Grab the DB name from appsettings.json
var dbName = options.Settings.DbName;
// But grab the cert and password from the cloud
var certBytes = Convert.FromBase64String(...); // load the certificate from wherever
var certPassword = ...; // grab the password from wherever
options.Certificate = new X509Certificate2(certBytes, certPassword);
});
```View the [Sample project](https://github.com/JudahGabriel/RavenDB.DependencyInjection/tree/master/Sample) to see it all in action.