Skip to content

Commit

Permalink
Merge pull request #807 from jbogard/fixing-samples
Browse files Browse the repository at this point in the history
Fixing samples
  • Loading branch information
jbogard authored Nov 30, 2022
2 parents 963df38 + c1d7e89 commit 09dbb3e
Show file tree
Hide file tree
Showing 52 changed files with 2,031 additions and 4 deletions.
65 changes: 65 additions & 0 deletions MediatR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Benchmarks", "test\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Contracts", "src\MediatR.Contracts\MediatR.Contracts.csproj", "{87F5A238-44B5-4769-82E5-E68B712D6E6D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E372BF0B-90E8-4DC1-A332-F023095A3C2A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples", "samples\MediatR.Examples\MediatR.Examples.csproj", "{FB683115-7F96-4529-A412-65A59CC991E4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.AspNetCore", "samples\MediatR.Examples.AspNetCore\MediatR.Examples.AspNetCore.csproj", "{7B6838F9-3CAC-45E8-A796-CA34B05B008F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Autofac", "samples\MediatR.Examples.Autofac\MediatR.Examples.Autofac.csproj", "{E7EF3E3E-BD58-482E-B293-26CA42C30D3E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.DryIoc", "samples\MediatR.Examples.DryIoc\MediatR.Examples.DryIoc.csproj", "{085B6925-A438-4DBD-82C4-B3006A798C24}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Lamar", "samples\MediatR.Examples.Lamar\MediatR.Examples.Lamar.csproj", "{A3E2B050-B636-46C4-8563-CEE18EB6B146}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.LightInject", "samples\MediatR.Examples.LightInject\MediatR.Examples.LightInject.csproj", "{738B6CF9-C5B0-424C-A63F-EE601DD2A266}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.PublishStrategies", "samples\MediatR.Examples.PublishStrategies\MediatR.Examples.PublishStrategies.csproj", "{004D029A-43E7-47B0-BA74-D0A9F7FC7713}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.SimpleInjector", "samples\MediatR.Examples.SimpleInjector\MediatR.Examples.SimpleInjector.csproj", "{7CEB57F2-B6DC-4A18-A040-D12555C3D32F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Stashbox", "samples\MediatR.Examples.Stashbox\MediatR.Examples.Stashbox.csproj", "{F9148E20-5856-484C-8410-B515C6C56214}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -49,6 +69,42 @@ Global
{87F5A238-44B5-4769-82E5-E68B712D6E6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87F5A238-44B5-4769-82E5-E68B712D6E6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87F5A238-44B5-4769-82E5-E68B712D6E6D}.Release|Any CPU.Build.0 = Release|Any CPU
{FB683115-7F96-4529-A412-65A59CC991E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB683115-7F96-4529-A412-65A59CC991E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB683115-7F96-4529-A412-65A59CC991E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB683115-7F96-4529-A412-65A59CC991E4}.Release|Any CPU.Build.0 = Release|Any CPU
{7B6838F9-3CAC-45E8-A796-CA34B05B008F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B6838F9-3CAC-45E8-A796-CA34B05B008F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B6838F9-3CAC-45E8-A796-CA34B05B008F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B6838F9-3CAC-45E8-A796-CA34B05B008F}.Release|Any CPU.Build.0 = Release|Any CPU
{E7EF3E3E-BD58-482E-B293-26CA42C30D3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7EF3E3E-BD58-482E-B293-26CA42C30D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7EF3E3E-BD58-482E-B293-26CA42C30D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7EF3E3E-BD58-482E-B293-26CA42C30D3E}.Release|Any CPU.Build.0 = Release|Any CPU
{085B6925-A438-4DBD-82C4-B3006A798C24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{085B6925-A438-4DBD-82C4-B3006A798C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{085B6925-A438-4DBD-82C4-B3006A798C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{085B6925-A438-4DBD-82C4-B3006A798C24}.Release|Any CPU.Build.0 = Release|Any CPU
{A3E2B050-B636-46C4-8563-CEE18EB6B146}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3E2B050-B636-46C4-8563-CEE18EB6B146}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3E2B050-B636-46C4-8563-CEE18EB6B146}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3E2B050-B636-46C4-8563-CEE18EB6B146}.Release|Any CPU.Build.0 = Release|Any CPU
{738B6CF9-C5B0-424C-A63F-EE601DD2A266}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{738B6CF9-C5B0-424C-A63F-EE601DD2A266}.Debug|Any CPU.Build.0 = Debug|Any CPU
{738B6CF9-C5B0-424C-A63F-EE601DD2A266}.Release|Any CPU.ActiveCfg = Release|Any CPU
{738B6CF9-C5B0-424C-A63F-EE601DD2A266}.Release|Any CPU.Build.0 = Release|Any CPU
{004D029A-43E7-47B0-BA74-D0A9F7FC7713}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{004D029A-43E7-47B0-BA74-D0A9F7FC7713}.Debug|Any CPU.Build.0 = Debug|Any CPU
{004D029A-43E7-47B0-BA74-D0A9F7FC7713}.Release|Any CPU.ActiveCfg = Release|Any CPU
{004D029A-43E7-47B0-BA74-D0A9F7FC7713}.Release|Any CPU.Build.0 = Release|Any CPU
{7CEB57F2-B6DC-4A18-A040-D12555C3D32F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CEB57F2-B6DC-4A18-A040-D12555C3D32F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CEB57F2-B6DC-4A18-A040-D12555C3D32F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CEB57F2-B6DC-4A18-A040-D12555C3D32F}.Release|Any CPU.Build.0 = Release|Any CPU
{F9148E20-5856-484C-8410-B515C6C56214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9148E20-5856-484C-8410-B515C6C56214}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9148E20-5856-484C-8410-B515C6C56214}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9148E20-5856-484C-8410-B515C6C56214}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -58,6 +114,15 @@ Global
{4FB0CFC4-90E3-467F-9704-6FBF637F9B4B} = {962C5ACA-AB2B-4E9B-9EBB-7E7EE28CDBB1}
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD} = {962C5ACA-AB2B-4E9B-9EBB-7E7EE28CDBB1}
{87F5A238-44B5-4769-82E5-E68B712D6E6D} = {6267E2ED-942C-497D-BFC9-B3CE0AFC276F}
{FB683115-7F96-4529-A412-65A59CC991E4} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{7B6838F9-3CAC-45E8-A796-CA34B05B008F} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{E7EF3E3E-BD58-482E-B293-26CA42C30D3E} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{085B6925-A438-4DBD-82C4-B3006A798C24} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{A3E2B050-B636-46C4-8563-CEE18EB6B146} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{738B6CF9-C5B0-424C-A63F-EE601DD2A266} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{004D029A-43E7-47B0-BA74-D0A9F7FC7713} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{7CEB57F2-B6DC-4A18-A040-D12555C3D32F} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
{F9148E20-5856-484C-8410-B515C6C56214} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D58286E3-878B-4ACB-8E76-F61E708D4339}
Expand Down
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>
38 changes: 38 additions & 0 deletions samples/MediatR.Examples.AspNetCore/Program.cs
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 samples/MediatR.Examples.Autofac/MediatR.Examples.Autofac.csproj
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>
91 changes: 91 additions & 0 deletions samples/MediatR.Examples.Autofac/Program.cs
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 samples/MediatR.Examples.DryIoc/MediatR.Examples.DryIoc.csproj
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>
36 changes: 36 additions & 0 deletions samples/MediatR.Examples.DryIoc/Program.cs
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 samples/MediatR.Examples.Lamar/MediatR.Examples.Lamar.csproj
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>
56 changes: 56 additions & 0 deletions samples/MediatR.Examples.Lamar/Program.cs
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;
}
}
Loading

0 comments on commit 09dbb3e

Please sign in to comment.