Mercurial > pub > ImplabNet
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 |
| rev | line source |
|---|---|
| 274 | 1 using System; |
| 2 using System.Collections.Generic; | |
| 3 using System.Linq; | |
| 4 using Unity.Injection; | |
| 5 using Unity.Registration; | |
| 6 | |
| 7 namespace Implab.ServiceHost.Unity { | |
| 8 public class TypeRegistrationBuilder : RegistrationBuilder { | |
| 9 | |
| 10 readonly TypeResolver m_resolver; | |
| 11 | |
| 12 readonly List<InjectionMember> m_injections = new List<InjectionMember>(); | |
| 13 | |
| 277 | 14 internal InjectionMember[] Injections { get { return m_injections.ToArray(); } } |
| 274 | 15 |
| 16 public Type ImplementationType { | |
| 17 get; | |
| 18 private set; | |
| 19 } | |
| 20 | |
| 21 internal TypeRegistrationBuilder(TypeResolver resolver, Type registrationType, Type implementationType) : base(registrationType) { | |
| 22 ImplementationType = implementationType; | |
| 23 | |
| 24 // when registering a generic mapping, register all generic parameter names as local types | |
| 25 if (ImplementationType.IsGenericTypeDefinition) { | |
| 26 m_resolver = new TypeResolver(resolver); | |
| 27 | |
| 28 foreach (var p in ImplementationType.GetGenericArguments()) | |
| 29 m_resolver.AddMapping(p.Name, p); | |
| 30 } else { | |
| 31 m_resolver = resolver; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 internal void Visit(ConstructorInjectionElement constructorInjection) { | |
| 36 | |
| 37 | |
| 38 var parameters = constructorInjection.Parameters? | |
| 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 | 41 x.Visit(valueBuilder); |
| 42 return valueBuilder.Injection; | |
| 43 }) | |
| 44 .ToArray(); | |
| 45 | |
| 46 var injection = parameters != null ? new InjectionConstructor(parameters) : new InjectionConstructor(); | |
| 47 m_injections.Add(injection); | |
| 48 } | |
| 49 | |
| 50 internal void Visit(MethodInjectionElement methodInjection) { | |
| 51 var parameters = methodInjection.Parameters? | |
| 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 | 54 x.Visit(valueBuilder); |
| 55 return valueBuilder.Injection; | |
| 56 }) | |
| 57 .ToArray(); | |
| 58 | |
| 59 var injection = parameters != null ? new InjectionMethod(methodInjection.Name, parameters) : new InjectionMethod(methodInjection.Name); | |
| 60 m_injections.Add(injection); | |
| 61 } | |
| 62 | |
| 63 internal void Visit(PropertyInjectionElement propertyInjection) { | |
| 64 if (propertyInjection.Value == null) | |
| 65 throw new Exception($"A value value must be specified for the property '{propertyInjection.Name}'"); | |
| 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 | 69 |
| 70 propertyInjection.Value.Visit(valueContext); | |
| 71 var injection = new InjectionProperty(propertyInjection.Name, valueContext.Injection); | |
| 72 m_injections.Add(injection); | |
| 73 } | |
| 279 | 74 |
| 75 public void AddInjectionMember(InjectionMember injection) { | |
| 76 Safe.ArgumentNotNull(injection, nameof(injection)); | |
| 77 | |
| 78 m_injections.Add(injection); | |
| 79 } | |
| 274 | 80 } |
| 81 } |
