annotate Implab.ServiceHost/Unity/ContainerBuilder.cs @ 275:6fefd5811b9b v3

refactoring
author cin
date Fri, 27 Apr 2018 16:57:30 +0300
parents 22629bf26121
children 963b17c275be
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;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
2 using System.Collections.Generic;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
3 using System.Text.RegularExpressions;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
4 using Implab.Diagnostics;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7 using System.Linq;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 using System.Reflection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9 using System.Text;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
10 using global::Unity;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11 using global::Unity.Registration;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 using Implab.Xml;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13 using static Trace<ContainerBuilder>;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
14
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15 public class ContainerBuilder {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19 readonly UnityContainer m_container;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
20
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 readonly ContainerConfigurationSchema m_schema;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23 public UnityContainer Container {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 return m_container;
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
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
29 public ContainerBuilder() : this(null, null) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
30 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32 public ContainerBuilder(UnityContainer container, ContainerConfigurationSchema schema) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
33 m_container = container ?? new UnityContainer();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
34 m_resolver = new TypeResolver();
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
35 m_schema = schema ?? ContainerConfigurationSchema.Default;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
36 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38 public Type ResolveType(string typeReference) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
39 return m_resolver.Resolve(typeReference);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
40 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
41
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
42 internal void Visit(RegisterElement registerElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
43 var registrationType = ResolveType(registerElement.RegistrationType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
44 var implementationType = string.IsNullOrEmpty(registerElement.MapToType) ? registrationType : ResolveType(registerElement.MapToType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
45
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
46 var registrationContext = new TypeRegistrationBuilder(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
47 m_resolver,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
48 registrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
49 implementationType
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
50 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
51
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
52 if (registerElement.Injectors != null) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
53 foreach (var injector in registerElement.Injectors) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
54 injector.Visit(registrationContext);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
55 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
56 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
57
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
58 m_container.RegisterType(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59 registrationContext.RegistrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
60 registrationContext.ImplementationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
61 registerElement.Name,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62 registerElement.Lifetime?.GetLifetimeManager(this),
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
63 registrationContext.Injections
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
64 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
65 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
66
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
67 internal void Visit(SerializedElement serializedElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
68 var registrationType = ResolveType(serializedElement.RegistrationType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
69 var valueBuilder = new InjectionValueBuilder(m_resolver, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
70
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
71 valueBuilder.Visit(serializedElement);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
72
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
73 m_container.RegisterInstance(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
74 registrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
75 serializedElement.Name,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
76 valueBuilder.Value,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
77 serializedElement.Lifetime?.GetLifetimeManager(this)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
78 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
79 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
80
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
81 internal void Visit(ValueElement valueElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
82 var registrationType = ResolveType(valueElement.RegistrationType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
83 var valueBuilder = new InjectionValueBuilder(m_resolver, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
84
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
85 valueBuilder.Visit(valueElement);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
86
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
87 m_container.RegisterInstance(
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
88 registrationType,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
89 valueElement.Name,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
90 valueBuilder.Value,
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
91 valueElement.Lifetime?.GetLifetimeManager(this)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
92 );
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
93 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
94
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
95 internal void Visit(NamespaceElement namespaceElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
96 m_resolver.AddNamespace(namespaceElement.Name);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
97 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
98
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
99 internal void Visit(AssemblyElement assemblyElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
100 Assembly.Load(assemblyElement.AssemblyName);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
101 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
102
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
103 internal void Visit(IncludeElement includeElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
104 Include(includeElement.Href);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
105 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
106
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
107 public void Include(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
108 var includeContext = new ContainerBuilder(m_container, m_schema);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
109 includeContext.LoadConfig(file);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
110 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
111
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
112 public void LoadConfig(string file) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
113 var config = m_schema.LoadFile(file);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
114 Visit(config);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
115 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
116
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
117 internal void Visit(ContainerElement containerElement) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
118 foreach (var item in containerElement.Items)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
119 item.Visit(this);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
120 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
121
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
122
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
123
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
124 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
125 }