Ejemplo n.º 1
0
        public static Task RunAsync(IMediator mediator, WrappingWriter writer, string projectName)
        {
            if (mediator is null)
            {
                throw new ArgumentNullException(nameof(mediator));
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            return(RunInternalAsync(mediator, writer, projectName));
        }
Ejemplo n.º 2
0
        private static IMediator BuildMediator(WrappingWriter writer)
        {
            container = new Container();
            container.RegisterInstance(typeof(TextWriter), writer);
            container.Register(typeof(IRequestPostProcessor <,>), typeof(ConstrainedRequestPostProcessor <,>));
            container.Register(typeof(INotificationHandler <>), typeof(ConstrainedPingedHandler <>));

            container.AddMediatR(
                config =>
            {
                config.WithHandlerAssemblyMarkerTypes(typeof(Ping));
                config.UsingBuiltinPipelineProcessorBehaviors(true);
                config.UsingPipelineProcessorBehaviors(typeof(GenericPipelineBehavior <,>));
            });

            foreach (var service in container.GetCurrentRegistrations())
            {
                Console.WriteLine(service.ServiceType + " - " + service.Registration.ImplementationType);
            }

            return(container.GetInstance <IMediator>());
        }
Ejemplo n.º 3
0
        public static async Task Run(IMediator mediator, WrappingWriter writer, string projectName)
        {
            await writer.WriteLineAsync("===============");

            await writer.WriteLineAsync(projectName);

            await writer.WriteLineAsync("===============");

            await writer.WriteLineAsync("Sending Ping...");

            var pong = await mediator.Send(new Ping { Message = "Ping" });

            await writer.WriteLineAsync("Received: " + pong.Message);

            await writer.WriteLineAsync("Publishing Pinged...");

            await mediator.Publish(new Pinged());

            await writer.WriteLineAsync("Publishing Ponged...");

            var failedPong = false;

            try
            {
                await mediator.Publish(new Ponged());
            }
            catch (Exception e)
            {
                failedPong = true;
                await writer.WriteLineAsync(e.ToString());
            }

            bool failedJing = false;
            await writer.WriteLineAsync("Sending Jing...");

            try
            {
                await mediator.Send(new Jing { Message = "Jing" });
            }
            catch (Exception e)
            {
                failedJing = true;
                await writer.WriteLineAsync(e.ToString());
            }

            bool failedMeow = false;
            await writer.WriteLineAsync("Sending Meow...");

            try
            {
                await mediator.Send(new Meow { Message = "Meow" });
            }
            catch (Exception e)
            {
                failedMeow = true;
                await writer.WriteLineAsync(e.ToString());
            }

            await writer.WriteLineAsync("---------------");

            var contents = writer.Contents;
            var order    = new[] {
                contents.IndexOf("- Starting Up", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("-- Handling Request", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("--- Handled Ping", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("-- Finished Request", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("- All Done", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("- All Done with Ping", StringComparison.OrdinalIgnoreCase),
            };

            var results = new RunResults
            {
                RequestHandlers                       = contents.Contains("--- Handled Ping:"),
                VoidRequestsHandlers                  = contents.Contains("--- Handled Jing:"),
                PipelineBehaviors                     = contents.Contains("-- Handling Request"),
                RequestPreProcessors                  = contents.Contains("- Starting Up"),
                RequestPostProcessors                 = contents.Contains("- All Done"),
                ConstrainedGenericBehaviors           = contents.Contains("- All Done with Ping") && !failedJing,
                OrderedPipelineBehaviors              = order.SequenceEqual(order.OrderBy(i => i)),
                NotificationHandler                   = contents.Contains("Got pinged async"),
                MultipleNotificationHandlers          = contents.Contains("Got pinged async") && contents.Contains("Got pinged also async"),
                ConstrainedGenericNotificationHandler = contents.Contains("Got pinged constrained async") && !failedPong,
                CovariantNotificationHandler          = contents.Contains("Got notified"),
                CovariantPipelineBehavior             = contents.Contains("Got meowed") && !failedMeow
            };

            await writer.WriteLineAsync($"Request Handler...................{(results.RequestHandlers ? "Y" : "N")}");

            await writer.WriteLineAsync($"Void Request Handler..............{(results.VoidRequestsHandlers ? "Y" : "N")}");

            await writer.WriteLineAsync($"Pipeline Behavior.................{(results.PipelineBehaviors ? "Y" : "N")}");

            await writer.WriteLineAsync($"Pre-Processor.....................{(results.RequestPreProcessors ? "Y" : "N")}");

            await writer.WriteLineAsync($"Post-Processor....................{(results.RequestPostProcessors ? "Y" : "N")}");

            await writer.WriteLineAsync($"Constrained Post-Processor........{(results.ConstrainedGenericBehaviors ? "Y" : "N")}");

            await writer.WriteLineAsync($"Ordered Behaviors.................{(results.OrderedPipelineBehaviors ? "Y" : "N")}");

            await writer.WriteLineAsync($"Notification Handler..............{(results.NotificationHandler ? "Y" : "N")}");

            await writer.WriteLineAsync($"Notification Handlers.............{(results.MultipleNotificationHandlers ? "Y" : "N")}");

            await writer.WriteLineAsync($"Constrained Notification Handler..{(results.ConstrainedGenericNotificationHandler ? "Y" : "N")}");

            await writer.WriteLineAsync($"Covariant Notification Handler....{(results.CovariantNotificationHandler ? "Y" : "N")}");

            await writer.WriteLineAsync($"Covariant Pipeline Behavior.......{(results.CovariantPipelineBehavior ? "Y" : "N")}");
        }
Ejemplo n.º 4
0
#pragma warning disable MA0051 // Method is too long
        internal static async Task RunInternalAsync(
            IMediator mediator,
            WrappingWriter writer,
            string projectName)
        {
            await writer.WriteLineAsync("===============").ConfigureAwait(false);

            await writer.WriteLineAsync(projectName).ConfigureAwait(false);

            await writer.WriteLineAsync("===============").ConfigureAwait(false);

            await writer.WriteLineAsync("Sending Ping...").ConfigureAwait(false);

            var pong = await mediator.Send(new Ping { Message = nameof(Ping) }).ConfigureAwait(false);

            await writer.WriteLineAsync("Received: " + pong.Message).ConfigureAwait(false);

            await writer.WriteLineAsync("Publishing Pinged...").ConfigureAwait(false);

            await mediator.Publish(new Pinged()).ConfigureAwait(false);

            await writer.WriteLineAsync("Publishing Ponged...").ConfigureAwait(false);

            var failedPong = false;

            try
            {
                await mediator.Publish(new Ponged()).ConfigureAwait(false);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                failedPong = true;
                await writer.WriteLineAsync(e.ToString()).ConfigureAwait(false);
            }

            var failedJing = false;
            await writer.WriteLineAsync("Sending Jing...").ConfigureAwait(false);

            try
            {
                await mediator.Send(new Jing { Message = nameof(Jing) }).ConfigureAwait(false);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                failedJing = true;
                await writer.WriteLineAsync(e.ToString()).ConfigureAwait(false);
            }

            await writer.WriteLineAsync("---------------").ConfigureAwait(false);

            var contents = writer.Contents;
            var order    = new[]
            {
                contents.IndexOf("- Starting Up", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("-- Handling Request", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("--- Handled Ping", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("-- Finished Request", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("- All Done", StringComparison.OrdinalIgnoreCase),
                contents.IndexOf("- All Done with Ping", StringComparison.OrdinalIgnoreCase),
            };

            var results = new RunResults
            {
                RequestHandlers             = contents.Contains("--- Handled Ping:", StringComparison.OrdinalIgnoreCase),
                VoidRequestsHandlers        = contents.Contains("--- Handled Jing:", StringComparison.OrdinalIgnoreCase),
                PipelineBehaviors           = contents.Contains("-- Handling Request", StringComparison.OrdinalIgnoreCase),
                RequestPreProcessors        = contents.Contains("- Starting Up", StringComparison.OrdinalIgnoreCase),
                RequestPostProcessors       = contents.Contains("- All Done", StringComparison.OrdinalIgnoreCase),
                ConstrainedGenericBehaviors =
                    contents.Contains("- All Done with Ping", StringComparison.OrdinalIgnoreCase) && !failedJing,
                OrderedPipelineBehaviors     = order.SequenceEqual(order.OrderBy(i => i)),
                NotificationHandler          = contents.Contains("Got pinged async", StringComparison.OrdinalIgnoreCase),
                MultipleNotificationHandlers =
                    contents.Contains("Got pinged async", StringComparison.OrdinalIgnoreCase) && contents.Contains(
                        "Got pinged also async",
                        StringComparison.OrdinalIgnoreCase),
                ConstrainedGenericNotificationHandler =
                    contents.Contains("Got pinged constrained async", StringComparison.OrdinalIgnoreCase) &&
                    !failedPong,
                CovariantNotificationHandler = contents.Contains("Got notified", StringComparison.OrdinalIgnoreCase),
            };

            await writer.WriteLineAsync($"Request Handler...................{(results.RequestHandlers ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Void Request Handler..............{(results.VoidRequestsHandlers ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Pipeline Behavior.................{(results.PipelineBehaviors ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Pre-Processor.....................{(results.RequestPreProcessors ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Post-Processor....................{(results.RequestPostProcessors ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Constrained Post-Processor........{(results.ConstrainedGenericBehaviors ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Ordered Behaviors.................{(results.OrderedPipelineBehaviors ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Notification Handler..............{(results.NotificationHandler ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Notification Handlers.............{(results.MultipleNotificationHandlers ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Constrained Notification Handler..{(results.ConstrainedGenericNotificationHandler ? "Y" : "N")}")
            .ConfigureAwait(false);

            await writer.WriteLineAsync($"Covariant Notification Handler....{(results.CovariantNotificationHandler ? "Y" : "N")}")
            .ConfigureAwait(false);
        }