annotate Implab.ServiceHost/Unity/ConfigurationContext.cs @ 271:d4d437ec4483 v3

Working on Unity xml configuration
author cin
date Wed, 25 Apr 2018 19:23:35 +0300
parents ade80d94dfb5
children 9d1cca834b05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
1 using System;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
2 using System.Collections.Generic;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
3 using System.Text.RegularExpressions;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
4 using Implab.Diagnostics;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
5
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
6 namespace Implab.ServiceHost.Unity {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
7 using System.Linq;
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
8 using System.Reflection;
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
9 using System.Text;
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
10 using global::Unity;
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
11 using Implab.Xml;
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
12 using static Trace<ConfigurationContext>;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
13
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
14 public class ConfigurationContext {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
15
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
16 readonly TypeResolver m_resolver;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
17
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
18
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
19
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
20 readonly UnityContainer m_container;
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
21
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
22 public ConfigurationContext(UnityContainer container) {
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
23 m_container = container ?? new UnityContainer();
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
24 m_resolver = new TypeResolver();
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
25 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
26
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
27
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
28 public Type Resolve(string typeReference) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
29 return m_resolver.Resolve(TypeReference.Parse(typeReference));
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
30 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
31
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
32 internal void Visit(AbstractRegistration descriptor) {
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
33
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
34 }
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
35
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
36 internal void Visit(NamespaceElement namespaceElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
37 m_resolver.AddNamespace(namespaceElement.Name);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
38 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
39
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
40 internal void Visit(AssemblyElement assemblyElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
41 Assembly.Load(assemblyElement.AssemblyName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
42 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
43
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
44 internal void Visit(IncludeElement includeElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
45 Include(includeElement.Href);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
46 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
47
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
48 public void Include(string file) {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
49 var includeContext = new ConfigurationContext(m_container);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
50 includeContext.LoadConfig(file);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
51 }
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
52
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
53 public void LoadConfig(string file) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
54 var config = SerializationHelpers.DeserializeFromFile<ContainerElement>(file);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
55 Visit(config);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
56 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
57
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
58 public void Visit(ContainerElement containerElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
59 foreach (var item in containerElement.Items)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
60 item.Visit(this);
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
61 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
62
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
63 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
64 }