Skip to content

Commit

Permalink
Add support for Ninject and Unity
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed May 18, 2024
1 parent 6ce5509 commit be4c339
Show file tree
Hide file tree
Showing 32 changed files with 534 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.0.2</Version>
<Version>2.1.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Ninject" Version="3.0.1.10" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="PolySharp" Version="1.13.1" />
<PackageVersion Include="Unity" Version="5.11.10" />
<PackageVersion Include="Unity.Abstractions" Version="4.0.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="xunit" Version="2.6.2" />
</ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ This repository houses the source code for the ReflectionEventing NuGet package.
ReflectionEventing is available as NuGet package on NuGet.org:
https://www.nuget.org/packages/ReflectionEventing
https://www.nuget.org/packages/ReflectionEventing.Autofac
https://www.nuget.org/packages/ReflectionEventing.DependencyInjection
https://www.nuget.org/packages/ReflectionEventing.DependencyInjection
https://www.nuget.org/packages/ReflectionEventing.Ninject
https://www.nuget.org/packages/ReflectionEventing.Unity

You can add it to your project using .NET CLI:

Expand Down
26 changes: 26 additions & 0 deletions ReflectionEventing.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReflectionEventing.Autofac.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReflectionEventing.DependencyInjection.UnitTests", "tests\ReflectionEventing.DependencyInjection.UnitTests\ReflectionEventing.DependencyInjection.UnitTests.csproj", "{CF2E3E06-48A3-47F7-B152-E86F25C9A182}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionEventing.Ninject", "src\ReflectionEventing.Ninject\ReflectionEventing.Ninject.csproj", "{194CA822-C3D6-4FBC-989A-039A0B97F2A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionEventing.Ninject.UnitTests", "tests\ReflectionEventing.Ninject.UnitTests\ReflectionEventing.Ninject.UnitTests.csproj", "{A741FC02-03AE-4A34-AAA0-FBFBAB93377D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionEventing.Unity", "src\ReflectionEventing.Unity\ReflectionEventing.Unity.csproj", "{EBE050AC-CE99-49E0-9836-361D9F0DB515}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionEventing.Unity.UnitTests", "tests\ReflectionEventing.Unity.UnitTests\ReflectionEventing.Unity.UnitTests.csproj", "{772AA314-8522-4A95-A99B-E69A867704DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +71,22 @@ Global
{CF2E3E06-48A3-47F7-B152-E86F25C9A182}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF2E3E06-48A3-47F7-B152-E86F25C9A182}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF2E3E06-48A3-47F7-B152-E86F25C9A182}.Release|Any CPU.Build.0 = Release|Any CPU
{194CA822-C3D6-4FBC-989A-039A0B97F2A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{194CA822-C3D6-4FBC-989A-039A0B97F2A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{194CA822-C3D6-4FBC-989A-039A0B97F2A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{194CA822-C3D6-4FBC-989A-039A0B97F2A8}.Release|Any CPU.Build.0 = Release|Any CPU
{A741FC02-03AE-4A34-AAA0-FBFBAB93377D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A741FC02-03AE-4A34-AAA0-FBFBAB93377D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A741FC02-03AE-4A34-AAA0-FBFBAB93377D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A741FC02-03AE-4A34-AAA0-FBFBAB93377D}.Release|Any CPU.Build.0 = Release|Any CPU
{EBE050AC-CE99-49E0-9836-361D9F0DB515}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBE050AC-CE99-49E0-9836-361D9F0DB515}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBE050AC-CE99-49E0-9836-361D9F0DB515}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBE050AC-CE99-49E0-9836-361D9F0DB515}.Release|Any CPU.Build.0 = Release|Any CPU
{772AA314-8522-4A95-A99B-E69A867704DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{772AA314-8522-4A95-A99B-E69A867704DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{772AA314-8522-4A95-A99B-E69A867704DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{772AA314-8522-4A95-A99B-E69A867704DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -71,6 +95,8 @@ Global
{7F506A5A-1F70-49CB-B371-428DFA573451} = {7A58AE12-65AA-4E1B-B5DB-CBD2732D1AE7}
{E5C7D945-F96C-4C05-9C12-4DB246E207CB} = {7A58AE12-65AA-4E1B-B5DB-CBD2732D1AE7}
{CF2E3E06-48A3-47F7-B152-E86F25C9A182} = {7A58AE12-65AA-4E1B-B5DB-CBD2732D1AE7}
{A741FC02-03AE-4A34-AAA0-FBFBAB93377D} = {7A58AE12-65AA-4E1B-B5DB-CBD2732D1AE7}
{772AA314-8522-4A95-A99B-E69A867704DC} = {7A58AE12-65AA-4E1B-B5DB-CBD2732D1AE7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B017BA78-67A5-4D0F-92A3-671B0D8A2911}
Expand Down
6 changes: 4 additions & 2 deletions src/ReflectionEventing.Autofac/AutofacConsumerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.

using System;
using System.Collections.Generic;
using Autofac;

namespace ReflectionEventing.Autofac;

/// <summary>
/// Provides event consumers for Autofac.
/// </summary>
public class AutofacConsumerProvider(ILifetimeScope lifetimeScope) : IConsumerProvider
{
/// <inheritdoc />
public IEnumerable<object> GetConsumerTypes(Type consumerType)
{
if (consumerType is null)
Expand Down
3 changes: 3 additions & 0 deletions src/ReflectionEventing.Autofac/AutofacEventBusBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

namespace ReflectionEventing.Autofac;

/// <summary>
/// Represents a builder for configuring the event bus with Autofac.
/// </summary>
public class AutofacEventBusBuilder(ContainerBuilder builder) : EventBusBuilder;
14 changes: 13 additions & 1 deletion src/ReflectionEventing.Autofac/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.

using System;
using Autofac;

namespace ReflectionEventing.Autofac;

/// <summary>
/// Provides extension methods for the <see cref="ContainerBuilder"/> class.
/// </summary>
public static class ContainerBuilderExtensions
{
/// <summary>
/// Adds the event bus and its related services to the specified Autofac container builder.
/// </summary>
/// <param name="builder">The <see cref="ContainerBuilder"/> to add the event bus to.</param>
/// <param name="configure">A delegate that configures the <see cref="EventBusBuilder"/>.</param>
/// <returns>The same container builder so that multiple calls can be chained.</returns>
/// <remarks>
/// This method adds a singleton service of type <see cref="IConsumerTypesProvider"/> that uses a <see cref="HashedConsumerTypesProvider"/> with the consumers from the event bus builder.
/// It also adds a scoped service of type <see cref="IEventBus"/> that uses the <see cref="EventBus"/> class.
/// </remarks>
public static ContainerBuilder AddEventBus(
this ContainerBuilder builder,
Action<EventBusBuilder> configure
Expand Down
7 changes: 7 additions & 0 deletions src/ReflectionEventing.Autofac/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

namespace ReflectionEventing.DependencyInjection;

/// <summary>
/// Represents a provider for retrieving event consumers from .NET Core's built-in dependency injection container.
/// </summary>
public class DependencyInjectionConsumerProvider(IServiceProvider serviceProvider)
: IConsumerProvider
{
/// <inheritdoc />
public IEnumerable<object> GetConsumerTypes(Type consumerType)
{
if (consumerType is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

namespace ReflectionEventing.DependencyInjection;

/// <summary>
/// Represents a builder for configuring the event bus with .NET Core's built-in dependency injection.
/// </summary>
public class DependencyInjectionEventBusBuilder(IServiceCollection services) : EventBusBuilder
{
/// <inheritdoc />
public override void AddConsumer(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
Expand Down
2 changes: 0 additions & 2 deletions src/ReflectionEventing.DependencyInjection/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
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;
33 changes: 33 additions & 0 deletions src/ReflectionEventing.Ninject/EventBusModule.cs
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.

using Ninject.Modules;

namespace ReflectionEventing.Ninject;

/// <summary>
/// Represents a Ninject module for configuring the event bus.
/// </summary>
public class EventBusModule(Action<NinjectEventBusBuilder> configure) : NinjectModule
{
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
NinjectEventBusBuilder builder = new(Kernel);

configure(builder);

_ = Bind<IConsumerTypesProvider>()
.To<HashedConsumerTypesProvider>()
.InSingletonScope()
.WithConstructorArgument("consumers", builder.GetConsumers());

_ = Bind<IConsumerProvider>().To<NinjectConsumerProvider>().InTransientScope();

_ = Bind<IEventBus>().To<EventBus>().InTransientScope();
}
}
8 changes: 8 additions & 0 deletions src/ReflectionEventing.Ninject/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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;
25 changes: 25 additions & 0 deletions src/ReflectionEventing.Ninject/NinjectConsumerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 Ninject;

namespace ReflectionEventing.Ninject;

/// <summary>
/// Provides event consumers for Ninject.
/// </summary>
public class NinjectConsumerProvider(IKernel kernel) : IConsumerProvider
{
/// <inheritdoc />
public IEnumerable<object> GetConsumerTypes(Type consumerType)
{
if (consumerType is null)
{
throw new ArgumentNullException(nameof(consumerType));
}

return kernel.GetAll(consumerType);
}
}
25 changes: 25 additions & 0 deletions src/ReflectionEventing.Ninject/NinjectEventBusBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 Ninject;

namespace ReflectionEventing.Ninject;

/// <summary>
/// Represents a builder for configuring the event bus with Ninject.
/// </summary>
public class NinjectEventBusBuilder(IKernel kernel) : EventBusBuilder
{
/// <inheritdoc />
public override void AddConsumer(Type consumerType)
{
if (!kernel.GetBindings(consumerType).Any())
{
throw new InvalidOperationException("Event consumer must be registered in the kernel.");
}

base.AddConsumer(consumerType);
}
}
36 changes: 36 additions & 0 deletions src/ReflectionEventing.Ninject/ReflectionEventing.Ninject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>ReflectionEventing.DependencyInjection</PackageId>
<TargetFrameworks>net462;net472;net481</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</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="Ninject" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReflectionEventing\ReflectionEventing.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions src/ReflectionEventing.Unity/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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;
42 changes: 42 additions & 0 deletions src/ReflectionEventing.Unity/ReflectionEventing.Unity.csproj
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="Unity.Abstractions" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReflectionEventing\ReflectionEventing.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions src/ReflectionEventing.Unity/UnityConsumerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 Unity;

namespace ReflectionEventing.Unity;

/// <summary>
/// Provides event consumers for Unity.
/// </summary>
public class UnityConsumerProvider(IUnityContainer container) : IConsumerProvider
{
/// <inheritdoc />
public IEnumerable<object> GetConsumerTypes(Type consumerType)
{
if (consumerType is null)
{
throw new ArgumentNullException(nameof(consumerType));
}

return container.ResolveAll(consumerType);
}
}
Loading

0 comments on commit be4c339

Please sign in to comment.