Skip to content

Commit

Permalink
Removing MultiInstanceFactory; renaming SingleInstanceFactory to just…
Browse files Browse the repository at this point in the history
… ServiceFactory; main project compiles
  • Loading branch information
jbogard committed Apr 3, 2018
1 parent 771750b commit 3a2d8a5
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 80 deletions.
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
{
var services = new ServiceCollection();

services.AddScoped<SingleInstanceFactory>(p => p.GetRequiredService);
services.AddScoped<ServiceFactory>(p => p.GetRequiredService);
services.AddScoped<MultiInstanceFactory>(p => p.GetRequiredServices);

services.AddSingleton<TextWriter>(writer);
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.Autofac/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
builder.RegisterGeneric(typeof(ConstrainedRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>));
builder.RegisterGeneric(typeof(ConstrainedPingedHandler<>)).As(typeof(INotificationHandler<>));

builder.Register<SingleInstanceFactory>(ctx =>
builder.Register<ServiceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.DryIoc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
{
var container = new Container();

container.RegisterDelegate<SingleInstanceFactory>(r => r.Resolve);
container.RegisterDelegate<ServiceFactory>(r => r.Resolve);
container.RegisterDelegate<MultiInstanceFactory>(r => serviceType => r.ResolveMany(serviceType));
container.UseInstance<TextWriter>(writer);

Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.DryIocZero/Container.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ internal static object Get0_IRequestHandler(IResolverContext r)
// typeof(MediatR.IMediator)
internal static object Get1_IMediator(IResolverContext r)
{
return new MediatR.Mediator((MediatR.SingleInstanceFactory)r.Resolve(typeof(MediatR.SingleInstanceFactory), null, IfUnresolved.Throw, default(System.Type), Request.Empty.Push(typeof(MediatR.IMediator), default(System.Type), (object)null, 28, FactoryType.Service, typeof(MediatR.Mediator), Reuse.Transient, RequestFlags.IsResolutionCall), default(object[])), (MediatR.MultiInstanceFactory)r.Resolve(typeof(MediatR.MultiInstanceFactory), null, IfUnresolved.Throw, default(System.Type), Request.Empty.Push(typeof(MediatR.IMediator), default(System.Type), (object)null, 28, FactoryType.Service, typeof(MediatR.Mediator), Reuse.Transient, RequestFlags.IsResolutionCall), default(object[])));
return new MediatR.Mediator((MediatR.ServiceFactory)r.Resolve(typeof(MediatR.ServiceFactory), null, IfUnresolved.Throw, default(System.Type), Request.Empty.Push(typeof(MediatR.IMediator), default(System.Type), (object)null, 28, FactoryType.Service, typeof(MediatR.Mediator), Reuse.Transient, RequestFlags.IsResolutionCall), default(object[])), (MediatR.MultiInstanceFactory)r.Resolve(typeof(MediatR.MultiInstanceFactory), null, IfUnresolved.Throw, default(System.Type), Request.Empty.Push(typeof(MediatR.IMediator), default(System.Type), (object)null, 28, FactoryType.Service, typeof(MediatR.Mediator), Reuse.Transient, RequestFlags.IsResolutionCall), default(object[])));
}

// typeof(MediatR.INotification)
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.DryIocZero/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static IMediator BuildMediator(TextWriter writer)
{
var container = new Container();

container.RegisterDelegate<SingleInstanceFactory>(r => r.Resolve);
container.RegisterDelegate<ServiceFactory>(r => r.Resolve);
container.RegisterDelegate<MultiInstanceFactory>(r => r.ResolveMany);
container.UseInstance(writer);

Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.Lamar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
// This is the default but let's be explicit. At most we should be container scoped.
cfg.For<IMediator>().Use<Mediator>().Transient();

cfg.For<SingleInstanceFactory>().Use(ctx => ctx.GetInstance);
cfg.For<ServiceFactory>().Use(ctx => ctx.GetInstance);
cfg.For<MultiInstanceFactory>().Use(ctx => type => ctx.GetAllInstances(type).Cast<object>());
cfg.For<TextWriter>().Use(writer);
});
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.LightInject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
serviceContainer.Register(typeof(IRequestPreProcessor<>), typeof(GenericRequestPreProcessor<>));


serviceContainer.Register<SingleInstanceFactory>(fac => fac.GetInstance);
serviceContainer.Register<ServiceFactory>(fac => fac.GetInstance);
serviceContainer.Register<MultiInstanceFactory>(fac => fac.GetAllInstances);
return serviceContainer.GetInstance<IMediator>();
}
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.Ninject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
kernel.Bind(typeof(IRequestPostProcessor<,>)).To(typeof(ConstrainedRequestPostProcessor<,>));
kernel.Bind(typeof(INotificationHandler<>)).To(typeof(ConstrainedPingedHandler<>)).WhenNotificationMatchesType<Pinged>();

kernel.Bind<SingleInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.TryGet(t));
kernel.Bind<ServiceFactory>().ToMethod(ctx => t => ctx.Kernel.TryGet(t));
kernel.Bind<MultiInstanceFactory>().ToMethod(ctx => t =>
{
try
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.SimpleInjector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
container.RegisterCollection(typeof(IRequestPreProcessor<>), new [] { typeof(GenericRequestPreProcessor<>) });
container.RegisterCollection(typeof(IRequestPostProcessor<,>), new[] { typeof(GenericRequestPostProcessor<,>), typeof(ConstrainedRequestPostProcessor<,>) });

container.RegisterSingleton(new SingleInstanceFactory(container.GetInstance));
container.RegisterSingleton(new ServiceFactory(container.GetInstance));
container.RegisterSingleton(new MultiInstanceFactory(container.GetAllInstances));

container.Verify();
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.StructureMap/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
// This is the default but let's be explicit. At most we should be container scoped.
cfg.For<IMediator>().LifecycleIs<TransientLifecycle>().Use<Mediator>();

cfg.For<SingleInstanceFactory>().Use<SingleInstanceFactory>(ctx => ctx.GetInstance);
cfg.For<ServiceFactory>().Use<ServiceFactory>(ctx => ctx.GetInstance);
cfg.For<MultiInstanceFactory>().Use<MultiInstanceFactory>(ctx => ctx.GetAllInstances);
cfg.For<TextWriter>().Use(writer);
});
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.Unity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static class IUnityContainerExtensions
public static IUnityContainer RegisterMediator(this IUnityContainer container, LifetimeManager lifetimeManager)
{
return container.RegisterType<IMediator, Mediator>(lifetimeManager)
.RegisterInstance<SingleInstanceFactory>(t => container.IsRegistered(t) ? container.Resolve(t) : null)
.RegisterInstance<ServiceFactory>(t => container.IsRegistered(t) ? container.Resolve(t) : null)
.RegisterInstance<MultiInstanceFactory>(t =>
{
var allHandlers = container.ResolveAll(t).ToList();
Expand Down
2 changes: 1 addition & 1 deletion samples/MediatR.Examples.Windsor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static IMediator BuildMediator(WrappingWriter writer)

container.Register(Component.For<IMediator>().ImplementedBy<Mediator>());
container.Register(Component.For<TextWriter>().Instance(writer));
container.Register(Component.For<SingleInstanceFactory>().UsingFactoryMethod<SingleInstanceFactory>(k => k.Resolve));
container.Register(Component.For<ServiceFactory>().UsingFactoryMethod<ServiceFactory>(k => k.Resolve));
container.Register(Component.For<MultiInstanceFactory>().UsingFactoryMethod<MultiInstanceFactory>(k => t => (IEnumerable<object>)k.ResolveAll(t)));

//Pipeline
Expand Down
8 changes: 4 additions & 4 deletions src/MediatR/Internal/NotificationHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace MediatR.Internal

internal abstract class NotificationHandlerWrapper
{
public abstract Task Handle(INotification notification, CancellationToken cancellationToken, MultiInstanceFactory multiInstanceFactory, Func<IEnumerable<Task>, Task> publish);
public abstract Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory, Func<IEnumerable<Task>, Task> publish);
}

internal class NotificationHandlerWrapperImpl<TNotification> : NotificationHandlerWrapper
where TNotification : INotification
{
public override Task Handle(INotification notification, CancellationToken cancellationToken, MultiInstanceFactory multiInstanceFactory, Func<IEnumerable<Task>, Task> publish)
public override Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory, Func<IEnumerable<Task>, Task> publish)
{
var handlers = multiInstanceFactory(typeof(INotificationHandler<TNotification>))
.Cast<INotificationHandler<TNotification>>()
var handlers = serviceFactory
.GetInstances<INotificationHandler<TNotification>>()
.Select(x => x.Handle((TNotification)notification, cancellationToken));

return publish(handlers);
Expand Down
26 changes: 14 additions & 12 deletions src/MediatR/Internal/RequestHandlerWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace MediatR.Internal
{
using System;
Expand All @@ -7,13 +9,13 @@ namespace MediatR.Internal

internal abstract class RequestHandlerBase
{
protected static THandler GetHandler<THandler>(SingleInstanceFactory factory)
protected static THandler GetHandler<THandler>(ServiceFactory factory)
{
THandler handler;

try
{
handler = (THandler)factory(typeof(THandler));
handler = factory.GetInstance<THandler>();
}
catch (Exception e)
{
Expand All @@ -32,25 +34,25 @@ protected static THandler GetHandler<THandler>(SingleInstanceFactory factory)
internal abstract class RequestHandlerWrapper : RequestHandlerBase
{
public abstract Task Handle(IRequest request, CancellationToken cancellationToken,
SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory);
ServiceFactory serviceFactory);
}

internal abstract class RequestHandlerWrapper<TResponse> : RequestHandlerBase
{
public abstract Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory);
ServiceFactory serviceFactory);
}

internal class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWrapper<TResponse>
where TRequest : IRequest<TResponse>
{
public override Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory)
ServiceFactory serviceFactory)
{
Task<TResponse> Handler() => GetHandler<IRequestHandler<TRequest, TResponse>>(singleFactory).Handle((TRequest) request, cancellationToken);
Task<TResponse> Handler() => GetHandler<IRequestHandler<TRequest, TResponse>>(serviceFactory).Handle((TRequest) request, cancellationToken);

return multiFactory(typeof(IPipelineBehavior<TRequest, TResponse>))
.Cast<IPipelineBehavior<TRequest, TResponse>>()
return serviceFactory
.GetInstances<IPipelineBehavior<TRequest, TResponse>>()
.Reverse()
.Aggregate((RequestHandlerDelegate<TResponse>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next))();
}
Expand All @@ -60,16 +62,16 @@ internal class RequestHandlerWrapperImpl<TRequest> : RequestHandlerWrapper
where TRequest : IRequest
{
public override Task Handle(IRequest request, CancellationToken cancellationToken,
SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory)
ServiceFactory serviceFactory)
{
async Task<Unit> Handler()
{
await GetHandler<IRequestHandler<TRequest>>(singleFactory).Handle((TRequest) request, cancellationToken).ConfigureAwait(false);
await GetHandler<IRequestHandler<TRequest>>(serviceFactory).Handle((TRequest) request, cancellationToken).ConfigureAwait(false);
return Unit.Value;
}

return multiFactory(typeof(IPipelineBehavior<TRequest, Unit>))
.Cast<IPipelineBehavior<TRequest, Unit>>()
return serviceFactory
.GetInstances<IPipelineBehavior<TRequest, Unit>>()
.Reverse()
.Aggregate((RequestHandlerDelegate<Unit>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next))();
}
Expand Down
17 changes: 7 additions & 10 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ namespace MediatR
/// </summary>
public class Mediator : IMediator
{
private readonly SingleInstanceFactory _singleInstanceFactory;
private readonly MultiInstanceFactory _multiInstanceFactory;
private readonly ServiceFactory _serviceFactory;
private static readonly ConcurrentDictionary<Type, RequestHandlerWrapper> _voidRequestHandlers = new ConcurrentDictionary<Type, RequestHandlerWrapper>();
private static readonly ConcurrentDictionary<Type, object> _requestHandlers = new ConcurrentDictionary<Type, object>();
private static readonly ConcurrentDictionary<Type, NotificationHandlerWrapper> _notificationHandlers = new ConcurrentDictionary<Type, NotificationHandlerWrapper>();

/// <summary>
/// Initializes a new instance of the <see cref="Mediator"/> class.
/// </summary>
/// <param name="singleInstanceFactory">The single instance factory.</param>
/// <param name="multiInstanceFactory">The multi instance factory.</param>
public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactory multiInstanceFactory)
/// <param name="serviceFactory">The single instance factory.</param>
public Mediator(ServiceFactory serviceFactory)
{
_singleInstanceFactory = singleInstanceFactory;
_multiInstanceFactory = multiInstanceFactory;
_serviceFactory = serviceFactory;
}

public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -41,7 +38,7 @@ public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactor
var handler = (RequestHandlerWrapper<TResponse>)_requestHandlers.GetOrAdd(requestType,
t => Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse))));

