Zoe

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ruanmou.Project
{
    public class UserHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("UserHandler");
            return getNext()(input, getNext);
        }
    }
    public class UserHandlerAttribute : HandlerAttribute
    {
        public new int Order { get; set; }
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            ICallHandler handler = new UserHandler() { Order = this.Order };
            return handler;
        }
    }


    public class LogBehavior : IInterceptionBehavior
    {
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("LogBehavior");
            return getNext().Invoke(input, getNext);
        }

        public bool WillExecute
        {
            get { return true; }
        }
    }



}
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using Ruanmou.Interface;
using Ruanmou.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ruanmou.Project
{
    public class IOCTest
    {
        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();
            }
        }
    }
}
using Ruanmou.Interface;
using System;
using System.Configuration;
using System.Reflection;

namespace Ruanmou.Project
{
    public class ObjectFactory
    {
        /// <summary>
        /// 简单工厂+配置文件+反射
        /// </summary>
        /// <returns></returns>
        public static IPhone CreatePhone()
        {
            string classModule = ConfigurationManager.AppSettings["iPhoneType"];
            Assembly assemly = Assembly.Load(classModule.Split(',')[1]);
            Type type = assemly.GetType(classModule.Split(',')[0]);
            return (IPhone)Activator.CreateInstance(type);
        }
    }
}
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Ruanmou.Bussiness.Interface;
//using Ruanmou.Bussiness.Interface;
using Ruanmou.Bussiness.Service;
using Ruanmou.EF.Model;
using Ruanmou.Interface;
//using Ruanmou.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ruanmou.Project
{
    /// <summary>
    /// 1 Ioc(DI)介绍
    /// 控制反转ioc   是目的
    /// DI依赖注入    是手段
    /// 2 Unity容器使用
    /// 3 EF分层封装数据访问
    /// 4 测试数据访问
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班vip课程,今天是IOC+EF");

                //{
                //    AndroidPhone phone = new AndroidPhone();
                //}
                //{
                //    IPhone phone = new AndroidPhone();
                //}
                //{
                //    IPhone phone = ObjectFactory.CreatePhone();
                //}
                //IOCTest.Show();


                //using (JDContext context = new JDContext())
                //{
                //    User user = context.Set<User>().Find(2);
                //}

                //UserService service = new UserService();
                ////User user = service.Find(2);
                //User user = service.Find<User>(2);

                //IUserMenuService service = new UserMenuService();
                //User user = service.Find<User>(2);
                //service.UserLastLogin(user);

                IUnityContainer container = new UnityContainer();
                container.RegisterType<IBaseService, BaseService>();
                container.RegisterType<IUserMenuService, UserMenuService>();
                container.RegisterType<DbContext, JDContext>();

                using (IUserMenuService service = container.Resolve<IUserMenuService>())
                {
                    User user = service.Find<User>(2);
                }


                //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, "ruanmouContainer");

                //ICategoryService categoryService = container.Resolve<ICategoryService>();
                //Category category = categoryService.Find(1);
                //categoryService.Update(category);
                //categoryService.Show();

                //IBaseService<Category> iBaseServie = container.Resolve<IBaseService<Category>>();

                //Category category1 = iBaseServie.Find(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}
using Ruanmou.Interface;
//using Ruanmou.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ruanmou.Project
{
    public class Student
    {
        //public void PlayApplePhone(ApplePhone phone)
        //{
        //    phone.Call();
        //}
        //public void PlayAndroidPhone(AndroidPhone phone)
        //{
        //    phone.Call();
        //}
        public void PlayPhone(IPhone phone)
        {
            phone.Call();
        }

    }
}

 

posted on 2018-04-02 14:06  口袋里的SKY  阅读(202)  评论(0编辑  收藏  举报