annotate Implab.ServiceHost/Unity/ContainerBuilder.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents f07be402ab02
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;
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
2 using System.IO;
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
3 using System.Reflection;
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
4 using Implab.Diagnostics;
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
5 using Unity;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7 namespace Implab.ServiceHost.Unity {
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
8 using static Trace<ContainerBuilder>;
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
9
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
10 public class ContainerBuilder {
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
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
14 readonly IUnityContainer m_container;
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16 readonly ContainerConfigurationSchema m_schema;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
18 Uri m_location;
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
19
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
20 public IUnityContainer Container {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22 return m_container;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26 public ContainerBuilder() : this(null, null) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
29 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
30 m_container = container ?? new UnityContainer();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31 m_resolver = new TypeResolver();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32 m_schema = schema ?? ContainerConfigurationSchema.Default;
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 public Type ResolveType(string typeReference) {
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
36 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
39 public void Visit(ITypeRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
40 Safe.ArgumentNotNull(registration, nameof(registration));
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 registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
43 var implementationType = registration.GetImplementationType(this) ?? registrationType;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
44
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
45 if (registrationType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
46 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
47
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
48 var builder = new TypeRegistrationBuilder(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
49 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
50 registrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
51 implementationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
52 );
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 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
55
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
56 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
57 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
58 member.Visit(builder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
59 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
60
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
61 m_container.RegisterType(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
62 builder.RegistrationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
63 builder.ImplementationType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
64 registration.Name,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
65 builder.Lifetime,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
66 builder.Injections
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
67 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
68 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
69
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
70 public void Visit(IInstanceRegistration registration) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
71 Safe.ArgumentNotNull(registration, nameof(registration));
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 var registrationType = registration.GetRegistrationType(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
74
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
75 var builder = new InstanceRegistrationBuilder (
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
76 m_resolver,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
77 registrationType
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
78 );
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 builder.Lifetime = registration.GetLifetime(this);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
81
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
82 if (registration.MemberInjections != null) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
83 foreach(var member in registration.MemberInjections)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
84 member.Visit(builder.ValueBuilder);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
85 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
86
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
87 if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
88 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
89
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
90 m_container.RegisterInstance(
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
91 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
92 registration.Name,
279
8714471e8d78 Container configuration cleanup, RC2
cin
parents: 278
diff changeset
93 builder.ValueBuilder.Value,
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
94 builder.Lifetime
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
95 );
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
96 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
97
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
98 public void AddNamespace(string ns) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
99 m_resolver.AddNamespace(ns);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
100 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
101
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
102 public void AddAssembly(string assembly) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
103
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
104 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
105
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
106 /// <summary>
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
107 /// Includes the confguration. Creates a new <see cref="ContainerBuilder"/>,
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
108 /// and loads the configuration to it. The created builder will share the
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
109 /// container and will have its own isolated type resolver.
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
110 /// </summary>
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
111 /// <param name="file">A path to configuration relative to the current configuration.</param>
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
112 public void Include(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
113 var includeContext = new ContainerBuilder(m_container, m_schema);
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
114
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
115 if (m_location != null) {
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
116 var uri = new Uri(m_location, file);
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
117 includeContext.LoadConfig(uri);
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
118 } else {
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
119 includeContext.LoadConfig(file);
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
120 }
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
121 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
122
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
123 /// <summary>
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
124 /// Loads a configuration from the specified local file.
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
125 /// </summary>
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
126 /// <param name="file">The path to the configuration file.</param>
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
127 public void LoadConfig(string file) {
280
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
128 Safe.ArgumentNotEmpty(file, nameof(file));
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
129
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
130 LoadConfig(new Uri(Path.GetFullPath(file)));
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
131 }
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
132
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
133 public void LoadConfig(Uri location) {
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
134 Safe.ArgumentNotNull(location, nameof(location));
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
135
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
136 m_location = location;
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
137
f07be402ab02 Added Trace<T>.Debug(...) method for debug messages
cin
parents: 279
diff changeset
138 var config = m_schema.LoadConfig(location.ToString());
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
139 config.Visit(this);
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
140 }
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
141
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
142 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
143 }