예제 #1
0
        /// <summary>
        /// The main entry point for Program.
        /// </summary>
        /// <param name="args">The args.</param>
        public static void Main(string[] args)
        {
            DynamicDispatcher dispatcher = new DynamicDispatcher();

            dispatcher.Register<string>("Foo", str => new DispatchResult() { Result = str });
            dispatcher.Register<int>("Foo", num => new DispatchResult() { Result = num });

            DispatchResult result = dispatcher.Dispatch<string>("Foo", "Bar");

            Debug.WriteLine(dispatcher.Dispatch<string>("Foo", "Bar").Result.ToString());
            Debug.WriteLine(dispatcher.Dispatch<int>("Foo", 123).Result.ToString());

            int x = 0;
            x++;
        }
예제 #2
0
        public void NotCallHandler()
        {
            var registry = new Mock <IHandlerRegistry>();
            var mi       = typeof(ITestHandler).GetMethod("Handle");

            registry
            .Setup(x => x.GetHandlers(typeof(TestEvent)))
            .Returns((IEnumerable <HandlerInfo>) new[] { new HandlerInfo(mi) });

            var handler = new Mock <ITestHandler>();

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(x => x.GetService(typeof(ITestHandler)))
            .Returns(handler.Object);

            var unitOfWork = new Mock <IUnitOfWork>();

            var dispatcher = new DynamicDispatcher(registry.Object, serviceProvider.Object,
                                                   unitOfWork.Object);

            var context = new MessageContext(Guid.NewGuid(), 123, "ABC", 123);
            var e       = new TestEvent2();

            dispatcher.Dispatch(context, e).Wait();

            handler.Verify(x => x.Handle(context, It.IsAny <TestEvent>()), Times.Never);
            unitOfWork.Verify(x => x.Commit(), Times.Never);
        }
        public void ExposesExtensionMethods()
        {
            var dispatcher = new DynamicDispatcher(new MainTarget(), new MyExtension());

            Assert.IsTrue(dispatcher.HasMethod("PublicOp"));
            Assert.IsTrue(dispatcher.HasMethod("ExtMethod"));
            Assert.AreEqual("Hello", dispatcher.Dispatch("ExtMethod", null));
        }
        public void CanResolveMethodArgumentsWithParamArray()
        {
            var dispatcher = new DynamicDispatcher(new MainTarget());

            Assert.AreEqual(20, dispatcher.Dispatch("SumMany", new object[] { 1, 3, 7, 9 }));
        }
        public void CanResolveMethodArguments()
        {
            var dispatcher = new DynamicDispatcher(new MainTarget());

            Assert.AreEqual(4, dispatcher.Dispatch("Sum", new object[] { 1, 3 }));
        }
        public void CannotInvokeMethodsNotMarkedAsOperation()
        {
            var dispatcher = new DynamicDispatcher(new MainTarget());

            dispatcher.Dispatch("HiddenOp", null);
        }
        public void CanInvokeDynamicOperations()
        {
            var dispatcher = new DynamicDispatcher(new MainTarget());

            Assert.AreEqual("good code!", dispatcher.Dispatch("PublicOp", null));
        }