Skip to content

Commit

Permalink
Merge pull request #2 from event-driven-dotnet/use-mongo-dbcontext
Browse files Browse the repository at this point in the history
Refactor IMongoCollection<MongoSchema> Registration
  • Loading branch information
tonysneed authored May 16, 2021
2 parents 913c734 + 9e635af commit 3111710
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0-beta3</Version>
<Version>1.0.0-beta4</Version>
<Authors>Tony Sneed</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>eda-logo.jpeg</PackageIcon>
Expand All @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>schema-registry event-driven event-driven-architecture</PackageTags>
<PackageReleaseNotes>https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo/releases/tag/v1.0.0-beta3</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/event-driven-dotnet/EventDriven.SchemaRegistry.Mongo/releases/tag/v1.0.0-beta4</PackageReleaseNotes>
<PackageId>EventDriven.SchemaRegistry.Mongo</PackageId>
<RootNamespace>EventDriven.SchemaRegistry.Mongo</RootNamespace>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MongoDB.Driver;

namespace EventDriven.SchemaRegistry.Mongo
{
/// <summary>
/// Mongo Schema Registry DbContext
/// </summary>
public class MongoSchemaRegistryDbContext
{
/// <summary>
/// MongoSchemaRegistryDbContext constructor.
/// </summary>
/// <param name="client">Mongo client.</param>
/// <param name="schemasDatabaseName">Schemas database name.</param>
/// <param name="schemasCollectionName">Schemas collection name.</param>
public MongoSchemaRegistryDbContext(IMongoClient client,
string schemasDatabaseName, string schemasCollectionName)
{
var database = client.GetDatabase(schemasDatabaseName);
MongoSchemas = database.GetCollection<MongoSchema>(schemasCollectionName);
}

/// <summary>
/// Mongo schemas collection.
/// </summary>
public IMongoCollection<MongoSchema> MongoSchemas { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ public static IServiceCollection AddMongoSchemaRegistry(this IServiceCollection
configureStateStoreOptions.Invoke(schemaOptions);
services.Configure(configureStateStoreOptions);

// Register Mongo database and schema collection
services.AddSingleton<IMongoDatabase>(_ =>
// Register IMongoCollection<MongoSchema>
services.AddSingleton(sp =>
{
var client = new MongoClient(schemaOptions.ConnectionString);
return client.GetDatabase(schemaOptions.DatabaseName);
});
services.AddSingleton<IMongoCollection<MongoSchema>>(sp =>
{
var context = sp.GetRequiredService<IMongoDatabase>();
return context.GetCollection<MongoSchema>(schemaOptions.SchemasCollectionName);
var context = new MongoSchemaRegistryDbContext(new MongoClient(schemaOptions.ConnectionString),
schemaOptions.DatabaseName, schemaOptions.SchemasCollectionName);
return context.MongoSchemas;
});

// Register services
Expand Down

0 comments on commit 3111710

Please sign in to comment.