学习了Autofac的简单例子

记录一下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Autofac;
using System.Collections;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    interface IMemoDueNotiFier
    {
        void MemoIsDue(Memo memo);
    }
    class Memo
    {
        public  string Title { get; set; }
        public DateTime DueAt { get; set; }
    }
    class Memochecker
    {
        readonly IList<Memo> _memos;
        readonly IMemoDueNotiFier _notifier;
        public Memochecker(IList<Memo> memos, IMemoDueNotiFier notifier)
        {
            _memos = memos;
            _notifier = notifier;
        }
        public void checknow()
        {
            var overduememos = _memos.Where(memo => memo.DueAt < DateTime.Now);
            foreach (var memo in overduememos)
                _notifier.MemoIsDue(memo);
        }
    }
    class PrintingNotifier : IMemoDueNotiFier
    {
        readonly TextWriter _write;
        public PrintingNotifier(TextWriter  write)
        {
            _write = write;
        }
        public void MemoIsDue(Memo memo)
        {
            _write.WriteLine("Memo is {0} due",memo.Title);

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            #region
      
            //string str = "0045";
            //string str2 = (int.Parse(str) + 1).ToString("0000");
            //var memos = new List<Memo> {
            //    new Memo{ Title="Release autofac 1.1",DueAt=new DateTime(2012,6,4)},
            //    new Memo{ Title="update codeproject action",DueAt=DateTime.Now},
            //    new Memo{ Title="Release autofac 3",DueAt=new DateTime(2012,6,10)}

            //};
            //var builder = new ContainerBuilder();
            //builder.Register(c => new Memochecker(
            //    c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotiFier>()));
            //builder.RegisterType<PrintingNotifier>().As<IMemoDueNotiFier>();
            //builder.RegisterInstance(memos).As<IList<Memo>>();
            //builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();
            //using (var container = builder.Build())
            //{
            //    var demo = container.Resolve <IList<Memo>>();
            //    var demo2 = container.Resolve<Memochecker>();
            //    var demo3 = container.Resolve<IMemoDueNotiFier>();
            //    var demo4 = container.Resolve<TextWriter>();
            //    //demo.checknow();
 
            //}
            //Console.WriteLine("Done! Press any key.");
            //Console.ReadKey();
            #endregion
            PrintAllPetNames1();


            
        }
        static void CreateFunctionalXmlElement()
        {
            // A "functional" approach to build an  
            // XML element in memory.  
            XElement inventory =
            new XElement("Inventory",
            new XElement("Car", new XAttribute("ID", "1"),
            new XElement("Color", "Green"),
            new XElement("Make", "BMW"),
            new XElement("PetName", "Stan")
            )
            );
            // Call ToString() on our XElement.  
            Console.WriteLine(inventory);
            Console.ReadLine();
        }
        static void CreateFunctionalXmlDoc()
        {
            XDocument inventoryDoc =
            new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("Current Inventory of AutoLot"),
            new XElement("Inventory",
            new XElement("Car", new XAttribute("ID", "1"),
            new XElement("Color", "Green"),
            new XElement("Make", "BMW"),
            new XElement("PetName", "Stan")
            ),
            new XElement("Car", new XAttribute("ID", "2"),
            new XElement("Color", "Pink"),
            new XElement("Make", "Yugo"),
            new XElement("PetName", "Melvin")
            )
            )
            );
            // Display the document and save to disk.  
            Console.WriteLine(inventoryDoc);
            Console.ReadLine();
            inventoryDoc.Save("SimpleInventory.xml");
        }
        static void CreateXmlDocFromArray()
        {
            // Create an anonymous array of anonymous types.  
            var data = new[] {  
new { PetName = "Melvin", ID = 10 },  
new { PetName = "Pat", ID = 11 },  
new { PetName = "Danny", ID = 12 },  
new { PetName = "Clunker", ID = 13 }  
};
            // Now enumerate over the array to build  
            // an XElement.  
            XElement vehicles =
            new XElement("Inventory",
            from c in data
            select new XElement("Car",
            new XAttribute("ID", c.ID),
            new XElement("PetName", c.PetName)
            )
            );
            Console.WriteLine(vehicles);
            Console.ReadLine();
        }
        static void PrintAllPetNames1()
        {
            Console.WriteLine("***** Fun with LINQ to XML *****\n");
            // Load the Inventory.xml document into memory.  
            XElement doc = XElement.Load("Inventory.xml");
            // We will author each of these next  
            PrintAllPetNames(doc);
            Console.WriteLine();
            GetAllFords(doc);
            Console.ReadLine();
        }
        static void PrintAllPetNames(XElement doc)
        {
            var petNames = from pn in doc.Descendants("PetName")
                           select pn.Value;
            foreach (var name in petNames)
                Console.WriteLine("Name: {0}", name);
        }

        static void GetAllFords(XElement doc)
        {
            var fords = from c in doc.Descendants("Make")
                        where c.Value == "Ford"
                        select c;
            foreach (var f in fords)
                Console.WriteLine("Name: {0}", f);
        }


    }
    class Person
    {
        public string username { get; set; }
        public int age { get; set; }
    }
 

}