comparison 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
comparison
equal deleted inserted replaced
277:963b17c275be 278:6691aff01de1
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Reflection;
3 using System.Text.RegularExpressions; 3 using Unity;
4 using Implab.Diagnostics;
5 4
6 namespace Implab.ServiceHost.Unity { 5 namespace Implab.ServiceHost.Unity {
7 using System.Linq;
8 using System.Reflection;
9 using System.Text;
10 using global::Unity;
11 using global::Unity.Registration;
12 using Implab.Xml;
13 using static Trace<ContainerBuilder>;
14
15 public class ContainerBuilder { 6 public class ContainerBuilder {
16 7
17 readonly TypeResolver m_resolver; 8 readonly TypeResolver m_resolver;
18 9
19 readonly UnityContainer m_container; 10 readonly UnityContainer m_container;
34 m_resolver = new TypeResolver(); 25 m_resolver = new TypeResolver();
35 m_schema = schema ?? ContainerConfigurationSchema.Default; 26 m_schema = schema ?? ContainerConfigurationSchema.Default;
36 } 27 }
37 28
38 public Type ResolveType(string typeReference) { 29 public Type ResolveType(string typeReference) {
39 return m_resolver.Resolve(typeReference); 30 return m_resolver.Resolve(typeReference, true);
40 } 31 }
41 32
42 33
43 internal void Visit(TypeAbstractRegistration typeRegistration) { 34 internal void Visit(TypeAbstractRegistration typeRegistration) {
44 var registrationType = typeRegistration.GetRegistrationType(ResolveType); 35 var registrationType = typeRegistration.GetRegistrationType(ResolveType);
89 builder.Lifetime, 80 builder.Lifetime,
90 builder.Factory 81 builder.Factory
91 ); 82 );
92 } 83 }
93 84
94 internal void Visit(NamespaceElement namespaceElement) { 85 public void Visit(ITypeRegistration registration) {
95 m_resolver.AddNamespace(namespaceElement.Name); 86 Safe.ArgumentNotNull(registration, nameof(registration));
87
88 var registrationType = registration.GetRegistrationType(this);
89 var implementationType = registration.GetImplementationType(this) ?? registrationType;
90
91 if (registrationType == null)
92 throw new Exception($"A type must be specified for the registration {registration.Name}");
93
94 var builder = new TypeRegistrationBuilder(
95 m_resolver,
96 registrationType,
97 implementationType
98 );
99
100 builder.Lifetime = registration.GetLifetime(this);
101
102 if (registration.MemberInjections != null) {
103 foreach(var member in registration.MemberInjections)
104 member.Visit(builder);
105 }
106
107 m_container.RegisterType(
108 builder.RegistrationType,
109 builder.ImplementationType,
110 registration.Name,
111 builder.Lifetime,
112 builder.Injections
113 );
96 } 114 }
97 115
98 internal void Visit(AssemblyElement assemblyElement) { 116 public void Visit(IInstanceRegistration registration) {
99 Assembly.Load(assemblyElement.AssemblyName); 117 Safe.ArgumentNotNull(registration, nameof(registration));
118
119 var registrationType = registration.GetRegistrationType(this);
120
121 var builder = new InstanceRegistrationBuilder (
122 m_resolver,
123 registrationType
124 );
125
126 builder.Lifetime = registration.GetLifetime(this);
127
128 if (registration.MemberInjections != null) {
129 foreach(var member in registration.MemberInjections)
130 member.Visit(builder.ValueBuilder);
131 }
132
133 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
134 throw new Exception($"A type must be specified for the registration {registration.Name}");
135
136 m_container.RegisterInstance(
137 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
138 registration.Name,
139 builder.ValueBuilder.Injection,
140 builder.Lifetime
141 );
100 } 142 }
101 143
102 internal void Visit(IncludeElement includeElement) { 144 public void Visit(IFactoryRegistration registration) {
103 Include(includeElement.Href); 145 Safe.ArgumentNotNull(registration, nameof(registration));
146
147 var registrationType = registration.GetRegistrationType(this);
148
149 var builder = new FactoryRegistrationBuilder(registrationType);
150
151 if (registration.MemberInjections != null) {
152 foreach(var member in registration.MemberInjections)
153 member?.Visit(builder);
154 }
155
156 }
157
158 public void AddNamespace(string ns) {
159 m_resolver.AddNamespace(ns);
160 }
161
162 public void AddAssembly(string assembly) {
163
104 } 164 }
105 165
106 public void Include(string file) { 166 public void Include(string file) {
107 var includeContext = new ContainerBuilder(m_container, m_schema); 167 var includeContext = new ContainerBuilder(m_container, m_schema);
108 includeContext.LoadConfig(file); 168 includeContext.LoadConfig(file);
109 } 169 }
110 170
111 public void LoadConfig(string file) { 171 public void LoadConfig(string file) {
112 var config = m_schema.LoadFile(file); 172 var config = m_schema.LoadFile(file);
113
114 config.Visit(this); 173 config.Visit(this);
115 } 174 }
116 175
117 } 176 }
118 } 177 }