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
2 changes: 1 addition & 1 deletion Samples/MediatorSampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
services.AddValidatorsFromAssembly(typeof(Program).Assembly);


services.AddKSMediator(Assembly.GetExecutingAssembly());
services.AddKSFramework(Assembly.GetExecutingAssembly());


var provider = services.BuildServiceProvider();
Expand Down
6 changes: 6 additions & 0 deletions src/KSFramework/Contracts/IInjectable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace KSFramework.Contracts;

public interface IInjectable
{

}
6 changes: 6 additions & 0 deletions src/KSFramework/Contracts/IInjectableAndImplementations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace KSFramework.Contracts;

public interface IInjectableAndImplementations
{

}
42 changes: 39 additions & 3 deletions src/KSFramework/KSMessaging/Extensions/RegisterMediatorServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using KSFramework.KSMessaging.Abstraction;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using KSFramework.Contracts;
using KSFramework.KSMessaging.Behaviors;

namespace KSFramework.KSMessaging.Extensions;
Expand All @@ -16,12 +17,13 @@ public static class RegisterMediatorServices
/// <param name="services">The service collection.</param>
/// <param name="assemblies">The assemblies to scan.</param>
/// <returns>The service collection.</returns>
public static IServiceCollection AddKSMediator(this IServiceCollection services, params Assembly[] assemblies)
public static IServiceCollection AddKSFramework(this IServiceCollection services, params Assembly[] assemblies)
{
services.AddScoped<IMediator, Mediator>();
services.AddScoped<ISender>(sp => sp.GetRequiredService<IMediator>());

services.RegisterAllImplementationsOf<IRequestDecorator>(assemblies);
services.RegisterAllImplementations<IInjectable>(assemblies);
services.RegisterAllImplementationsOf<IInjectableAndImplementations>(assemblies);

services.Scan(scan => scan
.FromAssemblies(assemblies)
Expand Down Expand Up @@ -78,7 +80,41 @@ public static IServiceCollection AddKSMediator(this IServiceCollection services,
return services;
}

public static IServiceCollection RegisterAllImplementationsOf<TInterface>(this IServiceCollection services,
private static IServiceCollection RegisterAllImplementationsOf<TInterface>(this IServiceCollection services,
Assembly[] assemblies,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
var interfaceType = typeof(TInterface);

foreach (var assembly in assemblies)
{
var implementations = assembly.DefinedTypes
.Where(type => type.IsClass && !type.IsAbstract && interfaceType.IsAssignableFrom(type))
.ToList();

foreach (var implementation in implementations)
{
switch (lifetime)
{
case ServiceLifetime.Transient:
services.AddTransient(interfaceType, implementation);
break;
case ServiceLifetime.Scoped:
services.AddScoped(interfaceType, implementation);
break;
case ServiceLifetime.Singleton:
services.AddSingleton(interfaceType, implementation);
break;
default:
throw new ArgumentException("Invalid service lifetime", nameof(lifetime));
}
}
}

return services;
}

private static IServiceCollection RegisterAllImplementations<TInterface>(this IServiceCollection services,
Assembly[] assemblies,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
Expand Down
6 changes: 0 additions & 6 deletions src/KSFramework/KSMessaging/IRequestDecorator.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddKSMediator(typeof(MediatorTests).Assembly);
services.AddKSFramework(typeof(MediatorTests).Assembly);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace KSFramework.UnitTests.Configuration;

/// <summary>
/// Tests for verifying notification handlers and behaviors registration through AddKSMediator.
/// Tests for verifying notification handlers and behaviors registration through AddKSFramework.
/// </summary>
public class AddKSMediatorNotificationTests
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public async Task AddKSMediator_Should_Register_NotificationHandler_And_Behavior
// Arrange
var services = new ServiceCollection();

services.AddKSMediator(typeof(TestNotification).Assembly);
services.AddKSFramework(typeof(TestNotification).Assembly);
services.AddSingleton<INotificationHandler<TestNotification>, TestNotificationHandler>();
services.AddSingleton<INotificationBehavior<TestNotification>, TestNotificationBehavior>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task AddKSMediator_RegistersHandlersAndBehaviorsFromAssembly()
// Arrange
var services = new ServiceCollection();

services.AddKSMediator(typeof(MultiplyByTwoRequest).Assembly);
services.AddKSFramework(typeof(MultiplyByTwoRequest).Assembly);

var provider = services.BuildServiceProvider();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace KSFramework.UnitTests.Mediator.Registration;

/// <summary>
/// Unit tests for AddKSMediator service registration.
/// Unit tests for AddKSFramework service registration.
/// </summary>
public class AddKSMediatorRegistrationTests
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task AddKSMediator_RegistersHandlersAndBehaviorsCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddKSMediator(typeof(AddKSMediatorRegistrationTests).Assembly);
services.AddKSFramework(typeof(AddKSMediatorRegistrationTests).Assembly);
var provider = services.BuildServiceProvider();

var mediator = provider.GetService<IMediator>();
Expand Down