Beispiel #1
0
        static void Main(string[] args)
        {
            const int numOfCalls = 100000;

            ///***** PURE C# *****/
            var handler = new SampleHandler();

            Time <Message>("Pure C# - Shared handler", numOfCalls, handler.HandleAsync);
            Time <Message>("Pure C# - New handler", numOfCalls, m => new SampleHandler().HandleAsync(m));
            Time <MyRequest>("Pure C# Request - Shared handler", numOfCalls, handler.HandleAsync);
            Time <MyRequest>("Pure C# Request - New handler", numOfCalls, m => new SampleHandler().HandleAsync(m));

            /***** ROUTEMEISTER *****/
            var sampleAssembly               = typeof(Program).GetTypeInfo().Assembly;
            var routeFactory                 = new MessageRouteFactory();
            var routes                       = routeFactory.Create(sampleAssembly, typeof(IMyHandlerOf <>));
            var reqRoutes                    = routeFactory.Create(sampleAssembly, typeof(IMyRequestHandlerOf <,>));
            var sharedHandlerRouter          = new SequentialAsyncRouter((t, e) => handler, routes);
            var newHandlerRouter             = new SequentialAsyncRouter((t, e) => new SampleHandler(), routes);
            var asyncDispatcherSharedHandler = new AsyncDispatcher((t, e) => handler, reqRoutes);
            var asyncDispatcherNewHandler    = new AsyncDispatcher((t, e) => new SampleHandler(), reqRoutes);

            Time <Message>("SequentialAsyncRouter - Shared handler", numOfCalls, sharedHandlerRouter.RouteAsync);
            Time <Message>("SequentialAsyncRouter - New handler", numOfCalls, newHandlerRouter.RouteAsync);

            Time <MyRequest>("AsyncDispatcher.Send - Shared handler", numOfCalls, asyncDispatcherSharedHandler.SendAsync);
            Time <MyRequest>("AsyncDispatcher.Send - New handler", numOfCalls, asyncDispatcherNewHandler.SendAsync);

            Time <MyRequest>("AsyncDispatcher.Publish - Shared handler", numOfCalls, asyncDispatcherSharedHandler.PublishAsync);
            Time <MyRequest>("AsyncDispatcher.Publish - New handler", numOfCalls, asyncDispatcherNewHandler.PublishAsync);

            Time <MyRequest>("AsyncDispatcher.Request - Shared handler", numOfCalls, asyncDispatcherSharedHandler.RequestAsync);
            Time <MyRequest>("AsyncDispatcher.Request - New handler", numOfCalls, asyncDispatcherNewHandler.RequestAsync);

            var messageType = typeof(Message);
            var route       = routes.GetRoute(messageType);
            var routeAction = route.Actions.Single();

            Time <Message>("Manual Route - Shared handler", numOfCalls, m => (Task)routeAction.Invoke(handler, m));
            Time <Message>("Manual Route - New handler", numOfCalls, m => (Task)routeAction.Invoke(new SampleHandler(), m));
        }
Beispiel #2
0
        protected override void Load(ContainerBuilder builder)
        {
            //MessageRoutes
            builder.Register(ctx =>
            {
                var factory = new MessageRouteFactory();

                return(factory.Create(
                           SampleAssembly.Item,
                           typeof(IAsyncMessageHandler <>)));
            })
            .SingleInstance();

            //MiddlewareEnabledAsyncRouter sample
            builder.Register(ctx =>
            {
                var routes      = ctx.Resolve <MessageRoutes>();
                var parentScope = ctx.Resolve <ILifetimeScope>();
                var router      = new MiddlewareEnabledAsyncRouter(
                    (type, envelope) => envelope.GetScope().Resolve(type),
                    routes);
                router.Use(next => envelope =>
                {
                    using (var childScope = parentScope.BeginLifetimeScope())
                    {
                        envelope.SetScope(childScope);
                        return(next(envelope));
                    }
                });

                return(router);
            })
            .SingleInstance();

            //SequentialAsyncRouter sample
            builder.Register(ctx =>
            {
                var routes      = ctx.Resolve <MessageRoutes>();
                var parentScope = ctx.Resolve <ILifetimeScope>();
                var router      = new SequentialAsyncRouter(
                    (type, envelope) => envelope.GetScope().Resolve(type),
                    routes)
                {
                    OnBeforeRouting = envelope => envelope.SetScope(parentScope.BeginLifetimeScope()),
                    OnAfterRouted   = envelope => envelope.GetScope()?.Dispose()
                };


                return(router);
            })
            .SingleInstance();

            //AsyncDispatcher sample
            builder.Register(ctx =>
            {
                var routes      = ctx.Resolve <MessageRoutes>();
                var parentScope = ctx.Resolve <ILifetimeScope>();
                var router      = new AsyncDispatcher(
                    (type, envelope) => envelope.GetScope().Resolve(type),
                    routes)
                {
                    OnBeforeRouting = envelope => envelope.SetScope(parentScope.BeginLifetimeScope()),
                    OnAfterRouted   = envelope => envelope.GetScope()?.Dispose()
                };


                return(router);
            })
            .SingleInstance();

            builder.RegisterType <SomeDependency>();
            builder.RegisterType <ConcreteHandler>();
        }