Example #1
0
        public void Setup()
        {
            _monitor = new Monitor();
            _injected = new Injected();
            var collection = new ServiceCollection();

            //this registration is key, this is where the actions are associated with a middleware implementation.
            collection.AddSingleton(typeof(BusMiddleWare<>), typeof(BusMiddleWare<>));

            //register the Actions.
            collection.AddTransient(typeof(ConsumerAction<>));
            collection.AddSingleton(typeof(TestAction1<>));
            collection.AddTransient(typeof(TestGenericAction<>));

            //the framework we want to add middleware support with.
            collection.AddSingleton<FakeBus>();

            collection.AddSingleton(_monitor);
            collection.AddSingleton(_injected);


            //find all the IConsumer<T> interfaces
            var consumers = this.GetType().Assembly.ExportedTypes.SelectMany(x =>
            {
                var interfaces = x.GetInterfaces()
                    .Where(interfaceType => interfaceType.IsGenericType)
                    .Where(interfaceType => typeof(IConsumer<>).IsAssignableFrom(interfaceType.GetGenericTypeDefinition()))
                    .Select(interfaceType => new
                    {
                        Type = x,
                        Interface = interfaceType,
                        ConsumedType = interfaceType.GenericTypeArguments[0]
                    });

                return interfaces;
            });


            foreach (var consumer in consumers)
            {
                //Register IConsumer<T>
                collection.AddTransient(consumer.Interface, consumer.Type);
            }

            _baseScope = collection.BuildServiceProvider();
            _bus = _baseScope.GetService<FakeBus>();

            _bus.Subscribe<OrderPlaced>("OrderPlaced", _baseScope);
            _bus.Subscribe<OrderPaymentTaken>("OrderPaymentTaken", _baseScope);
        }
Example #2
0
        public void CanDoItAll()
        {
            var fakeBus = new FakeBus();

            fakeBus.Send(new MyMessage("send")).Wait();
            fakeBus.SendLocal(new MyMessage("send")).Wait();
            fakeBus.Publish(new MyMessage("send")).Wait();
            fakeBus.Defer(TimeSpan.FromSeconds(10), new MyMessage("send")).Wait();
            fakeBus.Subscribe<MyMessage>().Wait();
            fakeBus.Unsubscribe<MyMessage>().Wait();
        }
    public void CanDoItAll()
    {
        var fakeBus = new FakeBus();

        fakeBus.Send(new MyMessage("send")).Wait();
        fakeBus.SendLocal(new MyMessage("send")).Wait();
        fakeBus.Publish(new MyMessage("send")).Wait();
        fakeBus.Defer(TimeSpan.FromSeconds(10), new MyMessage("send")).Wait();
        fakeBus.Subscribe <MyMessage>().Wait();
        fakeBus.Unsubscribe <MyMessage>().Wait();
    }
 public static void Subscribe <T>(this FakeBus bus, string topic, ServiceProvider provider) where T : MessageBase
 {
     bus.Subscribe <T>(topic, msg =>
     {
         using (var scope = provider.CreateScope())
         {
             var pipeline = scope.ServiceProvider.GetService <BusMiddleWare <T> >();
             return(pipeline.Execute(scope.ServiceProvider, msg));
         }
     });
 }
Example #5
0
        public void BookRoom_WithEmptyStore_ShouldBeSuccessful()
        {
            var store = new InMemoryStore();

            Assert.Empty(store.AllBookings());

            var bus     = new FakeBus();
            var handler = new BookingHandler(new BookingService(store, bus));

            bus.Subscribe <BookRoom>(handler.Handle);

            bus.Send(new BookRoom("Room1", DateTime.Now, DateTime.Now.AddDays(2)));

            var bookings = store.AllBookings();

            Assert.NotEmpty(bookings);
            Assert.Single(bookings);
            Assert.Equal("Room1", bookings.Single().RoomName);
        }