Exemplo n.º 1
0
        public static MediatorBuilder AddLatransMedaitor(
            this IServiceCollection services,
            Action <IMediatorBuilder>?configure = null
            )
        {
            var builder = new MediatorBuilder();

            services.AddOptions <ActivityExecutionConfigurationOptions>();
            services.AddTransient <ActivityExecutionConfigurationDefaults>();
            services.AddScoped <ILocalDisposables, LocalDisposables>();
            services.AddScoped <IMediatorClientFactory, MediatorClientFactory>();
            services.AddTransient <IMediatorClient, MediatorClient>();
            services.AddSingleton <IMediatorService>((serviceProvider) => {
                IMediatorService result;
                lock (serviceProvider) {
                    builder.Build();
                    var options = builder.GetOptions();
                    result      = new MediatorService(options);
                }
                return(result);
            });
            if (configure is object)
            {
                configure(builder);
            }
            return(builder);
        }
Exemplo n.º 2
0
        public static void RegisterMediator(this Container container, MediatorBuilder mediatorBuilder)
        {
            container.RegisterSingleton(mediatorBuilder);
            container.Register(() =>
            {
                var resolver = new SimpleInjectorDependencyScope(container);
                return(mediatorBuilder.Build(resolver));
            }, Lifestyle.Scoped);

            RegisterHandlers(container, mediatorBuilder);
        }
        public static ContainerBuilder RegisterMediator(this ContainerBuilder containerBuilder, MediatorBuilder mediatorBuilder)
        {
            containerBuilder.RegisterInstance(mediatorBuilder).AsSelf().AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType <AutofacDependancyScope>().AsImplementedInterfaces();
            containerBuilder.Register(x =>
            {
                var resolver = x.Resolve <IDependancyScope>().BeginScope();
                return(mediatorBuilder.Build(resolver));
            }).AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope();

            RegisterHandlers(containerBuilder);

            return(containerBuilder);
        }
Exemplo n.º 4
0
        public static void Configure(this IContainer container, MediatorBuilder builder)
        {
            container.Configure(x =>
            {
                x.For <MediatorBuilder>().Add(builder).Singleton();
                x.For <IDependencyScope>().Use(() => new StructureMapDependencyScope(container).BeginScope());

                x.For <IMediator>().Use(context => builder.Build(context.GetInstance <IDependencyScope>()));
                foreach (var binding in builder.MessageHandlerRegistry.MessageBindings)
                {
                    x.AddType(binding.HandlerType, binding.HandlerType);
                }
            });
        }
Exemplo n.º 5
0
        public static void Configure(MediatorBuilder builder, IUnityContainer container)
        {
            container.RegisterInstance(typeof(MediatorBuilder), builder);
            container.RegisterType <IMediator, Mediator>(new InjectionFactory(x =>
            {
                var child = new UnityDependancyScope(container).BeginScope();
                return(builder.Build(child));
            }));

            foreach (var binding in MessageHandlerRegistry.MessageBindings)
            {
                container.RegisterType(binding.HandlerType, binding.HandlerType);
            }
        }
        void When1000CommandIsSentByDifferentMediator()
        {
            var sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
                var mediator = _builder.Build();
                mediator.SendAsync(new NoWorkCommand()).Wait();
            }

            sw.Stop();
            milliSeconds = sw.ElapsedMilliseconds;
            Console.WriteLine($"it took {milliSeconds} milliseconds");
        }
Exemplo n.º 7
0
        public static IServiceCollection RegisterMediator(this IServiceCollection services, MediatorBuilder mediatorBuilder)
        {
            services.AddSingleton <MediatorBuilder>(mediatorBuilder);

            services.AddScoped(x =>
            {
                var scope           = x.CreateScope();
                var dependencyScope = new MicrosoftDependencyScope(scope);
                return(mediatorBuilder.Build(dependencyScope));
            });

            foreach (var binding in mediatorBuilder.MessageHandlerRegistry.MessageBindings)
            {
                services.AddScoped(binding.HandlerType);
            }

            return(services);
        }
        Task When1000CommandIsSentByDifferentMediator()
        {
            var allTasks = new List <Task>();
            var sw       = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
                var mediator = _builder.Build();
                allTasks.Add(mediator.SendAsync(new NoWorkCommand()));
            }

            Task.WhenAll(allTasks.ToArray());
            sw.Stop();
            milliSeconds = sw.ElapsedMilliseconds;
            Console.WriteLine($"it took {milliSeconds} milliseconds");
            return(Task.FromResult(0));
        }