From eb894a4db907d4b9031d7448885353ea2b07c97d Mon Sep 17 00:00:00 2001 From: JudahGabriel Date: Thu, 8 Nov 2018 11:37:47 -0600 Subject: [PATCH] Database creation into its own extension. --- Sample/Extensions/RavenExtensions.cs | 32 ++++++++++++++++++++++ Sample/Startup.cs | 41 ++++++++++++++-------------- 2 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 Sample/Extensions/RavenExtensions.cs diff --git a/Sample/Extensions/RavenExtensions.cs b/Sample/Extensions/RavenExtensions.cs new file mode 100644 index 0000000..29e4ee5 --- /dev/null +++ b/Sample/Extensions/RavenExtensions.cs @@ -0,0 +1,32 @@ +using Raven.Client.Documents; +using Sample.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Sample.Extensions +{ + public static class RavenExtensions + { + public static IDocumentStore EnsureExists(this IDocumentStore store) + { + try + { + using (var dbSession = store.OpenSession()) + { + dbSession.Query().Take(0).ToList(); + } + } + catch (Raven.Client.Exceptions.Database.DatabaseDoesNotExistException) + { + store.Maintenance.Server.Send(new Raven.Client.ServerWide.Operations.CreateDatabaseOperation(new Raven.Client.ServerWide.DatabaseRecord + { + DatabaseName = store.Database + })); + } + + return store; + } + } +} diff --git a/Sample/Startup.cs b/Sample/Startup.cs index c8a9324..11cf7b7 100644 --- a/Sample/Startup.cs +++ b/Sample/Startup.cs @@ -12,6 +12,7 @@ using Sample.Models; using Sample.Services; using Raven.Identity; using Raven.Client.Documents; +using Sample.Extensions; namespace Sample { @@ -27,7 +28,6 @@ namespace Sample // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - // Connect to a Raven server. We're using the public test playground at http://live-test.ravendb.net var databaseName = "Raven.Identity.Sample"; var docStore = new DocumentStore @@ -35,8 +35,7 @@ namespace Sample Urls = new string[] { "http://live-test.ravendb.net" }, Database = databaseName }; - docStore.Initialize(); - CreateDatabaseIfNotExists(docStore, databaseName); + docStore.Initialize().EnsureExists(); // Add RavenDB and identity. services @@ -52,24 +51,6 @@ namespace Sample services.AddMvc(); } - private void CreateDatabaseIfNotExists(IDocumentStore docStore, string databaseName) - { - try - { - using (var dbSession = docStore.OpenSession()) - { - dbSession.Query().Take(0).ToList(); - } - } - catch (Raven.Client.Exceptions.Database.DatabaseDoesNotExistException) - { - docStore.Maintenance.Server.Send(new Raven.Client.ServerWide.Operations.CreateDatabaseOperation(new Raven.Client.ServerWide.DatabaseRecord - { - DatabaseName = databaseName - })); - } - } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { @@ -95,5 +76,23 @@ namespace Sample template: "{controller=Home}/{action=Index}/{id?}"); }); } + + private void CreateDatabaseIfNotExists(IDocumentStore docStore, string databaseName) + { + try + { + using (var dbSession = docStore.OpenSession()) + { + dbSession.Query().Take(0).ToList(); + } + } + catch (Raven.Client.Exceptions.Database.DatabaseDoesNotExistException) + { + docStore.Maintenance.Server.Send(new Raven.Client.ServerWide.Operations.CreateDatabaseOperation(new Raven.Client.ServerWide.DatabaseRecord + { + DatabaseName = databaseName + })); + } + } } }