comparison Implab.ServiceHost/Unity/ContainerBuilder.cs @ 274:22629bf26121 v3

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