/// <summary> /// Adds PineBlog MongoDb services to the specified services collection. /// </summary> /// <param name="services">The services available in the application.</param> /// <param name="configuration">The application configuration properties.</param> /// <returns>The original services object.</returns> public static IServiceCollection AddPineBlogMongoDb(this IServiceCollection services, IConfiguration configuration) { if (((IConfigurationRoot)configuration).Providers.SingleOrDefault(p => p.GetType() == typeof(BlogSettingsConfigurationProvider)) == null) { throw new ConfigurationException("The PineBlog IConfigurationProvider(s) are not configured, please add \"AddPineBlogMongoDbConfiguration\" to the \"ConfigureAppConfiguration\" on the \"IWebHostBuilder\".") { HelpLink = "https://github.com/ofpinewood/pineblog/blob/main/docs/mongodb.md#blog-settings-configurationprovider" }; } BsonClassMappings.Register(); // TODO: add check for when someone tries to add multiple databases services.AddTransient <IMongoDatabase>(provider => { var blogOptions = provider.GetRequiredService <IOptions <PineBlogOptions> >(); var connectionString = configuration.GetConnectionString(blogOptions.Value.ConnectionStringName); var client = new MongoClient(connectionString); return(client.GetDatabase(blogOptions.Value.MongoDbDatabaseName)); }); services.AddTransient <IBlogUnitOfWork, BlogUnitOfWork>(); return(services); }
/// <summary> /// Implementation of BlogSettingsConfigurationProvider. /// </summary> public BlogSettingsConfigurationProvider(BlogSettingsConfigurationSource source) { _source = source; // because the BlogSettingsConfigurationProvider runs outside of the DI we need to register the class mappings separately BsonClassMappings.Register(); if (_source.ReloadOnChange) { DocumentChangeObserver.Instance.Changed += DocumentChangeObserver_Changed; } }