annotate Implab.ServiceHost/Unity/ContainerBuilder.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents 6691aff01de1
children f07be402ab02
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;
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
2 using System.Reflection;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
3 using Unity;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
4
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6 public class ContainerBuilder {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
10 readonly IUnityContainer m_container;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 readonly ContainerConfigurationSchema m_schema;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
14 public IUnityContainer Container {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16 return m_container;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
20 public ContainerBuilder() : this(null, null) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
23 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 m_container = container ?? new UnityContainer();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 m_resolver = new TypeResolver();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26 m_schema = schema ?? ContainerConfigurationSchema.Default;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
29 public Type ResolveType(string typeReference) {
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
30 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
33 public void Visit(ITypeRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
34 Safe.ArgumentNotNull(registration, nameof(registration));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
35
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
36 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
37 var implementationType = registration.GetImplementationType(this) ?? registrationType;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
38
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
39 if (registrationType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
40 throw new Exception($"A type must be specified for the registration {registration.Name}");
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
41
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
42 var builder = new TypeRegistrationBuilder(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
43 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
44 registrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
45 implementationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
46 );
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
47
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
48 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
49
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
50 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
51 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
52 member.Visit(builder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
53 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
54
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
55 m_container.RegisterType(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
56 builder.RegistrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
57 builder.ImplementationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
58 registration.Name,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
59 builder.Lifetime,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
60 builder.Injections
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
61 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
63
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
64 public void Visit(IInstanceRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
65 Safe.ArgumentNotNull(registration, nameof(registration));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
66
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
67 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
68
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
69 var builder = new InstanceRegistrationBuilder (
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
70 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
71 registrationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
72 );
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
73
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
74 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
75
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
76 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
77 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
78 member.Visit(builder.ValueBuilder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
79 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
80
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
81 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
82 throw new Exception($"A type must be specified for the registration {registration.Name}");
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
83
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
84 m_container.RegisterInstance(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
85 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
86 registration.Name,
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
87 builder.ValueBuilder.Value,
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
88 builder.Lifetime
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
89 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
90 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
91
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
92 public void AddNamespace(string ns) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
93 m_resolver.AddNamespace(ns);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
94 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
95
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
96 public void AddAssembly(string assembly) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
97
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
98 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
99
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
100 public void Include(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
101 var includeContext = new ContainerBuilder(m_container, m_schema);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
102 includeContext.LoadConfig(file);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
103 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
104
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
105 public void LoadConfig(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
106 var config = m_schema.LoadFile(file);
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
107 config.Visit(this);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
108 }
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
109
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
110 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
111 }