annotate Implab.ServiceHost/Unity/InjectionValueBuilder.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;
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
2 using System.Collections.Generic;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
3 using System.ComponentModel;
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
4 using System.Linq;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5 using System.Xml.Serialization;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6 using Unity.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
10 public class InjectionParameterBuilder {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
14 public Type DefaultType { get; private set; }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
16 public Type ValueType { get; private set; }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18 public object Value { get; set; }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
20 internal InjectionParameterValue Injection {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22 if (Value != null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23 return InjectionParameterValue.ToParameter(Value);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 return new InjectionParameter(ValueType, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
29 internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
30 m_resolver = resolver;
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
31 DefaultType = defaultType;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
33
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
34 public Type ResolveInjectedValueType(string typeSpec) {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
35 if (string.IsNullOrEmpty(typeSpec)) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
36 if (DefaultType == null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37 throw new Exception("The type must be specified");
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38 return DefaultType;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
39 }
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
40 return m_resolver.Resolve(typeSpec, true);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
41 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
42
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
43 public Type ResolveType(string typeSpec) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
44 return m_resolver.Resolve(typeSpec, true);
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
45 }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
46
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
47 public void SetValue(Type type, object value) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
48 ValueType = type;
963b17c275be Refactoring
cin
parents: 274
diff changeset
49 Value = value;
963b17c275be Refactoring
cin
parents: 274
diff changeset
50 }
963b17c275be Refactoring
cin
parents: 274
diff changeset
51
963b17c275be Refactoring
cin
parents: 274
diff changeset
52 public void SetValue<T>(T value) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
53 SetValue(typeof(T), value);
963b17c275be Refactoring
cin
parents: 274
diff changeset
54 }
963b17c275be Refactoring
cin
parents: 274
diff changeset
55
963b17c275be Refactoring
cin
parents: 274
diff changeset
56 public void SetDependencyReference(Type type, string name, bool optional) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
57 ValueType = type;
963b17c275be Refactoring
cin
parents: 274
diff changeset
58 Value = optional ? (object)new OptionalParameter(type, name) : new ResolvedParameter(type, name);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
60
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
61 internal void Visit(ArrayParameterElement arrayParameter) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
62 Type itemsType = null;
963b17c275be Refactoring
cin
parents: 274
diff changeset
63 var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
64
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
65 if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
66 itemsType = ResolveType(arrayParameter.ItemsType);
963b17c275be Refactoring
cin
parents: 274
diff changeset
67 if (arrayType == null)
963b17c275be Refactoring
cin
parents: 274
diff changeset
68 arrayType = itemsType.MakeArrayType();
963b17c275be Refactoring
cin
parents: 274
diff changeset
69 } else {
963b17c275be Refactoring
cin
parents: 274
diff changeset
70 itemsType = arrayType?.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0];
963b17c275be Refactoring
cin
parents: 274
diff changeset
71 }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
72
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
73 if (itemsType == null)
963b17c275be Refactoring
cin
parents: 274
diff changeset
74 throw new Exception("Failed to determine array elements type");
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
75
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
76 InjectionParameterValue[] injections = (arrayParameter.Items ?? new InjectionParameterElement[0])
963b17c275be Refactoring
cin
parents: 274
diff changeset
77 .Select(x => {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
78 var builder = new InjectionParameterBuilder(m_resolver, itemsType);
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
79 x.Visit(builder);
963b17c275be Refactoring
cin
parents: 274
diff changeset
80 return builder.Injection;
963b17c275be Refactoring
cin
parents: 274
diff changeset
81 })
963b17c275be Refactoring
cin
parents: 274
diff changeset
82 .ToArray();
963b17c275be Refactoring
cin
parents: 274
diff changeset
83
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
84 var array = itemsType.IsGenericParameter ?
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
85 (object)new GenericResolvedArrayParameter(itemsType.Name, injections) :
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
86 new ResolvedArrayParameter(itemsType, injections);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
87
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
88 ValueType = arrayType;
963b17c275be Refactoring
cin
parents: 274
diff changeset
89 Value = array;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
90 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
91 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
92 }