annotate Implab.ServiceHost/Unity/TypeRegistrationBuilder.cs @ 280:f07be402ab02 v3

Added Trace<T>.Debug(...) method for debug messages Added ContainerBuilde.LoadConfig(Uri) method
author cin
date Fri, 25 May 2018 19:15:26 +0300
parents 8714471e8d78
children
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;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
2 using System.Collections.Generic;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
3 using System.Linq;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
4 using Unity.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5 using Unity.Registration;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 public class TypeRegistrationBuilder : RegistrationBuilder {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
10 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 readonly List<InjectionMember> m_injections = new List<InjectionMember>();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
14 internal InjectionMember[] Injections { get { return m_injections.ToArray(); } }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16 public Type ImplementationType {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17 get;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18 private set;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
20
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 internal TypeRegistrationBuilder(TypeResolver resolver, Type registrationType, Type implementationType) : base(registrationType) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22 ImplementationType = implementationType;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 // when registering a generic mapping, register all generic parameter names as local types
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 if (ImplementationType.IsGenericTypeDefinition) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26 m_resolver = new TypeResolver(resolver);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28 foreach (var p in ImplementationType.GetGenericArguments())
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
29 m_resolver.AddMapping(p.Name, p);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
30 } else {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31 m_resolver = resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
33 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
34
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
35 internal void Visit(ConstructorInjectionElement constructorInjection) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
36
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38 var parameters = constructorInjection.Parameters?
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
39 .Select(x => {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
40 var valueBuilder = new InjectionParameterBuilder(m_resolver, null);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
41 x.Visit(valueBuilder);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
42 return valueBuilder.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
43 })
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
44 .ToArray();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
45
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
46 var injection = parameters != null ? new InjectionConstructor(parameters) : new InjectionConstructor();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
47 m_injections.Add(injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
48 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
49
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
50 internal void Visit(MethodInjectionElement methodInjection) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
51 var parameters = methodInjection.Parameters?
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
52 .Select(x => {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
53 var valueBuilder = new InjectionParameterBuilder(m_resolver, null);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
54 x.Visit(valueBuilder);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
55 return valueBuilder.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
56 })
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
57 .ToArray();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
58
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59 var injection = parameters != null ? new InjectionMethod(methodInjection.Name, parameters) : new InjectionMethod(methodInjection.Name);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
60 m_injections.Add(injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
61 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
63 internal void Visit(PropertyInjectionElement propertyInjection) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
64 if (propertyInjection.Value == null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
65 throw new Exception($"A value value must be specified for the property '{propertyInjection.Name}'");
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
66
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
67 var propertyType = ImplementationType.GetProperty(propertyInjection.Name)?.PropertyType;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
68 var valueContext = new InjectionParameterBuilder(m_resolver, propertyType);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
69
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
70 propertyInjection.Value.Visit(valueContext);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
71 var injection = new InjectionProperty(propertyInjection.Name, valueContext.Injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
72 m_injections.Add(injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
73 }
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
74
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
75 public void AddInjectionMember(InjectionMember injection) {
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
76 Safe.ArgumentNotNull(injection, nameof(injection));
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
77
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
78 m_injections.Add(injection);
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
79 }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
80 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
81 }