コード例 #1
0
        public void without_monad_at_work()
        {
            Building building = BuildingFactory.AtWorkNotNullBuilding();
            IPhone   phone    = A.Fake <IPhone>();

            if (building != null)
            {
                if (building.Manager != null)
                {
                    Console.WriteLine("Found Manager");
                    if (building.Manager.IsAtWork)
                    {
                        Console.WriteLine("Manager is at work");
                        if (building.Manager.ContactInfo != null)
                        {
                            if (building.Manager.ContactInfo.PhoneNumber != null)
                            {
                                Console.WriteLine("Dialing The Manager");
                                phone.Call(building.Manager.ContactInfo.PhoneNumber);
                            }
                        }
                    }
                }
            }


            A.CallTo(() => phone.Call("1234")).MustHaveHappened();
        }
コード例 #2
0
        public void monad_with_null()
        {
            IPhone phone = A.Fake <IPhone>();

            BuildingFactory.AtWorkNullPhoneNumber()
            .With(b => b.Manager)
            .With(m => m.ContactInfo)
            .With(c => c.PhoneNumber)
            .Do(phoneNumber => phone.Call(phoneNumber));

            A.CallTo(() => phone.Call(null)).WithAnyArguments().MustNotHaveHappened();
        }
コード例 #3
0
        public void monad_with_not_null()
        {
            IPhone phone = A.Fake <IPhone>();

            BuildingFactory.AtWorkNotNullBuilding()
            .With(b => b.Manager)
            .With(m => m.ContactInfo)
            .With(c => c.PhoneNumber)
            .Do(phoneNumber => phone.Call(phoneNumber));

            A.CallTo(() => phone.Call("1234")).MustHaveHappened();
        }
