From 9e635af0ba18ec1ce2cfb9d79f5e4dcdffac71ef Mon Sep 17 00:00:00 2001 From: Anthony Sneed <2836367+tonysneed@users.noreply.github.com> Date: Sat, 15 May 2021 21:25:30 -0500 Subject: [PATCH] Refactor IMongoCollection registration. --- .../EventDriven.SchemaRegistry.Mongo.csproj | 4 +-- .../MongoSchemaRegistryDbContext.cs | 28 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 14 ++++------ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 src/EventDriven.SchemaRegistry.Mongo/MongoSchemaRegistryDbContext.cs diff --git a/src/EventDriven.SchemaRegistry.Mongo/EventDriven.SchemaRegistry.Mongo.csproj b/src/EventDriven.SchemaRegistry.Mongo/EventDriven.SchemaRegistry.Mongo.csproj index 003de00..94dfdc0 100644 --- a/src/EventDriven.SchemaRegistry.Mongo/EventDriven.SchemaRegistry.Mongo.csproj +++ b/src/EventDriven.SchemaRegistry.Mongo/EventDriven.SchemaRegistry.Mongo.csproj @@ -3,7 +3,7 @@ net5.0 true - 1.0.0-beta3 + 1.0.0-beta4 Tony Sneed MIT eda-logo.jpeg @@ -12,7 +12,7 @@ https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo.git git schema-registry event-driven event-driven-architecture - https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo/releases/tag/v1.0.0-beta3 + https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo/releases/tag/v1.0.0-beta4 EventDriven.SchemaRegistry.Mongo EventDriven.SchemaRegistry.Mongo diff --git a/src/EventDriven.SchemaRegistry.Mongo/MongoSchemaRegistryDbContext.cs b/src/EventDriven.SchemaRegistry.Mongo/MongoSchemaRegistryDbContext.cs new file mode 100644 index 0000000..dbb602c --- /dev/null +++ b/src/EventDriven.SchemaRegistry.Mongo/MongoSchemaRegistryDbContext.cs @@ -0,0 +1,28 @@ +using MongoDB.Driver; + +namespace EventDriven.SchemaRegistry.Mongo +{ + /// + /// Mongo Schema Registry DbContext + /// + public class MongoSchemaRegistryDbContext + { + /// + /// MongoSchemaRegistryDbContext constructor. + /// + /// Mongo client. + /// Schemas database name. + /// Schemas collection name. + public MongoSchemaRegistryDbContext(IMongoClient client, + string schemasDatabaseName, string schemasCollectionName) + { + var database = client.GetDatabase(schemasDatabaseName); + MongoSchemas = database.GetCollection(schemasCollectionName); + } + + /// + /// Mongo schemas collection. + /// + public IMongoCollection MongoSchemas { get; } + } +} \ No newline at end of file diff --git a/src/EventDriven.SchemaRegistry.Mongo/ServiceCollectionExtensions.cs b/src/EventDriven.SchemaRegistry.Mongo/ServiceCollectionExtensions.cs index 4d94b6b..c785b5c 100644 --- a/src/EventDriven.SchemaRegistry.Mongo/ServiceCollectionExtensions.cs +++ b/src/EventDriven.SchemaRegistry.Mongo/ServiceCollectionExtensions.cs @@ -28,16 +28,12 @@ public static IServiceCollection AddMongoSchemaRegistry(this IServiceCollection configureStateStoreOptions.Invoke(schemaOptions); services.Configure(configureStateStoreOptions); - // Register Mongo database and schema collection - services.AddSingleton(_ => + // Register IMongoCollection + services.AddSingleton(sp => { - var client = new MongoClient(schemaOptions.ConnectionString); - return client.GetDatabase(schemaOptions.DatabaseName); - }); - services.AddSingleton>(sp => - { - var context = sp.GetRequiredService(); - return context.GetCollection(schemaOptions.SchemasCollectionName); + var context = new MongoSchemaRegistryDbContext(new MongoClient(schemaOptions.ConnectionString), + schemaOptions.DatabaseName, schemaOptions.SchemasCollectionName); + return context.MongoSchemas; }); // Register services