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();
/...

  

posted @   庚武  Views(273)  Comments(0Edit  收藏  举报
编辑推荐:
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
阅读排行:
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路
点击右上角即可分享
微信分享提示