Unity 容器常见使用声景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /*****************************************************/ //Create Class With Interface Dependencies #region //需要某实现某接口的具体实例 UnityContainer container= new UnityContainer(); container.RegisterType(ILogger,NullLogger>(); Database datavase = container.Resolve<CustomDatabase>Database(); #endregion CustomDatabase的构造函数包含ILogger参数 CustomDatabase(ILogger log){} /*****************************************************/ //Create TypeMapping-Interface UnityContainer container= new UnityContainer(); container.RegisterType(ILogger,NullLogger>(); ILogger logger = container.Resolve<ILogger>(); /*****************************************************/ //Register Singleton UnityContainer container= new UnityContaier(); container.RegisterType<Database,SqlDatabase> ( new ContainerControlledLifetimeManager()); /*****************************************************/ //Registering Named Instances UnityContainer container= new UnityContaier(); container.RegisterType<Database,SqlDatabase>( "SQL" ); container.RegisterType<Database,OracleDatabase>( "ORACLE" ); IEnumerable<Database> databases = container.ResolveAll<Database>(); //Return All named instances Database database = container.Resolve<Database>( "SQL" ); /*****************************************************/ //Register Existing Intance //Registering Named Instances UnityContainer container= new UnityContaier(); container.RegisterInstance<Database>( new SqlDatabase()); //注册instance,Unity认为它是Singleton container.RegisterInstance<Database>( "ORACLE" , new OracleDatabase()); Database database = container.Resolve<Database>( "ORACLE" ); /*****************************************************/ //Buildup Existing Instace UnityContainer container= new UnityContainer(); container.RegisterType(ILogger,NullLogger>(); DB2Database existingDatabase = new DB2Database(); container.BuildUp(existingDatabase); //创建引用关系 container.RegisterInstance<Database>(existingDatabase); Database database = container.Resolve<Database>(); //----- public class DB2Database:Database{ [Dependency] public ILogger Logger{ get ; set } } /*****************************************************/ // Configure Container via Config File UnityContainer _contaier= new UnityContaier(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection( "unity" ); section.Configure(_container); // Unnamed <container> element ICustomerDataSource datasource=_container.Resolve<ICustomerDataSource>(); /*****************************************************/ container.Configure<InjectedMembers>() .ConfigureInjectionFor<GenericDatabase>( new ConfigurationManager.ConnectionStrings[ "ConnectionString" ].ConnectionString new InjectionProperty( "Logger" ) ); Database database=container.Resolve<Database>(); /*****************************************************/ //NestedContainers 父子容器,适用于,有公共容器。树型结构的容器o UnityContainer parentContainer = new UnityContaier(); IUnityContaier childContainer1 = parentContaier.CreateChildContainer(); IUnityContaier childContainer2 = parentContaier.CreateChildContainer(); /... |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路