Uno - Step 3
Our implementation here will fall into two broad categories - configuring the connection and adding it to the ASP.NET Core's DI container, then adding the indexing code. Before we get to that, though, we need to add some dependencies. To paket.dependencies
, add:
1: 2: 3: 4: |
|
Then, add these 4 packages to paket.references
in Uno and run paket install
to download and install these new dependencies.
Create the Database
If you run RavenDB in interactive mode, it should launch a browser with RavenDB Studio; if you have it running as a service on your local machine, go to http://localhost:8080. Using the studio, create a database called "O2F1".
Configuring the Connection and Adding to DI
We will store our connection settings with the other configuration for the application. The standard .NET Core name for such a file is appsettings.json
, so we create one with the following values:
1: 2: 3: 4: 5: 6: |
|
We also need to tell the compiler that, although this file doesn't need to be compiled, it does need to be copied to the output directory. We can add the following group to Uno.csproj
, just below the import for the Paket targets file:
1: 2: 3: 4: 5: |
|
When we were doing our quick-and-dirty "Hello World" in step 1, we had very minimal content in Startup.cs
. Now, we'll flesh that out a little more.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: |
|
This does the following:
- In the constructor, creates a configuration tree that is a union of
appsettings.json
,appsettings.{environment}.json
, and environment variables (each of those overriding the prior one if settings are specified in both) - In
ConfigureServices
, gets theRavenDB
configuration sections, uses it to configure theDocumentStore
instance, and registers the output of itsInitialize
method as theIDocumentStore
singleton in the DI container.
We'll come back to this file, but we need to write some more code first.
Defining Collections
RavenDB creates document collection names using the plural of the name of the type being stored - ex., a Post
would go in the Posts
collection. Its Ids also follow the form [collection]/[id]
, so post 123 would have the document Id Posts/123
. Data/Collection.cs
contains C# constants we will use to reference our collections. It also contains two utility methods: one for creating a document Id from a collection name and a Guid
, and the other for deriving the collection name and Id from a document Id.
Ensuring Indexes Exist
RavenDB provides a means of creating strongly-typed indexes as classes that extend AbstractIndexCreationTask<T>
; these definitions can be used to both define and query indexes. We will create these in the Uno.Data.Indexes
namespace. You can review all the files there, but we'll look at one example here.
The naming convention for indexes within RavenDB is [collection]/By[field]
. The index description below defines an index that allows us to query categories by web log Id and slug.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: |
|
Now, let's revisit Startup.cs
. The RavenDB client has a nice feature where it will scan assemblies for these indexes, and automatically create them. We'll use the name of this index to accomplish the registration.
1: 2: 3: 4: 5: 6: 7: 8: |
|
Now, if we build and run our application, then use RavenDB studio to look at the indexes for the O2F1
database, we should be able to see the indexes we specified.