268
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Text.RegularExpressions;
|
|
4 using Implab.Diagnostics;
|
|
5
|
|
6 namespace Implab.ServiceHost.Unity {
|
|
7 using System.Linq;
|
269
|
8 using System.Reflection;
|
268
|
9 using System.Text;
|
269
|
10 using global::Unity;
|
272
|
11 using global::Unity.Registration;
|
270
|
12 using Implab.Xml;
|
268
|
13 using static Trace<ConfigurationContext>;
|
|
14
|
|
15 public class ConfigurationContext {
|
|
16
|
270
|
17 readonly TypeResolver m_resolver;
|
|
18
|
272
|
19 readonly UnityContainer m_container;
|
268
|
20
|
269
|
21 public ConfigurationContext(UnityContainer container) {
|
|
22 m_container = container ?? new UnityContainer();
|
270
|
23 m_resolver = new TypeResolver();
|
268
|
24 }
|
|
25
|
|
26
|
270
|
27 public Type Resolve(string typeReference) {
|
|
28 return m_resolver.Resolve(TypeReference.Parse(typeReference));
|
268
|
29 }
|
|
30
|
272
|
31 internal void Visit(RegisterElement descriptor) {
|
|
32 var registrationContext = new RegistrationContext(m_resolver, descriptor.ProvidesType, descriptor.ImplementationType);
|
|
33
|
|
34 if (descriptor.Injectors != null) {
|
|
35 foreach (var injector in descriptor.Injectors) {
|
|
36 injector.Visit(registrationContext);
|
|
37 }
|
|
38 }
|
|
39
|
|
40 m_container.RegisterType(
|
|
41 registrationContext.RegistrationType,
|
|
42 registrationContext.ImplementationType,
|
|
43 descriptor.Name,
|
|
44 descriptor.Lifetime?.GetLifetimeManager(this),
|
|
45 registrationContext.Injections
|
|
46 );
|
|
47
|
269
|
48 }
|
|
49
|
270
|
50 internal void Visit(NamespaceElement namespaceElement) {
|
|
51 m_resolver.AddNamespace(namespaceElement.Name);
|
|
52 }
|
|
53
|
|
54 internal void Visit(AssemblyElement assemblyElement) {
|
|
55 Assembly.Load(assemblyElement.AssemblyName);
|
|
56 }
|
|
57
|
|
58 internal void Visit(IncludeElement includeElement) {
|
|
59 Include(includeElement.Href);
|
|
60 }
|
|
61
|
269
|
62 public void Include(string file) {
|
270
|
63 var includeContext = new ConfigurationContext(m_container);
|
|
64 includeContext.LoadConfig(file);
|
|
65 }
|
268
|
66
|
270
|
67 public void LoadConfig(string file) {
|
|
68 var config = SerializationHelpers.DeserializeFromFile<ContainerElement>(file);
|
|
69 Visit(config);
|
|
70 }
|
|
71
|
|
72 public void Visit(ContainerElement containerElement) {
|
|
73 foreach (var item in containerElement.Items)
|
|
74 item.Visit(this);
|
268
|
75 }
|
|
76
|
|
77 }
|
|
78 } |