Skip to content

Commit

Permalink
Allow AddMediatR to run with empty assemblies array and change AddMed…
Browse files Browse the repository at this point in the history
…iatRClasses to be an extension
  • Loading branch information
FuncLun committed Oct 20, 2019
1 parent ab26360 commit 695622b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ namespace MediatR.Registration
{
public static class ServiceRegistrar
{
public static void AddMediatRClasses(IServiceCollection services, IEnumerable<Assembly> assembliesToScan)

public static void AddMediatRClasses(this IServiceCollection services, params Assembly[] assembliesToScan)
=> services.AddMediatRClasses((IEnumerable<Assembly>) assembliesToScan);

public static void AddMediatRClasses(this IServiceCollection services, IEnumerable<Assembly> assembliesToScan)
{
assembliesToScan = (assembliesToScan as Assembly[] ?? assembliesToScan).Distinct().ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ namespace MediatR
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers mediator types only. Handlers must be registered separately, possibly with <see cref="AddMediatRClasses(IServiceCollection, Assembly[])"/>.
/// </summary>
/// <param name="services">Service collection</param>
/// <returns>Service collection</returns>
public static IServiceCollection AddMediatR(this IServiceCollection services)
=> services.AddMediatR(new Assembly[0], configuration: null);


/// <summary>
/// Registers handlers and mediator types from the specified assemblies
/// </summary>
Expand Down Expand Up @@ -47,10 +56,6 @@ public static IServiceCollection AddMediatR(this IServiceCollection services, Ac
/// <returns>Service collection</returns>
public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable<Assembly> assemblies, Action<MediatRServiceConfiguration> configuration)
{
if (!assemblies.Any())
{
throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
}
var serviceConfig = new MediatRServiceConfiguration();

configuration?.Invoke(serviceConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.Extensions.DependencyInjection;
using MediatR.Registration;
using Microsoft.Extensions.DependencyInjection;

namespace MediatR.Extensions.Microsoft.DependencyInjection.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Shouldly;
Expand Down Expand Up @@ -45,13 +47,33 @@ public void ShouldResolveNotificationHandlers()
}

[Fact]
public void ShouldRequireAtLeastOneAssembly()
public void ShouldNotRequireAtLeastOneAssembly()
{
var services = new ServiceCollection();

Action registration = () => services.AddMediatR(new Type[0]);

registration.ShouldThrow<ArgumentException>();
registration.ShouldNotThrow();
}

[Fact]
public void ShouldResolveHandlerWithParamsRegistration()
{
var services = new ServiceCollection();
services.AddMediatR();
services.AddMediatRClasses(typeof(Ping).GetTypeInfo().Assembly);

_provider.GetService<IRequestHandler<Ping, Pong>>().ShouldNotBeNull();
}

[Fact]
public void ShouldResolveHandlerWithEnumerableRegistration()
{
var services = new ServiceCollection();
services.AddMediatR();
services.AddMediatRClasses(new List<Assembly>() { typeof(Ping).GetTypeInfo().Assembly });

_provider.GetService<IRequestHandler<Ping, Pong>>().ShouldNotBeNull();
}
}
}

0 comments on commit 695622b

Please sign in to comment.