return handler.Handle(request, cancellationToken, _singleInstanceFactory, _multiInstanceFactory);
return handler.Handle(request, cancellationToken, _serviceFactory);
}

public Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -56,7 +53,7 @@ public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactor
var handler = _voidRequestHandlers.GetOrAdd(requestType,
t => (RequestHandlerWrapper) Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType)));

return handler.Handle(request, cancellationToken, _singleInstanceFactory, _multiInstanceFactory);
return handler.Handle(request, cancellationToken, _serviceFactory);
}

public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -71,7 +68,7 @@ public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactor
var handler = _notificationHandlers.GetOrAdd(notificationType,
t => (NotificationHandlerWrapper)Activator.CreateInstance(typeof(NotificationHandlerWrapperImpl<>).MakeGenericType(notificationType)));

return handler.Handle(notification, cancellationToken, _multiInstanceFactory, PublishCore);
return handler.Handle(notification, cancellationToken, _serviceFactory, PublishCore);
}

/// <summary>
Expand Down
13 changes: 0 additions & 13 deletions src/MediatR/MultiInstanceFactory.cs

This file was deleted.

21 changes: 21 additions & 0 deletions src/MediatR/ServiceFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;

namespace MediatR
{
/// <summary>
/// Factory method used to resolve all services. For multiple instances, it will resolve against <see cref="IEnumerable{T}" />
/// </summary>
/// <param name="serviceType">Type of service to resolve</param>
/// <returns>An instance of type <paramref name="serviceType" /></returns>
public delegate object ServiceFactory(Type serviceType);

public static class ServiceFactoryExtensions
{
public static T GetInstance<T>(this ServiceFactory factory)
=> (T) factory(typeof(T));

public static IEnumerable<T> GetInstances<T>(this ServiceFactory factory)
=> (IEnumerable<T>) factory(typeof(IEnumerable<T>));
}
}
12 changes: 0 additions & 12 deletions src/MediatR/SingleInstanceFactory.cs

This file was deleted.

Loading

0 comments on commit 3a2d8a5

Please sign in to comment.