-
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.
Merge pull request #807 from jbogard/fixing-samples
Fixing samples
- Loading branch information
Showing
52 changed files
with
2,031 additions
and
4 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
16 changes: 16 additions & 0 deletions
16
samples/MediatR.Examples.AspNetCore/MediatR.Examples.AspNetCore.csproj
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MediatR.Examples\MediatR.Examples.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using MediatR.Pipeline; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
|
||
namespace MediatR.Examples.AspNetCore; | ||
|
||
public static class Program | ||
{ | ||
public static Task Main(string[] args) | ||
{ | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
return Runner.Run(mediator, writer, "ASP.NET Core DI", testStreams: true); | ||
} | ||
|
||
private static IMediator BuildMediator(WrappingWriter writer) | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddSingleton<TextWriter>(writer); | ||
|
||
services.AddMediatR(typeof(Ping), typeof(Sing)); | ||
|
||
services.AddScoped(typeof(IStreamRequestHandler<Sing, Song>), typeof(SingHandler)); | ||
|
||
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(GenericPipelineBehavior<,>)); | ||
services.AddScoped(typeof(IRequestPreProcessor<>), typeof(GenericRequestPreProcessor<>)); | ||
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(GenericRequestPostProcessor<,>)); | ||
services.AddScoped(typeof(IStreamPipelineBehavior<,>), typeof(GenericStreamPipelineBehavior<,>)); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
return provider.GetRequiredService<IMediator>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/MediatR.Examples.Autofac/MediatR.Examples.Autofac.csproj
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MediatR.Examples\MediatR.Examples.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Autofac" Version="6.5.0" /> | ||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using Autofac.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
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 | ||
{ | ||
public static Task Main(string[] args) | ||
{ | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
|
||
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(); | ||
|
||
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(); | ||
} | ||
|
||
builder.RegisterInstance(writer).As<TextWriter>(); | ||
|
||
// 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<>)); | ||
|
||
|
||
var services = new ServiceCollection(); | ||
|
||
builder.Populate(services); | ||
|
||
// The below returns: | ||
// - RequestPreProcessorBehavior | ||
// - RequestPostProcessorBehavior | ||
// - GenericPipelineBehavior | ||
// - GenericStreamPipelineBehavior | ||
// - RequestExceptionActionProcessorBehavior | ||
// - RequestExceptionProcessorBehavior | ||
|
||
//var behaviors = container | ||
// .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>() | ||
// .ToList(); | ||
|
||
var container = builder.Build(); | ||
var serviceProvider = new AutofacServiceProvider(container); | ||
var mediator = serviceProvider.GetRequiredService<IMediator>(); | ||
|
||
return mediator; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
samples/MediatR.Examples.DryIoc/MediatR.Examples.DryIoc.csproj
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MediatR.Examples\MediatR.Examples.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DryIoc; | ||
using DryIoc.Microsoft.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MediatR.Examples.DryIoc; | ||
|
||
class Program | ||
{ | ||
static Task Main() | ||
{ | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
|
||
return Runner.Run(mediator, writer, "DryIoc"); | ||
} | ||
|
||
private static IMediator BuildMediator(WrappingWriter writer) | ||
{ | ||
var container = new Container(); | ||
|
||
container.Use<TextWriter>(writer); | ||
|
||
//Pipeline works out of the box here | ||
|
||
container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, Registrator.Interfaces); | ||
|
||
var services = new ServiceCollection(); | ||
|
||
var adapterContainer = container.WithDependencyInjectionAdapter(services); | ||
|
||
return adapterContainer.GetRequiredService<IMediator>(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
samples/MediatR.Examples.Lamar/MediatR.Examples.Lamar.csproj
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Lamar" Version="8.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MediatR.Examples\MediatR.Examples.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Lamar; | ||
using MediatR.Pipeline; | ||
|
||
namespace MediatR.Examples.Lamar; | ||
|
||
class Program | ||
{ | ||
static Task Main(string[] args) | ||
{ | ||
var writer = new WrappingWriter(Console.Out); | ||
var mediator = BuildMediator(writer); | ||
|
||
return Runner.Run(mediator, writer, "Lamar"); | ||
} | ||
|
||
private static IMediator BuildMediator(WrappingWriter writer) | ||
{ | ||
var container = new Container(cfg => | ||
{ | ||
cfg.Scan(scanner => | ||
{ | ||
scanner.AssemblyContainingType<Ping>(); | ||
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>)); | ||
scanner.ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>)); | ||
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestExceptionAction<>)); | ||
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestExceptionHandler<,,>)); | ||
}); | ||
|
||
//Pipeline | ||
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(RequestExceptionProcessorBehavior<,>)); | ||
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(RequestExceptionActionProcessorBehavior<,>)); | ||
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(RequestPreProcessorBehavior<,>)); | ||
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(RequestPostProcessorBehavior<,>)); | ||
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(GenericPipelineBehavior<,>)); | ||
cfg.For(typeof(IRequestPreProcessor<>)).Add(typeof(GenericRequestPreProcessor<>)); | ||
cfg.For(typeof(IRequestPostProcessor<,>)).Add(typeof(GenericRequestPostProcessor<,>)); | ||
cfg.For(typeof(IRequestPostProcessor<,>)).Add(typeof(ConstrainedRequestPostProcessor<,>)); | ||
|
||
//Constrained notification handlers | ||
cfg.For(typeof(INotificationHandler<>)).Add(typeof(ConstrainedPingedHandler<>)); | ||
|
||
// This is the default but let's be explicit. At most we should be container scoped. | ||
cfg.For<IMediator>().Use<Mediator>().Transient(); | ||
|
||
cfg.For<TextWriter>().Use(writer); | ||
}); | ||
|
||
|
||
var mediator = container.GetInstance<IMediator>(); | ||
|
||
return mediator; | ||
} | ||
} |
Oops, something went wrong.