Skip to content

Commit

Permalink
EventStore options builder (#206)
Browse files Browse the repository at this point in the history
* EventStore options builder

* renamed AddExtension

* docs

Co-authored-by: Radu Popovici <[email protected]>
  • Loading branch information
oncicaradupopovici and Radu Popovici authored Feb 11, 2022
1 parent c7af16c commit f68bc7e
Show file tree
Hide file tree
Showing 40 changed files with 431 additions and 251 deletions.
7 changes: 7 additions & 0 deletions NBB.sln
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Messaging.Noop", "src\M
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.MultiTenancy.Configuration.Tests", "test\UnitTests\MultiTenancy\NBB.MultiTenancy.Configuration.Tests\NBB.MultiTenancy.Configuration.Tests.csproj", "{CEE3FAE5-2E96-4318-9A4D-7FAAC529CB08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBB.EventStore.AdoNet.Tests", "test\UnitTests\EventStore\NBB.EventStore.AdoNet.Tests\NBB.EventStore.AdoNet.Tests.csproj", "{C3F79479-1011-4ECB-8876-81B81CA2457B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -855,6 +857,10 @@ Global
{CEE3FAE5-2E96-4318-9A4D-7FAAC529CB08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEE3FAE5-2E96-4318-9A4D-7FAAC529CB08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEE3FAE5-2E96-4318-9A4D-7FAAC529CB08}.Release|Any CPU.Build.0 = Release|Any CPU
{C3F79479-1011-4ECB-8876-81B81CA2457B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3F79479-1011-4ECB-8876-81B81CA2457B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3F79479-1011-4ECB-8876-81B81CA2457B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3F79479-1011-4ECB-8876-81B81CA2457B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1013,6 +1019,7 @@ Global
{2E1525F5-9FF4-47C0-94C1-2BA295A5AD20} = {A4361674-5AB7-442D-8DA7-7187C9BCA38F}
{A53CE5F4-841F-40E3-A079-22C583509D6E} = {584C62C0-2AE6-4DD6-9BCF-8FF28B7122CE}
{CEE3FAE5-2E96-4318-9A4D-7FAAC529CB08} = {0ECF24A0-BFED-4F7A-8B06-CC40E628E852}
{C3F79479-1011-4ECB-8876-81B81CA2457B} = {0407911F-89FC-4138-BD4D-D4CFCFBB5DC1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23A42379-616A-43EF-99BC-803DF151F54E}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public static async Task Main(string[] args)
services.AddContractsReadModelDataAccess();


services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer(new[] { new SingleValueObjectConverter() })
.WithAdoNetEventRepository();
services.AddEventStore(b =>
{
b.UseNewtownsoftJson(new SingleValueObjectConverter());
b.UseAdoNetEventRepository(o => o.FromConfiguration());
});

services.Decorate<IMessageBusPublisher, OpenTracingPublisherDecorator>();
services.AddMessagingHost(hostingContext.Configuration, hostBuilder => hostBuilder.UseStartup<MessagingHostStartup>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
},
"EventStore": {
"NBB": {
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Contracts;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true",
"TopicSufix": "NBB.Contracts"
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Contracts;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true"
},
"GetEventStore": {
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static async Task Main(string[] args)
.Enrich.FromLogContext()
.Enrich.With<CorrelationLogEventEnricher>()
.WriteTo.MSSqlServer(connectionString,
new MSSqlServerSinkOptions {TableName = "Logs", AutoCreateSqlTable = true})
new MSSqlServerSinkOptions { TableName = "Logs", AutoCreateSqlTable = true })
.CreateLogger();

loggingBuilder.AddSerilog(dispose: true);
Expand All @@ -52,14 +52,16 @@ public static async Task Main(string[] args)

services.AddMessageBus().AddNatsTransport(hostingContext.Configuration);
services.AddInvoicesWriteDataAccess();
services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer(new[] {new SingleValueObjectConverter()})
.WithAdoNetEventRepository();
services.AddEventStore(e =>
{
e.UseNewtownsoftJson(new SingleValueObjectConverter());
e.UseAdoNetEventRepository(o => o.FromConfiguration());
});

services.AddMessagingHost(
hostingContext.Configuration,
hostBuilder => hostBuilder
.Configure(configBuilder => configBuilder
.Configure(configBuilder => configBuilder
.AddSubscriberServices(subscriberBuilder => subscriberBuilder
.FromMediatRHandledCommands().AddAllClasses()
.FromMediatRHandledEvents().AddAllClasses()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
},
"EventStore": {
"NBB": {
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Invoices;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true",
"TopicSufix": "NBB.Invoices"
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Invoices;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true"
},
"GetEventStore": {
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ public static async Task Main(string[] args)

services.AddMessageBus().AddNatsTransport(hostingContext.Configuration);

services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer()
.WithAdoNetEventRepository();
services.AddEventStore(es =>
{
es.UseNewtownsoftJson();
es.UseAdoNetEventRepository(o => o.FromConfiguration());
});

var integrationMessageAssemblies = new[] {
typeof(NBB.Contracts.PublishedLanguage.ContractValidated).Assembly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public static async Task Main(string[] args)

services.AddPaymentsWriteDataAccess();

services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer(new[] {new SingleValueObjectConverter()})
.WithAdoNetEventRepository();
services.AddEventStore(es =>
{
es.UseNewtownsoftJson(new SingleValueObjectConverter());
es.UseAdoNetEventRepository(opts => opts.FromConfiguration());
});

services.AddMessagingHost(
hostingContext.Configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
},
"EventStore": {
"NBB": {
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Payments;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true",
"TopicSufix": "NBB.Payments"
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Payments;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true"
},
"GetEventStore": {
},
Expand Down
3 changes: 1 addition & 2 deletions samples/Monolith/NBB.Mono.Migrations/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
},
"EventStore": {
"NBB": {
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Mono;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true",
"TopicSufix": "NBB.Contracts"
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Mono;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true"
},
"GetEventStore": {
},
Expand Down
8 changes: 5 additions & 3 deletions samples/Monolith/NBB.Mono/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddInvoicesDataAccess();
services.AddPaymentsDataAccess();

services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer(new[] { new SingleValueObjectConverter() })
.WithAdoNetEventRepository();
services.AddEventStore(es =>
{
es.UseNewtownsoftJson(new SingleValueObjectConverter());
es.UseAdoNetEventRepository(opts => opts.FromConfiguration());
});

var integrationMessageAssemblies = new[] {
typeof(NBB.Contracts.PublishedLanguage.ContractValidated).Assembly,
Expand Down
3 changes: 1 addition & 2 deletions samples/Monolith/NBB.Mono/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
},
"EventStore": {
"NBB": {
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Mono;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true",
"TopicSufix": "NBB.Contracts"
"ConnectionString": "Server=YOUR_SERVER;Database=NBB_Mono;User Id=YOUR_USER;Password=YOUR_PASSWORD;MultipleActiveResultSets=true"
},
"GetEventStore": {
},
Expand Down
12 changes: 7 additions & 5 deletions samples/Orchestration/ProcessManagerSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public static void ConfigureServicesDelegate(HostBuilderContext context, IServic

services.AddProcessManager(Assembly.GetEntryAssembly());

services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer()
.WithAdoNetEventRepository();
services.AddEventStore(es =>
{
es.UseNewtownsoftJson();
es.UseAdoNetEventRepository(opts => opts.FromConfiguration());
});

services.AddMessagingHost(
context.Configuration,
Expand All @@ -41,8 +43,8 @@ public static void ConfigureServicesDelegate(HostBuilderContext context, IServic
.WithDefaultOptions()
.UsePipeline(builder => builder
.UseCorrelationMiddleware()
.UseExceptionHandlingMiddleware()
//.UseMiddleware<OpenTracingMiddleware>()
.UseExceptionHandlingMiddleware()
//.UseMiddleware<OpenTracingMiddleware>()
.UseDefaultResiliencyMiddleware()
.UseMiddleware<SubscriberLoggingMiddleware>()
.UseMediatRMiddleware())
Expand Down
3 changes: 1 addition & 2 deletions samples/Orchestration/ProcessManagerSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"EventStore": {
"NBB": {
"TopicSufix": "ProcessMananger"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static bool HasSameTypeDefinition(Type t1, Type t2)
return t1.IsGenericType && t2.IsGenericType && t1.GetGenericTypeDefinition() == t2.GetGenericTypeDefinition();
}

private static bool TryDecorateOpenGeneric(this IServiceCollection services, Type serviceType, Type decoratorType, [NotNullWhen(false)] out Exception? error, Predicate<Type> serviceTypePredicate = null)
private static bool TryDecorateOpenGeneric(this IServiceCollection services, Type serviceType, Type decoratorType, [NotNullWhen(false)] out Exception error, Predicate<Type> serviceTypePredicate = null)
{
var closedGenericServiceTypes = services
.Where(x => !x.ServiceType.IsGenericTypeDefinition)
Expand Down Expand Up @@ -323,7 +323,7 @@ private static IServiceCollection DecorateDescriptors(this IServiceCollection se
throw error;
}

private static bool TryDecorateDescriptors(this IServiceCollection services, Type serviceType, [NotNullWhen(false)] out Exception? error, Func<ServiceDescriptor, ServiceDescriptor> decorator)
private static bool TryDecorateDescriptors(this IServiceCollection services, Type serviceType, [NotNullWhen(false)] out Exception error, Func<ServiceDescriptor, ServiceDescriptor> decorator)
{
if (!services.TryGetDescriptors(serviceType, out var descriptors))
{
Expand Down Expand Up @@ -363,7 +363,7 @@ private static ServiceDescriptor Decorate(this ServiceDescriptor descriptor, Typ
return descriptor.WithFactory(provider => provider.CreateInstance(decoratorType, provider.GetInstance(descriptor)));
}

private static ServiceDescriptor WithFactory(this ServiceDescriptor descriptor, Func<IServiceProvider, object?> factory)
private static ServiceDescriptor WithFactory(this ServiceDescriptor descriptor, Func<IServiceProvider, object> factory)
{
return ServiceDescriptor.Describe(descriptor.ServiceType, factory, descriptor.Lifetime);
}
Expand Down
41 changes: 0 additions & 41 deletions src/EventStore/NBB.EventStore.Abstractions/EventStoreEnvelope.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/EventStore/NBB.EventStore.Abstractions/EventStoreOptions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NBB.EventStore.Abstractions;
using NBB.EventStore.AdoNet.Internal;
using NBB.MultiTenancy.Abstractions.Context;
using System.Collections.Generic;
Expand All @@ -16,7 +15,7 @@ public class AdoNetMultiTenantEventRepository : AdoNetEventRepository
{
private readonly ITenantContextAccessor _tenantContextAccessor;

public AdoNetMultiTenantEventRepository(Scripts scripts, ILogger<AdoNetEventRepository> logger, IOptions<EventStoreOptions> eventstoreOptions, ITenantContextAccessor tenantContextAccessor)
public AdoNetMultiTenantEventRepository(Scripts scripts, ILogger<AdoNetEventRepository> logger, IOptionsSnapshot<EventStoreAdoNetOptions> eventstoreOptions, ITenantContextAccessor tenantContextAccessor)
: base(scripts, logger, eventstoreOptions)
{
_tenantContextAccessor = tenantContextAccessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NBB.EventStore.Abstractions;
using NBB.EventStore.AdoNet.Internal;
using NBB.MultiTenancy.Abstractions.Context;
using System.Collections.Generic;
Expand All @@ -17,7 +16,7 @@ public class AdoNetMultitenantSnapshotRepository : AdoNetSnapshotRepository
private readonly ITenantContextAccessor _tenantContextAccessor;

public AdoNetMultitenantSnapshotRepository(Scripts scripts,
ILogger<AdoNetSnapshotRepository> logger, IOptions<EventStoreOptions> eventstoreOptions, ITenantContextAccessor tenantContextAccessor)
ILogger<AdoNetSnapshotRepository> logger, IOptionsSnapshot<EventStoreAdoNetOptions> eventstoreOptions, ITenantContextAccessor tenantContextAccessor)
: base(scripts, logger, eventstoreOptions)
{
_tenantContextAccessor = tenantContextAccessor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

using Microsoft.Extensions.Configuration;
using NBB.EventStore;
using NBB.EventStore.AdoNet;
using NBB.EventStore.AdoNet.Multitenancy;
using NBB.EventStore.AdoNet.Multitenancy.Internal;
using NBB.MultiTenancy.Abstractions.Configuration;
using System;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;

public static class MultiTenantEventStoreAdoNetBuilderExtensions
{
public static EventStoreAdoNetOptionsBuilder FromTenantConfiguration(this EventStoreAdoNetOptionsBuilder b, string name)
=> b.From<ITenantConfiguration>((c, o) => o.ConnectionString = c.GetConnectionString(name));
}

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
public static class MultiTenantAdoNetEventStoreOptionsBuilderExtensions
{
public static class DependencyInjectionExtensions
{
public static IServiceCollection WithMultiTenantAdoNetEventRepository(this IServiceCollection services)
{
services.AddSingleton<IEventRepository, AdoNetMultiTenantEventRepository>();
services.AddSingleton<ISnapshotRepository, AdoNetMultitenantSnapshotRepository>();
services.AddSingleton<NBB.EventStore.AdoNet.Internal.Scripts, Scripts>();
public static EventStoreOptionsBuilder UseMultiTenantAdoNetEventRepository(this EventStoreOptionsBuilder b, Action<EventStoreAdoNetOptionsBuilder> optionsAction) => ((IEventStoreOptionsBuilder)b).AdExtension(services =>
{
services.AddScoped<IEventRepository, AdoNetMultiTenantEventRepository>();
services.AddScoped<ISnapshotRepository, AdoNetMultitenantSnapshotRepository>();
services.AddSingleton<NBB.EventStore.AdoNet.Internal.Scripts, Scripts>();

return services;
}
}
var b = new EventStoreAdoNetOptionsBuilder();
optionsAction?.Invoke(b);
services.AddOptions<EventStoreAdoNetOptions>().Configure<IServiceProvider>(((IEventStoreAdoNetOptionsBuilder)b).Configure);
});
}
5 changes: 2 additions & 3 deletions src/EventStore/NBB.EventStore.AdoNet/AdoNetEventRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.Options;
using Microsoft.SqlServer.Server;
using NBB.Core.Abstractions;
using NBB.EventStore.Abstractions;
using NBB.EventStore.AdoNet.Internal;
using System;
using System.Collections.Generic;
Expand All @@ -22,7 +21,7 @@ public class AdoNetEventRepository : IEventRepository
{
private readonly Scripts _scripts;
private readonly ILogger<AdoNetEventRepository> _logger;
private readonly IOptions<EventStoreOptions> _eventstoreOptions;
private readonly IOptionsSnapshot<EventStoreAdoNetOptions> _eventstoreOptions;

// TODO: migrate to Microsoft.Data.SqlClient
private readonly SqlMetaData[] _appendEventsMetadata = new List<SqlMetaData>
Expand All @@ -34,7 +33,7 @@ public class AdoNetEventRepository : IEventRepository
new SqlMetaData("CorrelationId", SqlDbType.UniqueIdentifier),
}.ToArray();

public AdoNetEventRepository(Scripts scripts, ILogger<AdoNetEventRepository> logger, IOptions<EventStoreOptions> eventstoreOptions)
public AdoNetEventRepository(Scripts scripts, ILogger<AdoNetEventRepository> logger, IOptionsSnapshot<EventStoreAdoNetOptions> eventstoreOptions)
{
_scripts = scripts;
_eventstoreOptions = eventstoreOptions;
Expand Down
Loading

0 comments on commit f68bc7e

Please sign in to comment.