annotate Implab.ServiceHost/Unity/TypeRegistrationBuilder.cs @ 274:22629bf26121 v3

Unity xml configuration, alpha2
author cin
date Fri, 27 Apr 2018 04:47:52 +0300
parents
children 963b17c275be
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
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
14 public InjectionMember[] Injections { get { return m_injections.ToArray(); } }
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 => {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
40 var valueBuilder = new InjectionValueBuilder(m_resolver, null);
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 valueContext = new InjectionValueBuilder(m_resolver, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
52
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
53 var parameters = methodInjection.Parameters?
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
54 .Select(x => {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
55 var valueBuilder = new InjectionValueBuilder(m_resolver, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
56 x.Visit(valueBuilder);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
57 return valueBuilder.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
58 })
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59 .ToArray();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
60
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
61 var injection = parameters != null ? new InjectionMethod(methodInjection.Name, parameters) : new InjectionMethod(methodInjection.Name);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62 m_injections.Add(injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
63 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
64
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
65 internal void Visit(PropertyInjectionElement propertyInjection) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
66 if (propertyInjection.Value == null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
67 throw new Exception($"A value value must be specified for the property '{propertyInjection.Name}'");
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
68
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
69 var propertyType = RegistrationType.GetProperty(propertyInjection.Name)?.PropertyType;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
70 var valueContext = new InjectionValueBuilder(m_resolver, propertyType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
71
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
72 propertyInjection.Value.Visit(valueContext);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
73 var injection = new InjectionProperty(propertyInjection.Name, valueContext.Injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
74 m_injections.Add(injection);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
75 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
76 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
77 }