Skip to content

Making it simple and easy to use RavenDb in your .NET Core application(s).

License

Notifications You must be signed in to change notification settings

PureKrome/SimpleRavenDB

Repository files navigation

Simple: RavenDb

Making it simple and easy to use RavenDb in your .NET Core application(s).

Key Points

This library makes it SIMPLE (by abstracting away most of the boring ceremony) to use/code against a RavenDb server in your .NET application.

  • ✅ Easy to read in configuration settings.
  • ✅ Easy to seed fake data.
  • ✅ Easy to do 'database migrations'. (think => RavenDb indexes or even schema changes)
  • ✅ Easy to make sure any seeding/migrations are done before the web host starts.

Currently targetting: .NET60.


Installation

Package is available via NuGet.

dotnet add package WorldDomination.SimpleRavenDb

Quickstarts

REF: Sample Web Application which uses RavenDb

// appSettings.json

{
    "RavenDb": {
        "ServerUrls": [ "http://localhost:5200" ],
        "DatabaseName": "Testing-SimpleRavenDb",
        "X509CertificateBase64": ""
    },

    "Logging": {
        ...
    },
    "AllowedHosts": "*"
}
// startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        // Read in the RavenDb settings from appSettings.json
        var ravenDbOptions = Configuration.AddRavenDbConfiguration();
        var ravenDbSetupOptions = new RavenDbSetupOptions
        {
            DocumentCollections = FakeData()
        };

        // 1. Initializes an `IDocumentStore` and registers it with DI/IoC.
        // 2. Creates a DB migrations host, which will auto start
        // 3. Adds 2x fake document collections to the db, via the DB migrations host.
        services.AddSimpleRavenDb(ravenDbOptions, ravenDbSetupOptions);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    { ... }

    // A list of 2x document-collections: Users and Orders.
    private static List<IList> FakeData()
    {
        var fakeUsers = new List<User>
        {
            new User {  Name = "Princess Leia" },
            new User {  Name = "Han Solo" }
        };

        var fakeOrders = new List<Order>
        {
            new Order { Price = 1.1m },
            new Order { Price = 2.2m }
        };

        return new List<IList>
        {
            fakeUsers,
            fakeOrders
        };
    }
}

Breakdown of Quickstart sections

Reading RavenDb settings from an appSettings.json file

Given the following simple schema:

image

// `Configuration` is some IConfiguration which is usually setup elsewhere.
var ravenDbOptions = Configuration.AddRavenDbConfiguration();

Registering RavenDb with your applications DI/IoC

// `services` is some IServiceCollection which is usually setup elsewhere.
services.AddSimpleRavenDb(ravenDbOptions);

This will initialise a new IDocumentStore and add this to your DI/IoC. The ravenDbOptions settings are usually via configuration settings (e.g. environmental variables or appSettings.json, etc.)

Seeding fake data / migrating indexes.

Seeding fake data is a great scenario during your development. You wouldn't do this for prodution. This will insert some data into the database. For example: the same 10 users every time you start your application.

When doing database migations or data seeding, this should all be done -before- the website/application is ready to accept connections/requests from users. This means we will need to have a separate host which runs before the main application's host.

This is automatically setup when you call services.AddSimpleRavenDb(ravenDbOptions);. So besides just adding an IDocumentStore to your DI/IoC, it also secretly adds it's own 'IHostedService` which will run before your main application's hosted service, runs.


Contribute

Yep - contributions are always welcome. Please read the contribution guidelines first.

Code of Conduct

If you wish to participate in this repository then you need to abide by the code of conduct.

Feedback

Yes! Please use the Issues section to provide feedback - either good or needs improvement 🆒


About

Making it simple and easy to use RavenDb in your .NET Core application(s).

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages