示例#1
0
        static void Main(string[] args)
        {
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            Container = new CompositionContainer(catalog);

            Console.WriteLine("MEF DI Container Example");
            Console.WriteLine();

            OrderInfo orderInfo = new OrderInfo()
            {
                CustomerName = "Miguel Castro",
                Email = "*****@*****.**",
                Product = "Laptop",
                Price = 1200,
                CreditCard = "1234567890"
            };

            Commerce commerce = Container.GetExportedValue<Commerce>(); // requires commerce is MEF managed

            #region satisfy own imports

            //Commerce commerce = new Commerce(); // requires that Commerce satisfies its own imports
            //Container.SatisfyImportsOnce(commerce);

            #endregion

            commerce.ProcessOrder(orderInfo);

            Console.WriteLine();
            Console.WriteLine("Press [Enter] to exit...");
            Console.ReadLine();

  
        }
示例#2
0
        //public Commerce()
        //{
        //    //Program.Container.SatisfyImportsOnce(this);
        //}

        //[Import]
        //IBillingProcessor _BillingProcessor;

        //[Import]
        //ICustomer _Customer;

        //[Import]
        //INotifier _Notifier;

        //[Import]
        //ILogger _Logger;

        #endregion

        public void ProcessOrder(OrderInfo orderInfo)
        {
            _BillingProcessor.ProcessPayment(orderInfo.CustomerName, orderInfo.CreditCard, orderInfo.Price);
            _Logger.Log("Billing processed");
            _Customer.UpdateCustomerOrder(orderInfo.CustomerName, orderInfo.Product);
            _Logger.Log("Customer updated");
            _Notifier.SendReceipt(orderInfo);
            _Logger.Log("Receipt sent");
        }
示例#3
0
 void INotifier.SendReceipt(OrderInfo orderInfo)
 {
     // send email to customer with receipt
     Console.WriteLine(string.Format("Receipt sent to customer '{0}' via email.", orderInfo.CustomerName));
 }