Exemplo n.º 1
0
        public async Task CanRunWithKey()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyAsyncHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new CommandWithKeyAsync();

            int initialCount = cmd.Keys.Count;

            bool allAreTrue = true;
            int  callCount  = 0;

            var result = await tasqR.RunAsync(cmd);

            foreach (var item in result)
            {
                callCount++;
                if (!item)
                {
                    allAreTrue = false;
                }
            }

            Assert.AreEqual(initialCount, callCount);
            Assert.IsTrue(allAreTrue);
        }
Exemplo n.º 2
0
        public void CanResolveMultiLevelConcreteHandler()
        {
            var resolver = new TasqHandlerResolver();

            var handlers = resolver.GetAllHandlers(Assembly.GetExecutingAssembly());

            Assert.IsTrue(handlers.Any(t => t == typeof(TestCommandWithMultipleHandlerHandler2)));
            Assert.IsFalse(handlers.Any(t => t == typeof(Base1Handler)));
        }
Exemplo n.º 3
0
        public async Task CanRunWithReturn()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <TestCmdWithReturnForAsyncHandler>();
            var tasqR = new TasqR(handlerResolver);
            var cmd   = new TestCmdWithReturnForAsync(2);

            var result = await tasqR.RunAsync(cmd);

            Assert.AreEqual(3, result);
        }
Exemplo n.º 4
0
        public void CanRunWithKey()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new CommandWithKey();

            tasqR.Run(cmd);

            Assert.IsTrue(cmd.AllAreCorrect);
        }
Exemplo n.º 5
0
        public void CanRunWithKeyBaseType()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyHandler>();

            var tasqR    = new TasqR(handlerResolver);
            var instance = Activator.CreateInstance(typeof(CommandWithKey));

            tasqR.Run((ITasq <int, bool>)instance);

            Assert.IsTrue(((CommandWithKey)instance).AllAreCorrect);
        }
Exemplo n.º 6
0
        public void CanRunWithReturnForAsyncHandler()
        {
            int startNumber     = 8;
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <TestCmdWithReturnForAsyncHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new TestCmdWithReturnForAsync(startNumber);

            int finalNumber = tasqR.Run(cmd);

            Assert.AreEqual(9, finalNumber);
        }
Exemplo n.º 7
0
        public void CanRunWithReturn()
        {
            int startNumber     = 8;
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <SampleCommandWithReturnHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new SampleCommandWithReturn(startNumber);

            int finalNumber = tasqR.Run(cmd);

            Assert.AreEqual(9, finalNumber);
        }
Exemplo n.º 8
0
        public void CanRunWithKeyForAsyncHandlerBaseType()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyAsyncHandler>();

            var tasqR    = new TasqR(handlerResolver);
            var instance = (ITasq)Activator.CreateInstance(typeof(CommandWithKeyAsync));


            tasqR.Run(instance);

            Assert.IsTrue(true);
        }
Exemplo n.º 9
0
        public async Task CanRunWithoutReturn()
        {
            var testModel = new Test3Model {
                StartNumber = 10
            };
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithAsyncWithoutReturnHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new CommandWithAsyncWithoutReturn(testModel);

            await tasqR.RunAsync(cmd);

            Assert.AreEqual(11, testModel.StartNumber);
        }
Exemplo n.º 10
0
        public void CanRunWithoutReturn()
        {
            var testModel = new TestModel {
                SampleNumber = 10
            };
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <SampleCommandWithoutReturnHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new SampleCommandWithoutReturn(testModel);

            tasqR.Run(cmd);

            Assert.AreEqual(11, testModel.SampleNumber);
        }
Exemplo n.º 11
0
        public async Task WillThrowExceptionFromAsyncHandler()
        {
            try
            {
                var handlerResolver = new TasqHandlerResolver();

                handlerResolver.Register <TestWithErrorCmdHandler>();
                var tasqR = new TasqR(handlerResolver);

                var cmd = new TestWithErrorCmd();
                await tasqR.RunAsync(cmd);
            }
            catch (AggregateException ex)
            {
                throw ex.GetBaseException();
            }
        }
Exemplo n.º 12
0
        public static void AddTasqR(this IServiceCollection services, ServiceLifetime tasqRServiceLifeTime = ServiceLifetime.Scoped, params Assembly[] assemblies)
        {
            if (assemblies != null)
            {
                foreach (var a in assemblies)
                {
                    if (!assemblyList.Contains(a))
                    {
                        assemblyList.Add(a);
                    }
                }
            }

            var resolver = new TasqHandlerResolver();

            var handlers = resolver.GetAllHandlers(assemblies);

            foreach (var handler in handlers)
            {
                services.AddTransient(handler);
            }

            if (tasqRServiceLifeTime == ServiceLifetime.Scoped)
            {
                services.AddScoped <ITasqR>(p =>
                {
                    var msDIHandlerResolver = new MicrosoftDependencyTasqHandlerResolver(p);

                    msDIHandlerResolver.RegisterFromAssembly(assemblyList.ToArray());

                    return(new TasqR(msDIHandlerResolver));
                });
            }
            else if (tasqRServiceLifeTime == ServiceLifetime.Transient)
            {
                services.AddTransient <ITasqR>(p =>
                {
                    var msDIHandlerResolver = new MicrosoftDependencyTasqHandlerResolver(p);

                    msDIHandlerResolver.RegisterFromAssembly(assemblyList.ToArray());

                    return(new TasqR(msDIHandlerResolver));
                });
            }
        }
Exemplo n.º 13
0
        public void CanThrowExceptionIfNoHandlerRegistered()
        {
            try
            {
                var handlerResolver = new TasqHandlerResolver();

                var tasqR = new TasqR(handlerResolver);
                var cmd   = new CommandWithKey();

                tasqR.Run(cmd);
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Type CommandWithKey not registered", ex.Message);

                throw;
            }
        }
Exemplo n.º 14
0
        public void CannotRunDefaultCmdWithHandlerWithNoParamlessConstructor()
        {
            try
            {
                var handlerResolver = new TasqHandlerResolver();

                handlerResolver.Register <CmdHandlerWithNoParamlessCtorHandler>();
                var tasqR = new TasqR(handlerResolver);

                var cmd = new CmdHandlerWithNoParamlessCtor();
                tasqR.Run(cmd);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.StartsWith("No parameterless constructor defined for type"));

                throw;
            }
        }
Exemplo n.º 15
0
        public async Task CanRunWithKeyBaseType()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyAsyncHandler>();

            var tasqR    = new TasqR(handlerResolver);
            var instance = (ITasq <int, bool>)Activator.CreateInstance(typeof(CommandWithKeyAsync));

            bool allAreTrue = true;

            foreach (var item in await tasqR.RunAsync(instance))
            {
                if (!item)
                {
                    allAreTrue = false;
                }
            }

            Assert.IsTrue(allAreTrue);
        }
Exemplo n.º 16
0
        public void CanRunWithKeyForAsyncHandler()
        {
            var handlerResolver = new TasqHandlerResolver();

            handlerResolver.Register <CommandWithKeyAsyncHandler>();

            var tasqR = new TasqR(handlerResolver);
            var cmd   = new CommandWithKeyAsync();

            bool allAreTrue = true;

            foreach (var item in tasqR.Run(cmd))
            {
                if (!item)
                {
                    allAreTrue = false;
                }
            }

            Assert.IsTrue(allAreTrue);
        }