annotate Implab.ServiceHost/Unity/ConfigurationContext.cs @ 272:9d1cca834b05 v3

preview version of Unity xml configuration
author cin
date Thu, 26 Apr 2018 03:14:54 +0300
parents ade80d94dfb5
children
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;
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
11 using global::Unity.Registration;
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
12 using Implab.Xml;
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
13 using static Trace<ConfigurationContext>;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
14
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
15 public class ConfigurationContext {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
16
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
17 readonly TypeResolver m_resolver;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
18
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
19 readonly UnityContainer m_container;
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
20
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
21 public ConfigurationContext(UnityContainer container) {
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
22 m_container = container ?? new UnityContainer();
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
23 m_resolver = new TypeResolver();
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
24 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
25
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
26
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
27 public Type Resolve(string typeReference) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
28 return m_resolver.Resolve(TypeReference.Parse(typeReference));
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
29 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
30
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
31 internal void Visit(RegisterElement descriptor) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
32 var registrationContext = new RegistrationContext(m_resolver, descriptor.ProvidesType, descriptor.ImplementationType);
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
33
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
34 if (descriptor.Injectors != null) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
35 foreach (var injector in descriptor.Injectors) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
36 injector.Visit(registrationContext);
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
37 }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
38 }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
39
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
40 m_container.RegisterType(
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
41 registrationContext.RegistrationType,
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
42 registrationContext.ImplementationType,
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
43 descriptor.Name,
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
44 descriptor.Lifetime?.GetLifetimeManager(this),
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
45 registrationContext.Injections
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
46 );
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
47
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
48 }
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
49
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
50 internal void Visit(NamespaceElement namespaceElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
51 m_resolver.AddNamespace(namespaceElement.Name);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
52 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
53
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
54 internal void Visit(AssemblyElement assemblyElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
55 Assembly.Load(assemblyElement.AssemblyName);
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 internal void Visit(IncludeElement includeElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
59 Include(includeElement.Href);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
60 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
61
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
62 public void Include(string file) {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
63 var includeContext = new ConfigurationContext(m_container);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
64 includeContext.LoadConfig(file);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
65 }
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
66
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
67 public void LoadConfig(string file) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
68 var config = SerializationHelpers.DeserializeFromFile<ContainerElement>(file);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
69 Visit(config);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
70 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
71
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
72 public void Visit(ContainerElement containerElement) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
73 foreach (var item in containerElement.Items)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
74 item.Visit(this);
268
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
75 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
76
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
77 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
78 }