annotate Implab.ServiceHost/Unity/ContainerBuilder.cs @ 278:6691aff01de1 v3

Implab: added XmlDefaultSeializer (SerializersPool is now obsolete) Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author cin
date Thu, 03 May 2018 09:59:44 +0300
parents 963b17c275be
children 8714471e8d78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
1 using System;
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
2 using System.Reflection;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
3 using Unity;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
4
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6 public class ContainerBuilder {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
10 readonly UnityContainer m_container;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 readonly ContainerConfigurationSchema m_schema;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
14 public UnityContainer Container {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16 return m_container;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
20 public ContainerBuilder() : this(null, null) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23 public ContainerBuilder(UnityContainer container, ContainerConfigurationSchema schema) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 m_container = container ?? new UnityContainer();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 m_resolver = new TypeResolver();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26 m_schema = schema ?? ContainerConfigurationSchema.Default;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
29 public Type ResolveType(string typeReference) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
30 return m_resolver.Resolve(typeReference, true);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
33
963b17c275be Refactoring
cin
parents: 274
diff changeset
34 internal void Visit(TypeAbstractRegistration typeRegistration) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
35 var registrationType = typeRegistration.GetRegistrationType(ResolveType);
963b17c275be Refactoring
cin
parents: 274
diff changeset
36 var implementationType = typeRegistration.GetImplementationType(ResolveType) ?? registrationType;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38 var registrationContext = new TypeRegistrationBuilder(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
39 m_resolver,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
40 registrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
41 implementationType
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
42 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
43
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
44 typeRegistration.Visit(registrationContext);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
45
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
46 m_container.RegisterType(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
47 registrationContext.RegistrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
48 registrationContext.ImplementationType,
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
49 typeRegistration.Name,
963b17c275be Refactoring
cin
parents: 274
diff changeset
50 registrationContext.Lifetime,
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
51 registrationContext.Injections
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
52 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
53 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
54
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
55 internal void Visit(InstanceAbstractRegistration instanceRegistration) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
56 var registrationType = instanceRegistration.GetRegistrationType(ResolveType);
963b17c275be Refactoring
cin
parents: 274
diff changeset
57
963b17c275be Refactoring
cin
parents: 274
diff changeset
58 var builder = new InstanceRegistrationBuilder(m_resolver, registrationType);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
60 instanceRegistration.Visit(builder);
963b17c275be Refactoring
cin
parents: 274
diff changeset
61
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62 m_container.RegisterInstance(
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
63 builder.ValueBuilder.ValueType,
963b17c275be Refactoring
cin
parents: 274
diff changeset
64 instanceRegistration.Name,
963b17c275be Refactoring
cin
parents: 274
diff changeset
65 builder.ValueBuilder.Value,
963b17c275be Refactoring
cin
parents: 274
diff changeset
66 builder.Lifetime
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
67 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
68 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
69
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
70 internal void Visit(FactoryAbstractRegistratrion factoryRgistration) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
71 var registrationType = factoryRgistration.GetRegistrationType(ResolveType);
963b17c275be Refactoring
cin
parents: 274
diff changeset
72
963b17c275be Refactoring
cin
parents: 274
diff changeset
73 var builder = new FactoryRegistrationBuilder(registrationType);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
74
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
75 factoryRgistration.Visit(builder);
963b17c275be Refactoring
cin
parents: 274
diff changeset
76
963b17c275be Refactoring
cin
parents: 274
diff changeset
77 m_container.RegisterType(
963b17c275be Refactoring
cin
parents: 274
diff changeset
78 builder.RegistrationType,
963b17c275be Refactoring
cin
parents: 274
diff changeset
79 factoryRgistration.Name,
963b17c275be Refactoring
cin
parents: 274
diff changeset
80 builder.Lifetime,
963b17c275be Refactoring
cin
parents: 274
diff changeset
81 builder.Factory
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
82 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
83 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
84
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
85 public void Visit(ITypeRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
86 Safe.ArgumentNotNull(registration, nameof(registration));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
87
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
88 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
89 var implementationType = registration.GetImplementationType(this) ?? registrationType;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
90
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
91 if (registrationType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
92 throw new Exception($"A type must be specified for the registration {registration.Name}");
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
93
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
94 var builder = new TypeRegistrationBuilder(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
95 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
96 registrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
97 implementationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
98 );
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
99
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
100 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
101
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
102 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
103 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
104 member.Visit(builder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
105 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
106
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
107 m_container.RegisterType(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
108 builder.RegistrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
109 builder.ImplementationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
110 registration.Name,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
111 builder.Lifetime,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
112 builder.Injections
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
113 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
114 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
115
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
116 public void Visit(IInstanceRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
117 Safe.ArgumentNotNull(registration, nameof(registration));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
118
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
119 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
120
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
121 var builder = new InstanceRegistrationBuilder (
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
122 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
123 registrationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
124 );
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
125
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
126 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
127
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
128 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
129 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
130 member.Visit(builder.ValueBuilder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
131 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
132
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
133 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
134 throw new Exception($"A type must be specified for the registration {registration.Name}");
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
135
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
136 m_container.RegisterInstance(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
137 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
138 registration.Name,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
139 builder.ValueBuilder.Injection,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
140 builder.Lifetime
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
141 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
142 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
143
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
144 public void Visit(IFactoryRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
145 Safe.ArgumentNotNull(registration, nameof(registration));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
146
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
147 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
148
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
149 var builder = new FactoryRegistrationBuilder(registrationType);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
150
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
151 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
152 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
153 member?.Visit(builder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
154 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
155
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
156 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
157
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
158 public void AddNamespace(string ns) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
159 m_resolver.AddNamespace(ns);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
160 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
161
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
162 public void AddAssembly(string assembly) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
163
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
164 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
165
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
166 public void Include(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
167 var includeContext = new ContainerBuilder(m_container, m_schema);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
168 includeContext.LoadConfig(file);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
169 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
170
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
171 public void LoadConfig(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
172 var config = m_schema.LoadFile(file);
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
173 config.Visit(this);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
174 }
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
175
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
176 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
177 }