コード例 #1
0
        private void Subscribe()
        {
            // create an instance of the topic helper
            var helper = ServiceBusTopicHelper.Setup(SubscriptionInitializer.Initialize());

            // send the message into the topic
            helper.Subscribe<PizzaOrder>((order) =>
                {
                    // save the order
                    var context = new EnterprisePizzaDataContext();
                    context.Orders.Add(order);
                    context.SaveChanges();

                    // write out a note
                    Console.WriteLine("Order {0} just taken with {1} pizza(s)",
                        order.Id,
                        order.Pizzas.Count);

                    // now notify the store of the new order
                    order.IsOrdered = true;

                    // publish the messages as saved but not received yet
                    helper.Publish<PizzaOrder>(order, (m) =>
                        {
                            m.Properties["IsOrdered"] = true;
                            m.Properties["IsReceivedByStore"] = false;
                        });
                }
                , "(IsOrdered = false) AND (IsReceivedByStore = false)",
                "NewPizzaOrders"
            );
        }
コード例 #2
0
        public static OrderItem ConvertOrderToViewModel(PizzaOrder order)
        {
            // get the ingredients from the database
            var dataContext = new EnterprisePizzaDataContext();
            var ingredients = dataContext.AvailableIngredients.ToList();

            // build the object we'll throw into the service bus topic
            var item = new OrderItem
            {
                OrderId = order.Id,
                ClientIdentifier = order.ClientIdentifier.Value
            };

            // add the pizzas to the order
            order.Pizzas.ForEach(p =>
            {
                var pizzaItem = new PizzaItem
                {
                    PizzaId = p.Id
                };

                // add the sections to the pizza
                p.Sections.ForEach(s =>
                {
                    var sectionItem = new SectionItem();

                    s.Ingredients.ForEach(i =>
                    {
                        sectionItem.Toppings.Add(
                            ingredients.First(x =>
                                x.Id == i.AvailableIngredientId).Name);
                    });

                    pizzaItem.Sections.Add(sectionItem);
                });

                item.Pizzas.Add(pizzaItem);
            });

            return item;
        }