Mercurial > pub > ImplabNet
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 |
rev | line source |
---|---|
274 | 1 using System; |
280 | 2 using System.IO; |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
3 using System.Reflection; |
280 | 4 using Implab.Diagnostics; |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
5 using Unity; |
274 | 6 |
7 namespace Implab.ServiceHost.Unity { | |
280 | 8 using static Trace<ContainerBuilder>; |
9 | |
274 | 10 public class ContainerBuilder { |
11 | |
12 readonly TypeResolver m_resolver; | |
13 | |
279 | 14 readonly IUnityContainer m_container; |
274 | 15 |
16 readonly ContainerConfigurationSchema m_schema; | |
17 | |
280 | 18 Uri m_location; |
19 | |
279 | 20 public IUnityContainer Container { |
274 | 21 get { |
22 return m_container; | |
23 } | |
24 } | |
25 | |
26 public ContainerBuilder() : this(null, null) { | |
27 } | |
28 | |
279 | 29 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) { |
274 | 30 m_container = container ?? new UnityContainer(); |
31 m_resolver = new TypeResolver(); | |
32 m_schema = schema ?? ContainerConfigurationSchema.Default; | |
33 } | |
34 | |
35 public Type ResolveType(string typeReference) { | |
279 | 36 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true); |
274 | 37 } |
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 | 68 } |
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 | 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 | 96 } |
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 | 104 } |
105 | |
280 | 106 /// <summary> |
107 /// Includes the confguration. Creates a new <see cref="ContainerBuilder"/>, | |
108 /// and loads the configuration to it. The created builder will share the | |
109 /// container and will have its own isolated type resolver. | |
110 /// </summary> | |
111 /// <param name="file">A path to configuration relative to the current configuration.</param> | |
274 | 112 public void Include(string file) { |
113 var includeContext = new ContainerBuilder(m_container, m_schema); | |
280 | 114 |
115 if (m_location != null) { | |
116 var uri = new Uri(m_location, file); | |
117 includeContext.LoadConfig(uri); | |
118 } else { | |
119 includeContext.LoadConfig(file); | |
120 } | |
274 | 121 } |
122 | |
280 | 123 /// <summary> |
124 /// Loads a configuration from the specified local file. | |
125 /// </summary> | |
126 /// <param name="file">The path to the configuration file.</param> | |
274 | 127 public void LoadConfig(string file) { |
280 | 128 Safe.ArgumentNotEmpty(file, nameof(file)); |
129 | |
130 LoadConfig(new Uri(Path.GetFullPath(file))); | |
131 } | |
132 | |
133 public void LoadConfig(Uri location) { | |
134 Safe.ArgumentNotNull(location, nameof(location)); | |
135 | |
136 m_location = location; | |
137 | |
138 var config = m_schema.LoadConfig(location.ToString()); | |
277 | 139 config.Visit(this); |
274 | 140 } |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
141 |
274 | 142 } |
143 } |