Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion publisher/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>

<CoreLibVersion>10.0.2750-preview</CoreLibVersion>
<CoreLibVersion>10.0.2794-preview</CoreLibVersion>
<MassTransitVersion>8.5.7</MassTransitVersion>
</PropertyGroup>

Expand All @@ -13,6 +13,7 @@
<PackageVersion Include="LeanCode.CQRS.Security" Version="$(CoreLibVersion)" />
<PackageVersion Include="LeanCode.IntegrationTestHelpers" Version="$(CoreLibVersion)" />
<PackageVersion Include="LeanCode.Logging.AspNetCore" Version="$(CoreLibVersion)" />
<PackageVersion Include="LeanCode.Serialization" Version="$(CoreLibVersion)" />

<PackageVersion Include="MassTransit.RabbitMQ" Version="$(MassTransitVersion)" />
<PackageVersion Include="MassTransit.SignalR" Version="$(MassTransitVersion)" />
Expand Down
18 changes: 14 additions & 4 deletions publisher/src/Funnel/Instance/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using LeanCode.Serialization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;

namespace LeanCode.Pipe.Funnel.Instance;
Expand All @@ -9,15 +11,23 @@ public static class ServiceCollectionExtensions
/// </summary>
public static IServiceCollection AddLeanPipeFunnel(
this IServiceCollection services,
FunnelConfiguration? config = null
FunnelConfiguration? config = null,
Action<HubOptions<LeanPipeSubscriber>>? configureLeanPipeHub = null,
Action<JsonHubProtocolOptions>? overrideJsonHubProtocolOptions = null
)
{
services
var signalRBuilder = services
.AddSignalR()
.AddJsonProtocol(options =>
options.PayloadSerializerOptions.PropertyNamingPolicy = null
.AddJsonProtocol(
overrideJsonHubProtocolOptions
?? (options => options.PayloadSerializerOptions.ConfigureForCQRS())
);

if (configureLeanPipeHub is not null)
{
signalRBuilder.AddHubOptions(configureLeanPipeHub);
}

services.AddMemoryCache();

services.AddSingleton(config ?? FunnelConfiguration.Default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LeanCode.Components;
using LeanCode.Serialization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,7 +18,8 @@ public static class ServiceCollectionExtensions
public static LeanPipeServicesBuilder AddFunnelledLeanPipe(
this IServiceCollection services,
TypesCatalog topics,
TypesCatalog handlers
TypesCatalog handlers,
Action<JsonHubProtocolOptions>? overrideJsonHubProtocolOptions = null
)
{
services.AddTransient<LeanPipeSecurity>();
Expand All @@ -28,8 +30,8 @@ TypesCatalog handlers

services.TryAddEnumerable(ServiceDescriptor.Singleton<IHubProtocol, JsonHubProtocol>());
services.Configure(
(JsonHubProtocolOptions options) =>
options.PayloadSerializerOptions.PropertyNamingPolicy = null
overrideJsonHubProtocolOptions
?? (options => options.PayloadSerializerOptions.ConfigureForCQRS())
);

return new LeanPipeServicesBuilder(services, topics).AddHandlers(handlers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text.Json;
using LeanCode.Components;
using LeanCode.Contracts;
using LeanCode.Serialization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -16,15 +18,23 @@ public static class LeanPipeServiceCollectionExtensions
public static LeanPipeServicesBuilder AddLeanPipe(
this IServiceCollection services,
TypesCatalog topics,
TypesCatalog handlers
TypesCatalog handlers,
Action<HubOptions<LeanPipeSubscriber>>? configureLeanPipeHub = null,
Action<JsonHubProtocolOptions>? overrideJsonHubProtocolOptions = null
)
{
services
var signalRBuilder = services
.AddSignalR()
.AddJsonProtocol(options =>
options.PayloadSerializerOptions.PropertyNamingPolicy = null
.AddJsonProtocol(
overrideJsonHubProtocolOptions
?? (options => options.PayloadSerializerOptions.ConfigureForCQRS())
);

if (configureLeanPipeHub is not null)
{
signalRBuilder.AddHubOptions(configureLeanPipeHub);
}

services.AddTransient<LeanPipeSecurity>();
services.AddTransient<ISubscriptionExecutor, SubscriptionExecutor>();
services.AddTransient(typeof(ISubscriptionHandler<>), typeof(KeyedSubscriptionHandler<>));
Expand All @@ -44,14 +54,17 @@ public class LeanPipeServicesBuilder
public IServiceCollection Services { get; }
public TypesCatalog Topics { get; private set; }

private JsonSerializerOptions? options;
private JsonSerializerOptions options;

public LeanPipeServicesBuilder(IServiceCollection services, TypesCatalog topics)
{
Services = services;
Topics = topics;

Services.AddSingleton<ITopicExtractor>(new DefaultTopicExtractor(topics, null));
options = new();
options.ConfigureForCQRS();

Services.AddSingleton<ITopicExtractor>(new DefaultTopicExtractor(topics, options));
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions publisher/src/LeanCode.Pipe/LeanCode.Pipe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="LeanCode.Contracts" />
<PackageReference Include="LeanCode.CQRS.Security" />
<PackageReference Include="LeanCode.Logging.AspNetCore" />
<PackageReference Include="LeanCode.Serialization" />

<PackageReference Include="MassTransit.SignalR" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Text.Json;
using LeanCode.Pipe.Funnel.Instance;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace LeanCode.Pipe.Funnel.Tests.Instance;

public class ServiceCollectionExtensionsTests
{
[Fact]
public void Registers_required_basic_types_when_service_is_funnel()
{
var collection = new ServiceCollection();
collection.AddLeanPipeFunnel();
collection
.Should()
.ContainSingle(d =>
d.ServiceType == typeof(HubLifetimeManager<>)
&& d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(IMemoryCache) && d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(FunnelConfiguration)
&& d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(ISubscriptionExecutor)
&& d.Lifetime == ServiceLifetime.Transient
);
}

[Fact]
public void Default_serializer_configuration_is_applied_to_funnel_when_no_override_is_provided()
{
var collection = new ServiceCollection();
collection.AddLeanPipeFunnel();

var provider = collection.BuildServiceProvider();
var hubProtocolOptions = provider
.GetRequiredService<IOptions<JsonHubProtocolOptions>>()
.Value;

hubProtocolOptions.PayloadSerializerOptions.PropertyNamingPolicy.Should().BeNull();
}

[Fact]
public void Override_replaces_default_serializer_configuration_in_funnel()
{
var collection = new ServiceCollection();
collection.AddLeanPipeFunnel(overrideJsonHubProtocolOptions: options =>
options.PayloadSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase
);

var provider = collection.BuildServiceProvider();
var hubProtocolOptions = provider
.GetRequiredService<IOptions<JsonHubProtocolOptions>>()
.Value;

hubProtocolOptions
.PayloadSerializerOptions.PropertyNamingPolicy.Should()
.Be(JsonNamingPolicy.CamelCase);
}

[Fact]
public void Hub_options_delegate_is_invoked_when_provided_to_funnel()
{
var collection = new ServiceCollection();
collection.AddLeanPipeFunnel(configureLeanPipeHub: options =>
options.MaximumReceiveMessageSize = 12345
);

var provider = collection.BuildServiceProvider();
var hubOptions = provider
.GetRequiredService<IOptions<HubOptions<LeanPipeSubscriber>>>()
.Value;

hubOptions.MaximumReceiveMessageSize.Should().Be(12345);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Text.Json;
using LeanCode.Components;
using LeanCode.Pipe.Funnel.Instance;
using LeanCode.Pipe.Funnel.Publishing;
using LeanCode.Pipe.Tests;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace LeanCode.Pipe.Funnel.Tests;
namespace LeanCode.Pipe.Funnel.Tests.Publishing;

public class ServiceCollectionExtensionsTests
{
Expand Down Expand Up @@ -48,26 +48,37 @@ public void Registers_required_basic_types_when_service_is_funnelled()
}

[Fact]
public void Registers_required_basic_types_when_service_is_funnel()
public void Default_serializer_configuration_is_applied_to_funnelled_when_no_override_is_provided()
{
var collection = new ServiceCollection();
collection.AddLeanPipeFunnel();
collection
.Should()
.ContainSingle(d =>
d.ServiceType == typeof(HubLifetimeManager<>)
&& d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(IMemoryCache) && d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(FunnelConfiguration)
&& d.Lifetime == ServiceLifetime.Singleton
)
.And.ContainSingle(d =>
d.ServiceType == typeof(ISubscriptionExecutor)
&& d.Lifetime == ServiceLifetime.Transient
);
collection.AddFunnelledLeanPipe(ThisCatalog, ThisCatalog);

var provider = collection.BuildServiceProvider();
var hubProtocolOptions = provider
.GetRequiredService<IOptions<JsonHubProtocolOptions>>()
.Value;

hubProtocolOptions.PayloadSerializerOptions.PropertyNamingPolicy.Should().BeNull();
}

[Fact]
public void Override_replaces_default_serializer_configuration_in_funnelled()
{
var collection = new ServiceCollection();
collection.AddFunnelledLeanPipe(
ThisCatalog,
ThisCatalog,
overrideJsonHubProtocolOptions: options =>
options.PayloadSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase
);

var provider = collection.BuildServiceProvider();
var hubProtocolOptions = provider
.GetRequiredService<IOptions<JsonHubProtocolOptions>>()
.Value;

hubProtocolOptions
.PayloadSerializerOptions.PropertyNamingPolicy.Should()
.Be(JsonNamingPolicy.CamelCase);
}
}
Loading