-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Autofac; | ||
|
||
namespace ReflectionEventing.Autofac; | ||
|
||
public class AutofacConsumerProvider(ILifetimeScope lifetimeScope) : IConsumerProvider | ||
{ | ||
public IEnumerable<object> GetConsumerTypes(Type consumerType) | ||
{ | ||
if (consumerType is null) | ||
{ | ||
throw new ArgumentNullException(nameof(consumerType)); | ||
} | ||
|
||
return new List<object> { lifetimeScope.Resolve(consumerType) }; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
using Autofac; | ||
|
||
namespace ReflectionEventing.Autofac; | ||
|
||
public class AutofacEventBusBuilder(ContainerBuilder builder) : EventBusBuilder; | ||
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
Check warning on line 10 in src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs GitHub Actions / deploy
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
using System; | ||
using Autofac; | ||
|
||
namespace ReflectionEventing.Autofac; | ||
|
||
public static class ContainerBuilderExtensions | ||
{ | ||
public static ContainerBuilder AddEventBus( | ||
this ContainerBuilder builder, | ||
Action<EventBusBuilder> configure | ||
) | ||
{ | ||
AutofacEventBusBuilder autofacBuilder = new(builder); | ||
|
||
configure(autofacBuilder); | ||
|
||
_ = builder | ||
.RegisterType<HashedConsumerTypesProvider>() | ||
.As<IConsumerTypesProvider>() | ||
.WithParameter("consumers", autofacBuilder.GetConsumers()) | ||
.SingleInstance(); | ||
|
||
_ = builder | ||
.RegisterType<AutofacConsumerProvider>() | ||
.As<IConsumerProvider>() | ||
.InstancePerLifetimeScope(); | ||
|
||
_ = builder.RegisterType<EventBus>().As<IEventBus>().InstancePerLifetimeScope(); | ||
|
||
return builder; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>ReflectionEventing.Autofac</PackageId> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net6.0;net8.0</TargetFrameworks> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<IsTrimmable>true</IsTrimmable> | ||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<PublishAot>true</PublishAot> | ||
<StripSymbols>true</StripSymbols> | ||
<OptimizationPreference>Speed</OptimizationPreference> | ||
</PropertyGroup> | ||
|
||
<!-- Necessary polyfills --> | ||
<PropertyGroup> | ||
<PolySharpIncludeGeneratedTypes> | ||
System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute; | ||
System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.MemberNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute; | ||
System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute; | ||
System.Runtime.CompilerServices.CallerArgumentExpressionAttribute; | ||
System.Runtime.CompilerServices.IsExternalInit; | ||
System.Runtime.CompilerServices.SkipLocalsInitAttribute; | ||
</PolySharpIncludeGeneratedTypes> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Autofac" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ReflectionEventing\ReflectionEventing.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
namespace ReflectionEventing.DependencyInjection; | ||
|
||
public class DependencyInjectionConsumerProvider(IServiceProvider serviceProvider) | ||
: IConsumerProvider | ||
{ | ||
public IEnumerable<object> GetConsumerTypes(Type consumerType) | ||
{ | ||
if (consumerType is null) | ||
{ | ||
throw new ArgumentNullException(nameof(consumerType)); | ||
} | ||
|
||
IServiceScopeFactory? scopeFactory = serviceProvider.GetService<IServiceScopeFactory>(); | ||
|
||
if (scopeFactory is null) | ||
{ | ||
return GetConsumersFromComputedScope(consumerType); | ||
} | ||
|
||
return serviceProvider.GetServices(consumerType); | ||
} | ||
|
||
private IEnumerable<object> GetConsumersFromComputedScope(Type consumerType) | ||
{ | ||
using IServiceScope scope = serviceProvider.CreateScope(); | ||
return scope.ServiceProvider.GetServices(consumerType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace ReflectionEventing.DependencyInjection; | ||
|
||
public class DependencyInjectionEventBusBuilder(IServiceCollection services) : EventBusBuilder | ||
{ | ||
public override void AddConsumer( | ||
#if NET5_0_OR_GREATER | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] | ||
#endif | ||
Type consumerType | ||
) | ||
{ | ||
ServiceDescriptor? descriptor = services.FirstOrDefault(d => d.ServiceType == consumerType); | ||
|
||
if (descriptor is null) | ||
{ | ||
throw new InvalidOperationException( | ||
"Event consumer must be registered in the service collection." | ||
); | ||
} | ||
|
||
if (descriptor.Lifetime == ServiceLifetime.Transient) | ||
{ | ||
throw new InvalidOperationException("Transient consumers are not supported."); | ||
} | ||
|
||
base.AddConsumer(consumerType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Linq; | ||
global using System.Threading; | ||
global using System.Threading.Tasks; | ||
global using Microsoft.Extensions.DependencyInjection; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>ReflectionEventing.DependencyInjection</PackageId> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net6.0;net8.0</TargetFrameworks> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<IsTrimmable>true</IsTrimmable> | ||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<PublishAot>true</PublishAot> | ||
<StripSymbols>true</StripSymbols> | ||
<OptimizationPreference>Speed</OptimizationPreference> | ||
</PropertyGroup> | ||
|
||
<!-- Necessary polyfills --> | ||
<PropertyGroup> | ||
<PolySharpIncludeGeneratedTypes> | ||
System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute; | ||
System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.MemberNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute; | ||
System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute; | ||
System.Runtime.CompilerServices.CallerArgumentExpressionAttribute; | ||
System.Runtime.CompilerServices.IsExternalInit; | ||
System.Runtime.CompilerServices.SkipLocalsInitAttribute; | ||
</PolySharpIncludeGeneratedTypes> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ReflectionEventing\ReflectionEventing.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |