-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
91 changed files
with
4,272 additions
and
4,366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,86 @@ | ||
namespace MediatR.Examples.Autofac | ||
{ | ||
using global::Autofac; | ||
using MediatR.Pipeline; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
namespace MediatR.Examples.Autofac; | ||
|
||
using global::Autofac; | ||
using MediatR.Pipeline; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
internal static class Program | ||
internal static class Program | ||
{ | ||
public static Task Main(string[] args) | ||
{ | ||
public static Task Main(string[] args) | ||
{ | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
|
||
return Runner.Run(mediator, writer, "Autofac", testStreams: true); | ||
} | ||
return Runner.Run(mediator, writer, "Autofac", testStreams: true); | ||
} | ||
|
||
private static IMediator BuildMediator(WrappingWriter writer) | ||
{ | ||
var builder = new ContainerBuilder(); | ||
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces(); | ||
private static IMediator BuildMediator(WrappingWriter writer) | ||
{ | ||
var builder = new ContainerBuilder(); | ||
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces(); | ||
|
||
var mediatrOpenTypes = new[] | ||
{ | ||
typeof(IRequestHandler<,>), | ||
typeof(IRequestExceptionHandler<,,>), | ||
typeof(IRequestExceptionAction<,>), | ||
typeof(INotificationHandler<>), | ||
typeof(IStreamRequestHandler<,>) | ||
}; | ||
var mediatrOpenTypes = new[] | ||
{ | ||
typeof(IRequestHandler<,>), | ||
typeof(IRequestExceptionHandler<,,>), | ||
typeof(IRequestExceptionAction<,>), | ||
typeof(INotificationHandler<>), | ||
typeof(IStreamRequestHandler<,>) | ||
}; | ||
|
||
foreach (var mediatrOpenType in mediatrOpenTypes) | ||
{ | ||
builder | ||
.RegisterAssemblyTypes(typeof(Ping).GetTypeInfo().Assembly) | ||
.AsClosedTypesOf(mediatrOpenType) | ||
// when having a single class implementing several handler types | ||
// this call will cause a handler to be called twice | ||
// in general you should try to avoid having a class implementing for instance `IRequestHandler<,>` and `INotificationHandler<>` | ||
// the other option would be to remove this call | ||
// see also https://github.com/jbogard/MediatR/issues/462 | ||
.AsImplementedInterfaces(); | ||
} | ||
foreach (var mediatrOpenType in mediatrOpenTypes) | ||
{ | ||
builder | ||
.RegisterAssemblyTypes(typeof(Ping).GetTypeInfo().Assembly) | ||
.AsClosedTypesOf(mediatrOpenType) | ||
// when having a single class implementing several handler types | ||
// this call will cause a handler to be called twice | ||
// in general you should try to avoid having a class implementing for instance `IRequestHandler<,>` and `INotificationHandler<>` | ||
// the other option would be to remove this call | ||
// see also https://github.com/jbogard/MediatR/issues/462 | ||
.AsImplementedInterfaces(); | ||
} | ||
|
||
builder.RegisterInstance(writer).As<TextWriter>(); | ||
builder.RegisterInstance(writer).As<TextWriter>(); | ||
|
||
// It appears Autofac returns the last registered types first | ||
builder.RegisterGeneric(typeof(GenericStreamPipelineBehavior<,>)).As(typeof(IStreamPipelineBehavior<,>)); | ||
// It appears Autofac returns the last registered types first | ||
builder.RegisterGeneric(typeof(GenericStreamPipelineBehavior<,>)).As(typeof(IStreamPipelineBehavior<,>)); | ||
|
||
builder.RegisterGeneric(typeof(RequestPostProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestPreProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestExceptionActionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestExceptionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(GenericRequestPreProcessor<>)).As(typeof(IRequestPreProcessor<>)); | ||
builder.RegisterGeneric(typeof(GenericRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>)); | ||
builder.RegisterGeneric(typeof(GenericPipelineBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(ConstrainedRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>)); | ||
builder.RegisterGeneric(typeof(ConstrainedPingedHandler<>)).As(typeof(INotificationHandler<>)); | ||
builder.RegisterGeneric(typeof(RequestPostProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestPreProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestExceptionActionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(RequestExceptionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(GenericRequestPreProcessor<>)).As(typeof(IRequestPreProcessor<>)); | ||
builder.RegisterGeneric(typeof(GenericRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>)); | ||
builder.RegisterGeneric(typeof(GenericPipelineBehavior<,>)).As(typeof(IPipelineBehavior<,>)); | ||
builder.RegisterGeneric(typeof(ConstrainedRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>)); | ||
builder.RegisterGeneric(typeof(ConstrainedPingedHandler<>)).As(typeof(INotificationHandler<>)); | ||
|
||
builder.Register<ServiceFactory>(ctx => | ||
{ | ||
var c = ctx.Resolve<IComponentContext>(); | ||
return t => c.Resolve(t); | ||
}); | ||
builder.Register<ServiceFactory>(ctx => | ||
{ | ||
var c = ctx.Resolve<IComponentContext>(); | ||
return t => c.Resolve(t); | ||
}); | ||
|
||
var container = builder.Build(); | ||
var container = builder.Build(); | ||
|
||
// The below returns: | ||
// - RequestPreProcessorBehavior | ||
// - RequestPostProcessorBehavior | ||
// - GenericPipelineBehavior | ||
// - GenericStreamPipelineBehavior | ||
// - RequestExceptionActionProcessorBehavior | ||
// - RequestExceptionProcessorBehavior | ||
// The below returns: | ||
// - RequestPreProcessorBehavior | ||
// - RequestPostProcessorBehavior | ||
// - GenericPipelineBehavior | ||
// - GenericStreamPipelineBehavior | ||
// - RequestExceptionActionProcessorBehavior | ||
// - RequestExceptionProcessorBehavior | ||
|
||
//var behaviors = container | ||
// .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>() | ||
// .ToList(); | ||
//var behaviors = container | ||
// .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>() | ||
// .ToList(); | ||
|
||
var mediator = container.Resolve<IMediator>(); | ||
var mediator = container.Resolve<IMediator>(); | ||
|
||
return mediator; | ||
} | ||
return mediator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.