public override int Write(TextWriter writer, int count, int p) { using (var parenWriter = new WrappingWriter(() => writer.Write('('), () => writer.Write(')'))) { count += this.Declarator.Write(writer, count, 0) + 2; } return count; }
private static IMediator BuildMediator(WrappingWriter writer) { var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces(); var mediatrOpenTypes = new[] { typeof(IRequestHandler <,>), typeof(IRequestHandler <>), typeof(INotificationHandler <>), }; foreach (var mediatrOpenType in mediatrOpenTypes) { builder .RegisterAssemblyTypes(typeof(Ping).GetTypeInfo().Assembly) .AsClosedTypesOf(mediatrOpenType) .AsImplementedInterfaces(); } builder.RegisterInstance(writer).As <TextWriter>(); // It appears Autofac returns the last registered types first builder.RegisterGeneric(typeof(RequestPostProcessorBehavior <,>)).As(typeof(IPipelineBehavior <,>)); builder.RegisterGeneric(typeof(RequestPreProcessorBehavior <,>)).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.Register <SingleInstanceFactory>(ctx => { var c = ctx.Resolve <IComponentContext>(); return(t => c.Resolve(t)); }); builder.Register <MultiInstanceFactory>(ctx => { var c = ctx.Resolve <IComponentContext>(); return(t => (IEnumerable <object>)c.Resolve(typeof(IEnumerable <>).MakeGenericType(t))); }); var container = builder.Build(); // The below returns: // - RequestPreProcessorBehavior // - RequestPostProcessorBehavior // - GenericPipelineBehavior //var behaviors = container // .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>() // .ToList(); var mediator = container.Resolve <IMediator>(); return(mediator); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new StashboxContainer() .RegisterInstance <TextWriter>(writer) .Register <ServiceFactory>(c => c.WithFactory(r => type => r.Resolve(type))) .RegisterAssemblies(new[] { typeof(Mediator).Assembly, typeof(Ping).Assembly }, serviceTypeSelector: Rules.ServiceRegistrationFilters.Interfaces, registerSelf: false); return(container.Resolve <IMediator>()); }
private static IMediator BuildMediator(WrappingWriter writer) { var builder = new ContainerBuilder(); builder.RegisterModule(new MediatRModule(typeof(Runner).Assembly)); builder.Register <System.IO.TextWriter>(ctn => writer); var container = builder.Build(); return(container.Resolve <IMediator>()); }
public static async Task Main(string[] args) { var writer = new WrappingWriter(Console.Out); var mediator = BuildMediator(writer); await mediator.Handle(new JingCommand { Message = "Hello world" }); var response = await mediator.Handle <PingQuery, PongResponse>(new PingQuery { Message = "Hello world" }); await writer.WriteLineAsync($"--- Handled PingQuery with response: {response.Message}"); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new Container(); container.UseInstance <TextWriter>(writer); container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, Registrator.Interfaces); container.Register <IMediator, InjectingMediator>(ifAlreadyRegistered: IfAlreadyRegistered.Replace); container.UseInstance <ServiceFactory>(container.Resolve); return(container.Resolve <IMediator>()); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new Container(); container.RegisterDelegate <ServiceFactory>(r => r.Resolve); container.UseInstance <TextWriter>(writer); //Pipeline works out of the box here container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, Registrator.Interfaces); return(container.Resolve <IMediator>()); }
private static IMediator BuildMediator(WrappingWriter writer) { var services = new ServiceCollection(); services.AddSingleton <TextWriter>(writer); services.AddMediatR(typeof(Ping)); services.AddScoped(typeof(IPipelineBehavior <,>), typeof(GenericPipelineBehavior <,>)); services.AddScoped(typeof(IRequestPreProcessor <>), typeof(GenericRequestPreProcessor <>)); services.AddScoped(typeof(IRequestPostProcessor <,>), typeof(GenericRequestPostProcessor <,>)); var provider = services.BuildServiceProvider(); return(provider.GetRequiredService <IMediator>()); }
public static void Main(string[] args) { InitStore(); ConfigMappings(); _writer = new WrappingWriter(Console.Out); _mediator = BuildMediator(_writer); while (true) { CreateNewClient(); Console.WriteLine("Press any key to create a new client..."); Console.ReadKey(); } }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new UnityContainer(); container.RegisterInstance <TextWriter>(writer) .RegisterMediator(new HierarchicalLifetimeManager()) .RegisterMediatorHandlers(Assembly.GetAssembly(typeof(Ping))); container.RegisterType(typeof(IPipelineBehavior <,>), typeof(RequestPreProcessorBehavior <,>), "RequestPreProcessorBehavior"); container.RegisterType(typeof(IPipelineBehavior <,>), typeof(RequestPostProcessorBehavior <,>), "RequestPostProcessorBehavior"); container.RegisterType(typeof(IPipelineBehavior <,>), typeof(GenericPipelineBehavior <,>), "GenericPipelineBehavior"); container.RegisterType(typeof(IRequestPreProcessor <>), typeof(GenericRequestPreProcessor <>), "GenericRequestPreProcessor"); container.RegisterType(typeof(IRequestPostProcessor <,>), typeof(GenericRequestPostProcessor <,>), "GenericRequestPostProcessor"); return(container.Resolve <IMediator>()); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new DependencyInjectionContainer(); container.Configure(c => { c.ExportFunc <ServiceFactory>(scope => scope.Locate); c.ExportInstance <TextWriter>(writer); //Pipeline works out of the box here c.ExportAssemblies( new[] { typeof(IMediator).Assembly, typeof(Ping).Assembly }) .ByInterfaces(); }); return(container.Locate <IMediator>()); }
private static IMediator BuildMediator(WrappingWriter writer) { var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces(); var mediatrOpenTypes = new[] { typeof(IRequestHandler <,>), typeof(INotificationHandler <>), typeof(IValidator <>), }; foreach (var mediatrOpenType in mediatrOpenTypes) { builder .RegisterAssemblyTypes(typeof(ClientCreateRequest).GetTypeInfo().Assembly) .AsClosedTypesOf(mediatrOpenType) .AsImplementedInterfaces(); } builder.RegisterInstance(writer).As <TextWriter>(); // It appears Autofac returns the last registered types first builder.RegisterGeneric(typeof(RequestPostProcessorBehavior <,>)).As(typeof(IPipelineBehavior <,>)); builder.RegisterGeneric(typeof(RequestPreProcessorBehavior <,>)).As(typeof(IPipelineBehavior <,>)); builder.RegisterGeneric(typeof(ValidationBehavior <,>)).As(typeof(IPipelineBehavior <,>)); builder.Register <ServiceFactory>(ctx => { var c = ctx.Resolve <IComponentContext>(); return(t => c.Resolve(t)); }); var container = builder.Build(); var mediator = container.Resolve <IMediator>(); return(mediator); }
public override int Write(TextWriter writer, int count, int p) { writer.Write("enum"); count += 4; if (this.Identifier != null) { writer.Write(' '); writer.Write(this.Identifier); count += this.Identifier.Length + 1; } else if (this.EnumeratorList != null) { using (var bracketWriter = new WrappingWriter(() => writer.WriteLine(" {"), () => writer.WriteLine('}'))) { count += 2; bool first = true; foreach (var enumerator in this.EnumeratorList) { if (!first) { writer.WriteLine(","); count++; } else { first = false; } enumerator.Write(writer, count, p + 1); } writer.WriteLine(); writer.WriteIndentTabs(p); count += p + 2; } } return(count); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new Container(); var assemblies = GetAssemblies().ToArray(); container.RegisterSingleton <IMediator, Mediator>(); container.Register(typeof(IRequestHandler <,>), assemblies); container.Register(typeof(IRequestHandler <>), assemblies); // we have to do this because by default, generic type definitions (such as the Constrained Notification Handler) won't be registered var notificationHandlerTypes = container.GetTypesToRegister(typeof(INotificationHandler <>), assemblies, new TypesToRegisterOptions { IncludeGenericTypeDefinitions = true, IncludeComposites = false, }); container.RegisterCollection(typeof(INotificationHandler <>), notificationHandlerTypes); container.RegisterSingleton <TextWriter>(writer); //Pipeline container.RegisterCollection(typeof(IPipelineBehavior <,>), new [] { typeof(RequestPreProcessorBehavior <,>), typeof(RequestPostProcessorBehavior <,>), typeof(GenericPipelineBehavior <,>) }); container.RegisterCollection(typeof(IRequestPreProcessor <>), new [] { typeof(GenericRequestPreProcessor <>) }); container.RegisterCollection(typeof(IRequestPostProcessor <,>), new[] { typeof(GenericRequestPostProcessor <,>), typeof(ConstrainedRequestPostProcessor <,>) }); container.RegisterSingleton(new SingleInstanceFactory(container.GetInstance)); container.RegisterSingleton(new MultiInstanceFactory(container.GetAllInstances)); container.Verify(); var mediator = container.GetInstance <IMediator>(); return(mediator); }
public override int Write(TextWriter writer, int count, int p) { if (this.StructureType == StructureType.Union) { writer.Write("union"); count += 5; } else { writer.Write("struct"); count += 6; } if (this.Identifier != null) { writer.Write(' '); writer.Write(this.Identifier); count += this.Identifier.Length + 1; } else if (this.StructureDeclarationList != null) { using (var bracketWriter = new WrappingWriter(() => writer.WriteLine(" {"), () => writer.WriteLine('}'))) { count += 2; foreach (var decl in this.StructureDeclarationList) { count = decl.Write(writer, count, p + 1); writer.WriteLine(';'); count++; } writer.WriteIndentTabs(p); count += p + 1; } } return(count); }
private static IMediator BuildMediator(WrappingWriter writer) { var kernel = new StandardKernel(); kernel.Components.Add <IBindingResolver, ContravariantBindingResolver>(); kernel.Bind(scan => scan.FromAssemblyContaining <IMediator>().SelectAllClasses().BindDefaultInterface()); kernel.Bind <TextWriter>().ToConstant(writer); kernel.Bind(scan => scan.FromAssemblyContaining <Ping>().SelectAllClasses().InheritedFrom(typeof(IRequestHandler <,>)).BindAllInterfaces()); kernel.Bind(scan => scan.FromAssemblyContaining <Ping>().SelectAllClasses().InheritedFrom(typeof(IRequestHandler <>)).BindAllInterfaces()); kernel.Bind(scan => scan.FromAssemblyContaining <Ping>().SelectAllClasses().InheritedFrom(typeof(INotificationHandler <>)).BindAllInterfaces()); //Pipeline kernel.Bind(typeof(IPipelineBehavior <,>)).To(typeof(RequestPreProcessorBehavior <,>)); kernel.Bind(typeof(IPipelineBehavior <,>)).To(typeof(RequestPostProcessorBehavior <,>)); kernel.Bind(typeof(IPipelineBehavior <,>)).To(typeof(GenericPipelineBehavior <,>)); kernel.Bind(typeof(IRequestPreProcessor <>)).To(typeof(GenericRequestPreProcessor <>)); kernel.Bind(typeof(IRequestPostProcessor <,>)).To(typeof(GenericRequestPostProcessor <,>)); kernel.Bind(typeof(IRequestPostProcessor <,>)).To(typeof(ConstrainedRequestPostProcessor <,>)); kernel.Bind(typeof(INotificationHandler <>)).To(typeof(ConstrainedPingedHandler <>)).WhenNotificationMatchesType <Pinged>(); kernel.Bind <SingleInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.TryGet(t)); kernel.Bind <MultiInstanceFactory>().ToMethod(ctx => t => { try { return(ctx.Kernel.GetAll(t).ToList()); } catch (Exception e) { writer.WriteLine(e.ToString()); return(new object[0]); } }); var mediator = kernel.Get <IMediator>(); return(mediator); }
private static IMediator BuildMediator(WrappingWriter writer) { var serviceContainer = new ServiceContainer(); serviceContainer.Register <IMediator, Mediator>(); serviceContainer.RegisterInstance <TextWriter>(writer); serviceContainer.RegisterAssembly(typeof(Ping).GetTypeInfo().Assembly, (serviceType, implementingType) => serviceType.IsConstructedGenericType && ( serviceType.GetGenericTypeDefinition() == typeof(IRequestHandler <,>) || serviceType.GetGenericTypeDefinition() == typeof(IRequestHandler <>) || serviceType.GetGenericTypeDefinition() == typeof(INotificationHandler <>) )); serviceContainer.RegisterOrdered(typeof(IPipelineBehavior <,>), new[] { typeof(RequestPreProcessorBehavior <,>), typeof(RequestPostProcessorBehavior <,>), typeof(GenericPipelineBehavior <,>) }, type => new PerContainerLifetime()); serviceContainer.RegisterOrdered(typeof(IRequestPostProcessor <,>), new[] { typeof(GenericRequestPostProcessor <,>), typeof(ConstrainedRequestPostProcessor <,>) }, type => new PerContainerLifetime()); serviceContainer.Register(typeof(IRequestPreProcessor <>), typeof(GenericRequestPreProcessor <>)); serviceContainer.Register <SingleInstanceFactory>(fac => fac.GetInstance); serviceContainer.Register <MultiInstanceFactory>(fac => fac.GetAllInstances); return(serviceContainer.GetInstance <IMediator>()); }
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 <ServiceFactory>().Use(ctx => ctx.GetInstance); cfg.For <TextWriter>().Use(writer); }); var mediator = container.GetInstance <IMediator>(); return(mediator); }
private static IMediator BuildMediator(WrappingWriter writer) { var container = new WindsorContainer(); container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel)); container.Kernel.AddHandlersFilter(new ContravariantFilter()); // *** The default lifestyle for Windsor is Singleton // *** If you are using ASP.net, it's better to register your services with 'Per Web Request LifeStyle'. container.Register(Classes.FromAssemblyContaining <Ping>().BasedOn(typeof(IRequestHandler <,>)).WithServiceAllInterfaces()); container.Register(Classes.FromAssemblyContaining <Ping>().BasedOn(typeof(INotificationHandler <>)).WithServiceAllInterfaces()); container.Register(Component.For <IMediator>().ImplementedBy <Mediator>()); container.Register(Component.For <TextWriter>().Instance(writer)); container.Register(Component.For <ServiceFactory>().UsingFactoryMethod <ServiceFactory>(k => (type => { var enumerableType = type .GetInterfaces() .Concat(new [] { type }) .FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable <>)); return(enumerableType != null ? k.ResolveAll(enumerableType.GetGenericArguments()[0]) : k.Resolve(type)); }))); //Pipeline container.Register(Component.For(typeof(IPipelineBehavior <,>)).ImplementedBy(typeof(RequestPreProcessorBehavior <,>)).NamedAutomatically("PreProcessorBehavior")); container.Register(Component.For(typeof(IPipelineBehavior <,>)).ImplementedBy(typeof(RequestPostProcessorBehavior <,>)).NamedAutomatically("PostProcessorBehavior")); container.Register(Component.For(typeof(IPipelineBehavior <,>)).ImplementedBy(typeof(GenericPipelineBehavior <,>)).NamedAutomatically("Pipeline")); container.Register(Component.For(typeof(IRequestPreProcessor <>)).ImplementedBy(typeof(GenericRequestPreProcessor <>)).NamedAutomatically("PreProcessor")); container.Register(Component.For(typeof(IRequestPostProcessor <,>)).ImplementedBy(typeof(GenericRequestPostProcessor <,>)).NamedAutomatically("PostProcessor")); container.Register(Component.For(typeof(IRequestPostProcessor <,>), typeof(ConstrainedRequestPostProcessor <,>)).NamedAutomatically("ConstrainedRequestPostProcessor")); container.Register(Component.For(typeof(INotificationHandler <>), typeof(ConstrainedPingedHandler <>)).NamedAutomatically("ConstrainedPingedHandler")); var mediator = container.Resolve <IMediator>(); return(mediator); }
public void Write(TextWriter writer, IReadOnlyCollection <Definition> defns) { if (!WriterExtensions.IsIgnoredDecl(this.IfStack, defns)) { using (var ifStackWriter = new IfStackWriter(writer, this.IfStack)) { writer.Write("#define "); writer.Write(this.Identifier); if (this.Arguments != null) { using (var parentWriter = new WrappingWriter(() => writer.Write('('), () => writer.Write(')'))) { bool first = true; foreach (var arg in this.Arguments) { if (first) { first = false; } else { writer.Write(','); } writer.Write(arg); } } } if (!string.IsNullOrWhiteSpace(this.Replacement)) { writer.Write(' '); writer.WriteLine(this.Replacement.Trim()); } } } }
public static async Task Run(IResolver resolver, WrappingWriter writer, string projectName) { await writer.WriteLineAsync("==============="); await writer.WriteLineAsync(projectName); await writer.WriteLineAsync("==============="); var ct = new CancellationToken(); await writer.WriteLineAsync("Sending Ping..."); var pingPong = resolver.Resolve <IMessageHandler <Ping, Pong> >(); var pong = await pingPong.Handle(new Ping { Message = "Ping" }, ct); await writer.WriteLineAsync("Received: " + pong.Message); await writer.WriteLineAsync("Publishing Pinged..."); var pinged = resolver.Resolve <BroadcastMessageHandler <Pinged> >(); await pinged.Handle(new Pinged(), ct); await writer.WriteLineAsync("Publishing Ponged..."); var failedPong = false; try { var ponged = resolver.Resolve <BroadcastMessageHandler <Ponged> >(); await ponged.Handle(new Ponged(), ct); } catch (Exception e) { failedPong = true; await writer.WriteLineAsync(e.ToString()); } bool failedJing = false; await writer.WriteLineAsync("Sending Jing..."); try { var jing = resolver.Resolve <IMessageHandler <Jing, EmptyResponse> >(); await jing.Handle(new Jing { Message = "Jing" }, ct); } catch (Exception e) { failedJing = 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") }; 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")}"); }
public override int Write(TextWriter writer, int count, int p) { if (this.StructureType == StructureType.Union) { writer.Write("union"); count += 5; } else { writer.Write("struct"); count += 6; } if (this.Identifier != null) { writer.Write(' '); writer.Write(this.Identifier); count += this.Identifier.Length + 1; } else if (this.StructureDeclarationList != null) { using (var bracketWriter = new WrappingWriter(() => writer.WriteLine(" {"), () => writer.WriteLine('}'))) { count += 2; foreach (var decl in this.StructureDeclarationList) { count = decl.Write(writer, count, p + 1); writer.WriteLine(';'); count++; } writer.WriteIndentTabs(p); count += p + 1; } } return count; }
public override int Write(TextWriter writer, int count, int p) { if (this.Declarator != null) count += this.Declarator.Write(writer, count, 0); using (var parenWriter = new WrappingWriter(() => writer.Write('('), () => writer.Write(')'))) { if (this.ParameterTypeList != null) { bool first = true; foreach (var param in this.ParameterTypeList) { if (first) first = false; else { writer.Write(", "); count += 2; } count += param.Write(writer, 0, 0); } } } count += 2; return count; }
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 <>), }; 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(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)); }); var container = builder.Build(); // The below returns: // - RequestPreProcessorBehavior // - RequestPostProcessorBehavior // - GenericPipelineBehavior // - RequestExceptionActionProcessorBehavior // - RequestExceptionProcessorBehavior //var behaviors = container // .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>() // .ToList(); var mediator = container.Resolve <IMediator>(); return(mediator); }
public override int Write(TextWriter writer, int count, int p) { writer.Write("enum"); count += 4; if (this.Identifier != null) { writer.Write(' '); writer.Write(this.Identifier); count += this.Identifier.Length + 1; } else if (this.EnumeratorList != null) { using (var bracketWriter = new WrappingWriter(() => writer.WriteLine(" {"), () => writer.WriteLine('}'))) { count += 2; bool first = true; foreach (var enumerator in this.EnumeratorList) { if (!first) { writer.WriteLine(","); count++; } else first = false; enumerator.Write(writer, count, p + 1); } writer.WriteLine(); writer.WriteIndentTabs(p); count += p + 2; } } return count; }