コード例 #4
0
        public void my_strange_monad()
        {
            IPhone phone = A.Fake <IPhone>();

            BuildingFactory.AtWorkNotNullBuilding()
            .GetManager1()
            .GetContact()
            .GetPhoneNumber()
            .StringDo(phoneNumber => phone.Call(phoneNumber));

            A.CallTo(() => phone.Call("1234")).MustHaveHappened();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: yangyangcangcang/NetStudy
        /// <summary>
        /// 配置文件
        /// </summary>
        private static void  Test04()
        {
            IUnityContainer myContainer = UnityContainerFactory.CreateContainer();
            IPhone          phone       = myContainer.Resolve <IPhone>();

            phone.Call();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            try
            {
                {
                    AndroidPhone phone = new AndroidPhone();
                    phone.Call();
                }
                {
                    IPhone phone = new ApplePhone();
                    phone.Call();
                }
                {
                    IPhone phone = ObjectFactory.CreateInstance();
                    phone.Call();
                }

                {
                    //IocTest.Show();
                }
                {
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
コード例 #7
0
ファイル: Demo1.cs プロジェクト: NemoChris/c_charp_ioc
        public static void Run()
        {
            //AOP 面向切面编程,在执行具体业务的前后做其他的操作
            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");                //找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                IUnityContainer container = new UnityContainer();
                section.Configure(container, "testContainer");

                //给Iphone注册aop
                container.AddNewExtension <Interception>().Configure <Interception>()
                .SetInterceptorFor <IPhone>(new InterfaceInterceptor());

                IPhone phone = container.Resolve <IPhone>();
                phone.Call();

                IPhone android = container.Resolve <IPhone>("Android");
                android.Call();

                var _ = 0;
            }
        }
コード例 #8
0
ファイル: IOCTest.cs プロジェクト: doveyyang/2018Study
        public void Show()
        {
            //{
            //    IPhone phone = new ApplePhone();
            //    phone.Call();
            //    phone.System();
            //}

            //{
            //    IUnityContainer container = new UnityContainer();
            //    container.RegisterType<IPhone, ApplePhone>();
            //    container.RegisterType<IHead, Head>();
            //    container.RegisterType<IMicro, Micro>();

            //    IPhone phone = container.Resolve<IPhone>();
            //    phone.Call();
            //    phone.System();
            //}
            {
                IUnityContainer         container = new UnityContainer();
                ExeConfigurationFileMap fileMap   = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Unity.config.xml");

                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                section.Configure(container, "testContainer");

                IPhone phone = container.Resolve <IPhone>();
                phone.Call();
                phone.System();
            }
        }
コード例 #9
0
ファイル: Demo2.cs プロジェクト: NemoChris/c_charp_ioc
        public static void Run()
        {
            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");                //找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                //只有配置的register节点才会有Aop
                //如果都在执行前或都在执行后,Aop的执行顺序和配置的顺序一直
                //Aop针对对象的所有公用方法
                IUnityContainer container = new UnityContainer();
                section.Configure(container, "testContainerAOP");
                IPhone phone = container.Resolve <IPhone>();
                phone.Call();
                phone.SendMsg();

                IPhone phone1 = container.Resolve <IPhone>("apple");
                phone1.Call();
                phone1.SendMsg();

                IPhone phone2 = container.Resolve <IPhone>("android");
                phone2.Call();
                phone2.SendMsg();
            }
        }
コード例 #10
0
        public void monad_with_not_null_not_at_work()
        {
            IPhone phone = A.Fake <IPhone>();

            BuildingFactory.NotAtWorkNotNullBuilding()
            .With(b => b.Manager)
            .Log("Found Manager")
            .If(m => m.IsAtWork)
            .Log("Manager is at work")
            .With(m => m.ContactInfo)
            .With(c => c.PhoneNumber)
            .Log("Dialing The Manager")
            .Do((p) => phone.Call(p));

            A.CallTo(() => phone.Call("1234")).MustNotHaveHappened();
        }
コード例 #11
0
        public void monad_with_null()
        {
            IPhone phone = A.Fake <IPhone>();

            BuildingFactory.AtWorkNullPhoneNumber()
            .With(b => b.Manager)
            .Log("Found Manager")
            .If(m => m.IsAtWork)
            .Log("Manager is at work")
            .With(m => m.ContactInfo)
            .With(c => c.PhoneNumber)
            .Log("Dialing The Manager")
            .Do((p) => phone.Call(p));

            A.CallTo(() => phone.Call(null)).WithAnyArguments().MustNotHaveHappened();
        }
コード例 #12
0
        public static void CommonMethod(IPhone phone)
        {
            phone.VideoCall();
            phone.Call();
            string mes = phone.Message("I'm a Robert Kocharian!!!");

            Console.WriteLine(mes);
        }
コード例 #13
0
        void A()
        {
            IUnityContainer container = new UnityContainer(); //定义一个容器

            container.RegisterType <IPhone, AndroidPhone>();  //注册类型,表示遇到IPhone类型,创建AndroidPhone的实例
            IPhone phone = container.Resolve <IPhone>();      //创建实例

            phone.Call();                                     //调用实例方法
        }
コード例 #14
0
        public void Call()
        {
            if (_phone == null)
            {
                Console.WriteLine("Creating phone!");
                _phone = new Phone();
            }

            _phone.Call();
        }
コード例 #15
0
        public static void Show()
        {
            Console.WriteLine("=====Basic Usage of Unity=========");
            IUnityContainer container = new UnityContainer(); //1. Declare a container

            container.RegisterType <IPhone, AndriodPhone>();  //2. Initiate the container, register the type
            IPhone phone = container.Resolve <IPhone>();      //3. Create an object by reflection

            phone.Call();
        }
コード例 #16
0
        public void monad_with_not_null_at_work_with_logger_inside()
        {
            IPhone  phone  = A.Fake <IPhone>();
            ILogger logger = A.Fake <ILogger>();

            BuildingFactory.AtWorkNotNullBuilding()
            .With(b => b.Manager)
            .Log(logger, "Found Manager")
            .If(m => m.IsAtWork)
            .Log(logger, "Manager is at work")
            .With(m => m.ContactInfo)
            .With(c => c.PhoneNumber)
            .Log(logger, "Dialing The Manager")
            .Do((p) => phone.Call(p));

            A.CallTo(() => phone.Call("1234")).MustHaveHappened();
            A.CallTo(() => logger.Log("Monad.Person > Found Manager")).MustHaveHappened();
            A.CallTo(() => logger.Log("Monad.Person > Manager is at work")).MustHaveHappened();
            A.CallTo(() => logger.Log("1234 > Dialing The Manager")).MustHaveHappened();
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: yangyangcangcang/NetStudy
        private static void Test02()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterType <IPhone, ApplePhone>();
            container.RegisterType <IPower, Power>();
            container.RegisterType <IMicrophone, Microphone>();
            container.RegisterType <IHeadphone, Headphone>();
            IPhone phone = container.Resolve <IPhone>();

            phone.Call();
        }
コード例 #18
0
        public void without_monad_at_work()
        {
            Building building = BuildingFactory.AtWorkNotNullBuilding();
            IPhone   phone    = A.Fake <IPhone>();

            if (building != null)
            {
                if (building.Manager != null && building.Manager.IsAtWork)
                {
                    if (building.Manager.ContactInfo != null)
                    {
                        if (building.Manager.ContactInfo.PhoneNumber != null)
                        {
                            string phoneNumber = building.Manager.ContactInfo.PhoneNumber;
                            phone.Call(phoneNumber);
                        }
                    }
                }
            }

            A.CallTo(() => phone.Call("1234")).MustHaveHappened();
        }
コード例 #19
0
ファイル: Practical 5day study1.cs プロジェクト: fukui-m/C-
        static void Main(string[] args)
        {
            CellPhone cp = new CellPhone("*****@*****.**", "090-1234-5678");

            cp.Call("011-123-4567");
            cp.SendMail("*****@*****.**");
            IPhone Phone = (IPhone)cp;

            Phone.Call("011-987-6543");
            IEmail mail = (IEmail)cp;

            mail.SendMail("*****@*****.**");
        }
コード例 #20
0
        static void Main(string[] args)
        {
            IPower Power = UnityIocHelper.Instance.GetService <IPower>("Power");

            Console.WriteLine();

            IPhone AndroidPhone = UnityIocHelper.Instance.GetService <IPhone>("AndroidPhone");

            AndroidPhone.Call();
            Console.WriteLine();


            IPhone ApplePhone = UnityIocHelper.Instance.GetService <IPhone>("ApplePhone");

            ApplePhone.Call();

            Console.ReadLine();
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: hiroto302/OneWeekStudy
        static void Main(string[] args)
        {
            CellPhone cp = new CellPhone("*****@*****.**", "000-000-1234");

            // 携帯電話クラスで、電話とメールを実行
            cp.Call("001-001-1234");
            cp.SendMail("*****@*****.**");
            // 電話インターフェ-スでインスタンスにアクセス
            // あるクラス(CellPoneクラス)を、ベースクラスやインターフェース(IPhone)の型に代入するときはキャストを利用
            // キャスト(IPhone)をつけて代入することで、CellPhoneクラスを架空のIPhoneクラスとして振舞うことができる
            IPhone phone = (IPhone)cp;

            phone.Call("002-002-1234");
            // メールインターフェースでインスタンスにアクセス
            IEmail mail = (IEmail)cp;

            mail.SendMail("*****@*****.**");
        }
コード例 #22
0
        static void Main(string[] args)
        {
            CellPhone cp = new CellPhone("*****@*****.**", "090-1234-5678");

            //携帯電話クラスで電話とメールを送る
            cp.Call("011-123-4567");
            cp.SendMail("*****@*****.**");
            //電話インターフェースでインスタンスにアクセス
            IPhone phone = (IPhone)cp;

            phone.Call("011-987-6543"); //電話をかける
            //メールの送信メソッドは利用できない
            //phone.SendMail("*****@*****.**");
            //メールインターフェースでインスタンスにアクセス
            IEmail mail = (IEmail)cp;

            mail.SendMail("*****@*****.**");     //メールを出す
            //mailインターフェースでは電話の機能を利用できない
            //mail.Call("011-222-3333");
        }
コード例 #23
0
        void E()
        {
            //使用配置文件
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Configs\\Unity.config");//找配置文件的路径
            Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

            IUnityContainer container = new UnityContainer();

            section.Configure(container, "ContainerOne");
            IPhone phone = container.Resolve <IPhone>();

            phone.Call();

            IPhone android = container.Resolve <IPhone>("Android");

            android.Call();
        }
コード例 #24
0
ファイル: Program.cs プロジェクト: hiywin/Hiywin.Study
        // 依赖倒置写法
        static void DIP()
        {
            {   //依赖于BLL,DAL引用
                //IStudentService service = new StudentService();
                //service.Study();

                //AbstractPhone redmi = new Redmi();
                //service.PlayAbstractPhone(redmi);

                //AbstractPhone vivo = new Vivo();
                //service.PlayAbstractPhone(vivo);
            }
            {   //使用反射
                IStudentService service = SimpleFactory.CreateStudenService();
                service.Study();
                AbstractPhone phone = SimpleFactory.CreateAbstractPhone();
                service.PlayAbstractPhone(phone);
            }
            {    //使用反射
                IPhone phone = SimpleFactory.CreatePhone();
                phone.Call();
            }
        }
コード例 #25
0
        public static void Run()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterType <IPhone, AndroidPhone>();
            container.RegisterType <IPhone, ApplePhone>();            //后注册会替换先前注册的
            container.RegisterType <IPhone, ApplePhone>("Apple");     //为apple类型起别名
            container.RegisterType <IPhone, AndroidPhone>("Android"); //为android类型起别名
            container.RegisterType <IMicrophone, Microphone>();
            container.RegisterType <IHeadphone, Headphone>();
            container.RegisterType <IPower, Power>();
            container.RegisterType <IBLL.IBaseBll, BLL.BaseBll>();
            container.RegisterType <IDAL.IBaseDAL, Ruamou.DAL.BaseDAL>();

            IPhone iphone1 = container.Resolve <IPhone>();

            iphone1.Call();
            IPhone iphone2 = container.Resolve <IPhone>("Apple");           //使用aple别名注册时的类型,即ApplePhone类型
            IPhone iphone3 = container.Resolve <IPhone>("Android");         //使用android别名注册时的类型,即AndroidPhone类型
            IPhone iphone4 = container.Resolve <IPhone>();
            IPhone iphone5 = container.Resolve <IPhone>();

            var _ = 0;
        }
コード例 #26
0
ファイル: Program.cs プロジェクト: pepsiyoung/Sunny.Project
        static void Main(string[] args)
        {
            //IUnityContainer container = new UnityContainer();
            //container.RegisterType<IPhone, AndroidPhone>();//声明UnityContainer并注册IPhone
            //container.RegisterType<ICamera, Camera>();
            //container.AddNewExtension<Interception>().Configure<Interception>().SetInterceptorFor<IPhone>(new InterfaceInterceptor());
            //IPhone phone = container.Resolve<IPhone>();
            //phone.Call();

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找配置文件的路径
            Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
            IUnityContainer           container     = new UnityContainer();

            section.Configure(container, "testContainer");


            IPhone phone = container.Resolve <IPhone>();

            phone.Call();
            System.Console.ReadKey();
        }
コード例 #27
0
        public void TestCall()
        {
            AbstractFactory samsung = new SamsungFactory();
            IPhone          phone   = samsung.createPhone();
            IPhablet        phablet = samsung.createPhablet();
            SoundPlayer     sp      = new SoundPlayer();

            Assert.AreSame(phone.Call().GetType(), sp.GetType());
            Assert.AreSame(phablet.Call().GetType(), sp.GetType());

            AbstractFactory nokia        = new SamsungFactory();
            IPhone          nokiaphone   = nokia.createPhone();
            IPhablet        nokiaphablet = nokia.createPhablet();

            Assert.AreSame(nokiaphone.Call().GetType(), sp.GetType());
            Assert.AreSame(nokiaphablet.Call().GetType(), sp.GetType());

            AbstractFactory apple        = new SamsungFactory();
            IPhone          applephone   = apple.createPhone();
            IPhablet        applephablet = apple.createPhablet();

            Assert.AreSame(applephone.Call().GetType(), sp.GetType());
            Assert.AreSame(applephablet.Call().GetType(), sp.GetType());
        }
コード例 #28
0
        public static void Show()
        {
            Console.WriteLine("**************************************");
            {
                ApplePhone applePhone = new ApplePhone();
                Console.WriteLine("applePhone.Headphone==null? {0}", applePhone.Headphone == null);
                Console.WriteLine("applePhone.Microphone==null? {0}", applePhone.Microphone == null);
                Console.WriteLine("applePhone.Bluetooth==null? {0}", applePhone.Bluetooth == null);
            }

            Console.WriteLine("*****************UnityAndroid*********************");
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IPhone, AndroidPhone>();
                //接口--类型    父类-子类  抽象类-子类
                ////container.RegisterInstance<IPhone>(new AndroidPhone());//实例注册
                IPhone phone = container.Resolve <IPhone>();
                //phone.Call();

                Console.WriteLine("applePhone.Headphone==null? {0}", phone.Headphone == null);
                Console.WriteLine("applePhone.Microphone==null? {0}", phone.Microphone == null);
                Console.WriteLine("applePhone.Bluetooth==null? {0}", phone.Bluetooth == null);
            }

            Console.WriteLine("*****************UnityApple*********************");
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IPhone, ApplePhone>();
                container.RegisterType <IMicrophone, Microphone>();
                container.RegisterType <IHeadphone, Headphone>();
                container.RegisterType <IBluetooth, Bluetooth>();

                IPhone phone = container.Resolve <IPhone>();

                Console.WriteLine("applePhone.Headphone==null? {0}", phone.Headphone == null);
                Console.WriteLine("applePhone.Microphone==null? {0}", phone.Microphone == null);
                Console.WriteLine("applePhone.Bluetooth==null? {0}", phone.Bluetooth == null);
            }
            {
                Console.WriteLine("*****************UnityContainer*********************");

                IUnityContainer container = new UnityContainer();
                // container.RegisterType<IPhone, AndroidPhone>(new TransientLifetimeManager());//瞬态生命周期
                //container.RegisterType<IPhone, AndroidPhone>(new ContainerControlledLifetimeManager());//容器单例
                container.RegisterType <IPhone, AndroidPhone>(new PerThreadLifetimeManager());
                //IPhone iphone1 = null;
                //Action act1 = new Action(() =>
                //{
                //    iphone1 = container.Resolve<IPhone>();
                //});
                //IAsyncResult result1 = act1.BeginInvoke(null, null);
                //IPhone iphone2 = null;
                //Action act2 = new Action(() =>
                //{
                //    iphone2 = container.Resolve<IPhone>();
                //});
                //IAsyncResult result2 = act2.BeginInvoke(null, null);

                //act1.EndInvoke(result1);
                //act2.EndInvoke(result2);

                IPhone iphone1 = container.Resolve <IPhone>();
                IPhone iphone2 = container.Resolve <IPhone>();

                Console.WriteLine(object.ReferenceEquals(iphone1, iphone2));
            }
            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找到配置文件路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName);


                IUnityContainer container = new UnityContainer();
                section.Configure(container, "configContainer");

                IPhone phone = container.Resolve <IPhone>();
                Console.WriteLine("applePhone.Headphone==null? {0}", phone.Headphone == null);
                Console.WriteLine("applePhone.Microphone==null? {0}", phone.Microphone == null);
                Console.WriteLine("applePhone.Bluetooth==null? {0}", phone.Bluetooth == null);
                phone.Call();
            }
        }
