comparison Implab.ServiceHost/Unity/ContainerBuilder.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents 6691aff01de1
children f07be402ab02
comparison
equal deleted inserted replaced
278:6691aff01de1 279:8714471e8d78
5 namespace Implab.ServiceHost.Unity { 5 namespace Implab.ServiceHost.Unity {
6 public class ContainerBuilder { 6 public class ContainerBuilder {
7 7
8 readonly TypeResolver m_resolver; 8 readonly TypeResolver m_resolver;
9 9
10 readonly UnityContainer m_container; 10 readonly IUnityContainer m_container;
11 11
12 readonly ContainerConfigurationSchema m_schema; 12 readonly ContainerConfigurationSchema m_schema;
13 13
14 public UnityContainer Container { 14 public IUnityContainer Container {
15 get { 15 get {
16 return m_container; 16 return m_container;
17 } 17 }
18 } 18 }
19 19
20 public ContainerBuilder() : this(null, null) { 20 public ContainerBuilder() : this(null, null) {
21 } 21 }
22 22
23 public ContainerBuilder(UnityContainer container, ContainerConfigurationSchema schema) { 23 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
24 m_container = container ?? new UnityContainer(); 24 m_container = container ?? new UnityContainer();
25 m_resolver = new TypeResolver(); 25 m_resolver = new TypeResolver();
26 m_schema = schema ?? ContainerConfigurationSchema.Default; 26 m_schema = schema ?? ContainerConfigurationSchema.Default;
27 } 27 }
28 28
29 public Type ResolveType(string typeReference) { 29 public Type ResolveType(string typeReference) {
30 return m_resolver.Resolve(typeReference, true); 30 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
31 }
32
33
34 internal void Visit(TypeAbstractRegistration typeRegistration) {
35 var registrationType = typeRegistration.GetRegistrationType(ResolveType);
36 var implementationType = typeRegistration.GetImplementationType(ResolveType) ?? registrationType;
37
38 var registrationContext = new TypeRegistrationBuilder(
39 m_resolver,
40 registrationType,
41 implementationType
42 );
43
44 typeRegistration.Visit(registrationContext);
45
46 m_container.RegisterType(
47 registrationContext.RegistrationType,
48 registrationContext.ImplementationType,
49 typeRegistration.Name,
50 registrationContext.Lifetime,
51 registrationContext.Injections
52 );
53 }
54
55 internal void Visit(InstanceAbstractRegistration instanceRegistration) {
56 var registrationType = instanceRegistration.GetRegistrationType(ResolveType);
57
58 var builder = new InstanceRegistrationBuilder(m_resolver, registrationType);
59
60 instanceRegistration.Visit(builder);
61
62 m_container.RegisterInstance(
63 builder.ValueBuilder.ValueType,
64 instanceRegistration.Name,
65 builder.ValueBuilder.Value,
66 builder.Lifetime
67 );
68 }
69
70 internal void Visit(FactoryAbstractRegistratrion factoryRgistration) {
71 var registrationType = factoryRgistration.GetRegistrationType(ResolveType);
72
73 var builder = new FactoryRegistrationBuilder(registrationType);
74
75 factoryRgistration.Visit(builder);
76
77 m_container.RegisterType(
78 builder.RegistrationType,
79 factoryRgistration.Name,
80 builder.Lifetime,
81 builder.Factory
82 );
83 } 31 }
84 32
85 public void Visit(ITypeRegistration registration) { 33 public void Visit(ITypeRegistration registration) {
86 Safe.ArgumentNotNull(registration, nameof(registration)); 34 Safe.ArgumentNotNull(registration, nameof(registration));
87 35
134 throw new Exception($"A type must be specified for the registration {registration.Name}"); 82 throw new Exception($"A type must be specified for the registration {registration.Name}");
135 83
136 m_container.RegisterInstance( 84 m_container.RegisterInstance(
137 builder.RegistrationType ?? builder.ValueBuilder.ValueType, 85 builder.RegistrationType ?? builder.ValueBuilder.ValueType,
138 registration.Name, 86 registration.Name,
139 builder.ValueBuilder.Injection, 87 builder.ValueBuilder.Value,
140 builder.Lifetime 88 builder.Lifetime
141 ); 89 );
142 }
143
144 public void Visit(IFactoryRegistration registration) {
145 Safe.ArgumentNotNull(registration, nameof(registration));
146
147 var registrationType = registration.GetRegistrationType(this);
148
149 var builder = new FactoryRegistrationBuilder(registrationType);
150
151 if (registration.MemberInjections != null) {
152 foreach(var member in registration.MemberInjections)
153 member?.Visit(builder);
154 }
155
156 } 90 }
157 91
158 public void AddNamespace(string ns) { 92 public void AddNamespace(string ns) {
159 m_resolver.AddNamespace(ns); 93 m_resolver.AddNamespace(ns);
160 } 94 }