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 IEnumerableGetRequiredInterfaces() { 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(); //接口--类型 父类-子类 抽象类-子类 //container.RegisterInstance (new AndroidPhone());//实例注册 IPhone phone = container.Resolve (); 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 (); container.RegisterType (); container.RegisterType (); container.RegisterType (); IPhone phone = container.Resolve (); 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 (new PerResolveLifetimeManager()); container.RegisterType ("Apple"); container.RegisterType ("Android"); container.RegisterType (); container.RegisterType (); container.RegisterType (); container.AddNewExtension ().Configure () .SetInterceptorFor (new InterfaceInterceptor()); IPhone iphone1 = container.Resolve (); iphone1.Call(); IPhone iphone2 = container.Resolve ("Apple"); IPhone iphone3 = container.Resolve ("Android"); IPhone iphone4 = container.Resolve (); IPhone iphone5 = container.Resolve (); } Console.WriteLine("*****************UnityContainer*********************"); { IUnityContainer container = new UnityContainer(); //container.RegisterType (new TransientLifetimeManager());瞬时 container.RegisterType (new ContainerControlledLifetimeManager());//容器单例 //container.RegisterType (new PerThreadLifetimeManager());//线程单例 IPhone iphone1 = null; Action act1 = new Action(() => { iphone1 = container.Resolve (); }); var result1 = act1.BeginInvoke(null, null); IPhone iphone2 = null; Action act2 = new Action(() => { iphone2 = container.Resolve (); }); var result2 = act2.BeginInvoke(null, null); act1.EndInvoke(result1); act2.EndInvoke(result2); //IPhone iphone1 = container.Resolve (); //IPhone iphone2 = container.Resolve (); 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 (); 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 (); phone.Call(); } } }}
using Ruanmou.Interface;using System;using System.Configuration;using System.Reflection;namespace Ruanmou.Project{ public class ObjectFactory { ////// 简单工厂+配置文件+反射 /// ///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{ ////// 1 Ioc(DI)介绍 /// 控制反转ioc 是目的 /// DI依赖注入 是手段 /// 2 Unity容器使用 /// 3 EF分层封装数据访问 /// 4 测试数据访问 /// 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().Find(2); //} //UserService service = new UserService(); ////User user = service.Find(2); //User user = service.Find (2); //IUserMenuService service = new UserMenuService(); //User user = service.Find (2); //service.UserLastLogin(user); IUnityContainer container = new UnityContainer(); container.RegisterType (); container.RegisterType (); container.RegisterType (); using (IUserMenuService service = container.Resolve ()) { User user = service.Find (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 (); //Category category = categoryService.Find(1); //categoryService.Update(category); //categoryService.Show(); //IBaseService iBaseServie = container.Resolve >(); //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(); } }}