コード例 #29
0
ファイル: Student.cs プロジェクト: chenyuefen/Cyf.IOC.Unity
        ///// <summary>
        ///// 面向细节:参数类型写死了  只能为一种手机服务
        ///// </summary>
        ///// <param name="phone"></param>
        //public void PlayApplePhone(ApplePhone phone)
        //{
        //    phone.Call();
        //}

        ///// <summary>
        ///// 面向细节:参数类型写死了  只能为一种手机服务
        ///// 手机扩展  增加了别的类型? 修改这个student
        ///// </summary>
        ///// <param name="phone"></param>
        //public void PlayAndroidPhone(AndroidPhone phone)
        //{
        //    phone.Call();
        //}

        /// <summary>
        /// 面向抽象:可以适配更多类型
        /// 手机类型扩展,也不用修改
        /// </summary>
        /// <param name="phone"></param>
        public void PlayPhone(IPhone phone)
        {
            phone.Call();
        }
コード例 #30
0
        public static void Show()
        {
            Console.WriteLine("**************************************");
            {
                ApplePhone applePhone = new ApplePhone();
                Console.WriteLine("applePhone.iHeadphone==null? {0}", applePhone.iHeadphone == null);
                Console.WriteLine("applePhone.iMicrophone==null? {0}", applePhone.iMicrophone == null);
                Console.WriteLine("applePhone.iPower==null? {0}", applePhone.iPower == null);
            }



            Console.WriteLine("*****************UnityAndroid*********************");
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IPhone, AndroidPhone>();
                //接口--类型    父类-子类  抽象类-子类
                //container.RegisterInstance<IPhone>(new AndroidPhone());//实例注册
                IPhone phone = container.Resolve <IPhone>();

                Console.WriteLine("phone.iHeadphone==null? {0}", phone.iHeadphone == null);
                Console.WriteLine("phone.iMicrophone==null? {0}", phone.iMicrophone == null);
                Console.WriteLine("phone.iPower==null? {0}", phone.iPower == null);
            }
            Console.WriteLine("*****************UnityApple*********************");
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IPhone, ApplePhone>();
                container.RegisterType <IMicrophone, Microphone>();
                container.RegisterType <IHeadphone, Headphone>();
                container.RegisterType <IPower, Power>();

                IPhone phone = container.Resolve <IPhone>();

                Console.WriteLine("phone.iHeadphone==null? {0}", phone.iHeadphone == null);
                Console.WriteLine("phone.iMicrophone==null? {0}", phone.iMicrophone == null);
                Console.WriteLine("phone.iPower==null? {0}", phone.iPower == null);
            }


            Console.WriteLine("*****************UnityContainer*********************");
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IPhone, ApplePhone>(new PerResolveLifetimeManager());
                container.RegisterType <IPhone, ApplePhone>("Apple");
                container.RegisterType <IPhone, AndroidPhone>("Android");
                container.RegisterType <IMicrophone, Microphone>();
                container.RegisterType <IHeadphone, Headphone>();
                container.RegisterType <IPower, Power>();

                container.AddNewExtension <Interception>().Configure <Interception>()
                .SetInterceptorFor <IPhone>(new InterfaceInterceptor());

                IPhone iphone1 = container.Resolve <IPhone>();
                iphone1.Call();
                IPhone iphone2 = container.Resolve <IPhone>("Apple");
                IPhone iphone3 = container.Resolve <IPhone>("Android");
                IPhone iphone4 = container.Resolve <IPhone>();
                IPhone iphone5 = container.Resolve <IPhone>();
            }
            Console.WriteLine("*****************UnityContainer*********************");
            {
                IUnityContainer container = new UnityContainer();
                //container.RegisterType<IPhone, AndroidPhone>(new TransientLifetimeManager());瞬时
                container.RegisterType <IPhone, AndroidPhone>(new ContainerControlledLifetimeManager());//容器单例
                //container.RegisterType<IPhone, AndroidPhone>(new PerThreadLifetimeManager());//线程单例


                IPhone iphone1 = null;
                Action act1    = new Action(() =>
                {
                    iphone1 = container.Resolve <IPhone>();
                });
                var result1 = act1.BeginInvoke(null, null);

                IPhone iphone2 = null;
                Action act2    = new Action(() =>
                {
                    iphone2 = container.Resolve <IPhone>();
                });

                var result2 = act2.BeginInvoke(null, null);

                act1.EndInvoke(result1);
                act2.EndInvoke(result2);

                //IPhone iphone1 = container.Resolve<IPhone>();
                //IPhone iphone2 = container.Resolve<IPhone>();

                Console.WriteLine(object.ReferenceEquals(iphone1, iphone2));
            }
            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                IUnityContainer container = new UnityContainer();
                section.Configure(container, "testContainer");

                IPhone phone = container.Resolve <IPhone>();
                phone.Call();
            }



            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                IUnityContainer container = new UnityContainer();
                section.Configure(container, "testContainerAOP");
                IPhone phone = container.Resolve <IPhone>();
                phone.Call();
            }
        }