Mercurial > pub > ImplabNet
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 |
rev | line source |
---|---|
274 | 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 | 4 |
5 namespace Implab.ServiceHost.Unity { | |
6 public class ContainerBuilder { | |
7 | |
8 readonly TypeResolver m_resolver; | |
9 | |
10 readonly UnityContainer m_container; | |
11 | |
12 readonly ContainerConfigurationSchema m_schema; | |
13 | |
14 public UnityContainer Container { | |
15 get { | |
16 return m_container; | |
17 } | |
18 } | |
19 | |
20 public ContainerBuilder() : this(null, null) { | |
21 } | |
22 | |
23 public ContainerBuilder(UnityContainer container, ContainerConfigurationSchema schema) { | |
24 m_container = container ?? new UnityContainer(); | |
25 m_resolver = new TypeResolver(); | |
26 m_schema = schema ?? ContainerConfigurationSchema.Default; | |
27 } | |
28 | |
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 | 31 } |
32 | |
277 | 33 |
34 internal void Visit(TypeAbstractRegistration typeRegistration) { | |
35 var registrationType = typeRegistration.GetRegistrationType(ResolveType); | |
36 var implementationType = typeRegistration.GetImplementationType(ResolveType) ?? registrationType; | |
274 | 37 |
38 var registrationContext = new TypeRegistrationBuilder( | |
39 m_resolver, | |
40 registrationType, | |
41 implementationType | |
42 ); | |
43 | |
277 | 44 typeRegistration.Visit(registrationContext); |
274 | 45 |
46 m_container.RegisterType( | |
47 registrationContext.RegistrationType, | |
48 registrationContext.ImplementationType, | |
277 | 49 typeRegistration.Name, |
50 registrationContext.Lifetime, | |
274 | 51 registrationContext.Injections |
52 ); | |
53 } | |
54 | |
277 | 55 internal void Visit(InstanceAbstractRegistration instanceRegistration) { |
56 var registrationType = instanceRegistration.GetRegistrationType(ResolveType); | |
57 | |
58 var builder = new InstanceRegistrationBuilder(m_resolver, registrationType); | |
274 | 59 |
277 | 60 instanceRegistration.Visit(builder); |
61 | |
274 | 62 m_container.RegisterInstance( |
277 | 63 builder.ValueBuilder.ValueType, |
64 instanceRegistration.Name, | |
65 builder.ValueBuilder.Value, | |
66 builder.Lifetime | |
274 | 67 ); |
68 } | |
69 | |
277 | 70 internal void Visit(FactoryAbstractRegistratrion factoryRgistration) { |
71 var registrationType = factoryRgistration.GetRegistrationType(ResolveType); | |
72 | |
73 var builder = new FactoryRegistrationBuilder(registrationType); | |
274 | 74 |
277 | 75 factoryRgistration.Visit(builder); |
76 | |
77 m_container.RegisterType( | |
78 builder.RegistrationType, | |
79 factoryRgistration.Name, | |
80 builder.Lifetime, | |
81 builder.Factory | |
274 | 82 ); |
83 } | |
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 | 114 } |
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 | 142 } |
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 | 164 } |
165 | |
166 public void Include(string file) { | |
167 var includeContext = new ContainerBuilder(m_container, m_schema); | |
168 includeContext.LoadConfig(file); | |
169 } | |
170 | |
171 public void LoadConfig(string file) { | |
172 var config = m_schema.LoadFile(file); | |
277 | 173 config.Visit(this); |
274 | 174 } |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
175 |
274 | 176 } |
177 } |