Popularity
1.0
Growing
Activity
6.7
-
0
4
1

Programming language: C#
License: MIT License

AdaskoTheBeAsT.MediatR.SimpleInjector and AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore alternatives and similar packages

Based on the "Queue" category.
Alternatively, view AdaskoTheBeAsT.MediatR.SimpleInjector and AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of AdaskoTheBeAsT.MediatR.SimpleInjector and AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore or a related project?

Add another 'Queue' Package

README

AdaskoTheBeAsT.MediatR.SimpleInjector and AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore

MediatR extensions for SimpleInjector.

Badges

CodeFactor Total alerts Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Tests Sonar Test Count Sonar Test Execution Time Sonar Coverage Nuget

Usage in AspNetCore

Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the SimpleInjector container. Additionaly it register decorator which passes HttpContext.RequestAborted cancellation token from asp.net core controllers to MediatR.
Install package AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore.
There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

    container.AddMediatRAspNetCore(typeof(MyHandler), type2 /*, ...*/);
    
  2. Assembly which will be scanned

    container.AddMediatRAspNetCore(assembly, assembly2 /*, ...*/);
    
  3. Full configuration

    var testMediator = new Mock<IMediator>();
    container.AddMediatR(
        cfg =>
        {
            cfg.Using(() => testMediator.Object);
            cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
            cfg.UsingBuiltinPipelineProcessorBehaviors(true);
            cfg.UsingPipelineProcessorBehaviors(typeof(CustomPipelineBehavior<,>));
            cfg.UsingStreamPipelineBehaviors(typeof(CustomStreamPipelineBehavior<,>));
        });
    

Usage in other project types

Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the SimpleInjector container.
Install package AdaskoTheBeAsT.MediatR.SimpleInjector.
There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

    container.AddMediatR(typeof(MyHandler), type2 /*, ...*/);
    
  2. List of assemblies which will be scanned.

Below is sample for scanning assemblies from some solution.

```cs
[ExcludeFromCodeCoverage]
public static class MediatRConfigurator
{
    private const string NamespacePrefix = "YourNamespace";

    public static void Configure(Container container)
    {
        var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
        var assemblies = new List<Assembly>();
        var mainAssembly = typeof(MediatRConfigurator).GetTypeInfo().Assembly;
        var refAssemblies = mainAssembly.GetReferencedAssemblies();
        foreach (var assemblyName in refAssemblies
            .Where(a => a.FullName.StartsWith(NamespacePrefix, StringComparison.OrdinalIgnoreCase)))
            {
                var assembly = loadedAssemblies.Find(l => l.FullName == assemblyName.FullName)
                    ?? AppDomain.CurrentDomain.Load(assemblyName);
                assemblies.Add(assembly);
            }
        container.AddMediatR(assemblies);
    }
}
```

This will register:

  • IMediator as Singleton
  • IRequestHandler<> concrete implementations as Transient
  • INotificationHandler<> concrete implementations as Transient
  • IStreamRequestHandler<> concrete implementations as Transient

Advanced usage

Setting up custom IMediator instance and marker type from assembly for unit testing (Moq sample)

    var testMediator = new Mock<IMediator>();
    container.AddMediatR(
        cfg =>
        {
            cfg.Using(() => testMediator.Object);
            cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
        });

Setting up custom IMediator implementation and marker type from assembly

    container.AddMediatR(
        cfg =>
        {
            cfg.Using<MyCustomMediator>();
            cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
        });

Setting up custom IMediator implementation and assemblies to scan

    container.AddMediatR(
        cfg =>
        {
            cfg.Using<MyCustomMediator>();
            cfg.WithAssembliesToScan(assemblies);
        });

Setting assemblies to scan and different lifetime for IMediator implementation

    container.AddMediatR(
        cfg =>
        {
            cfg.WithAssembliesToScan(assemblies);
            cfg.AsScoped();
        });

Setting assemblies to scan and additonaly enabling all builtin behaviors and user defined processors/handlers

This will register following behaviors:

  • RequestPreProcessorBehavior<,>
  • RequestPostProcessorBehavior<,>
  • RequestExceptionProcessorBehavior<,>
  • RequestExceptionActionProcessorBehavior<,>

and all user defined implementation of processors and handlers:

  • IRequestPreProcessor<>
  • IRequestPostProcessor<,>
  • IRequestExceptionHandler<,,>
  • IRequestExceptionActionHandler<,>
    container.AddMediatR(
        cfg =>
        {
            cfg.WithAssembliesToScan(assemblies);
            cfg.UsingBuiltinPipelineProcessorBehaviors(true);
        });

Setting assemblies to scan and additonaly enabling choosen builtin behaviors and user defined processors/handlers

This will register following behaviors:

  • RequestPreProcessorBehavior<,>
  • RequestExceptionProcessorBehavior<,>

and all user defined implementation of processors and handlers:

  • IRequestPreProcessor<>
  • IRequestExceptionHandler<,,>
    container.AddMediatR(
        cfg =>
        {
            cfg.WithAssembliesToScan(assemblies);
            cfg.UsingBuiltinPipelineProcessorBehaviors(
                requestPreProcessorBehaviorEnabled: true,
                requestPostProcessorBehaviorEnabled: false,
                requestExceptionProcessorBehaviorEnabled: true,
                requestExceptionActionProcessorBehaviorEnabled: false);
        });

Setting assemblies to scan and additonaly custom stream request handlers behaviors

This will register following stream behaviors:

  • CustomStreamPipelineBehavior<,>
    container.AddMediatR(
        cfg =>
        {
            cfg.WithAssembliesToScan(assemblies);
            cfg.UsingStreamPipelineBehaviors(typeof(CustomStreamPipelineBehavior<,>));
        });

Setting assemblies to scan and additonaly enabling choosen builtin behaviors and user defined processors/handlers also with custom Pipeline Process Behaviours

    container.AddMediatR(
        cfg =>
        {
            cfg.WithAssembliesToScan(assemblies);
            cfg.UsingBuiltinPipelineProcessorBehaviors(
                requestPreProcessorBehaviorEnabled: true,
                requestPostProcessorBehaviorEnabled: false,
                requestExceptionProcessorBehaviorEnabled: true,
                requestExceptionActionProcessorBehaviorEnabled: false);
            cfg.UsingPipelineProcessorBehaviors(typeof(CustomPipelineBehavior<,>));
        });

Thanks to

Code originates from MediatR.Extensions.Microsoft.DependencyInjection and was changed to work with SimpleInjector.