ConfigurationContext.cs
77 lines
| 2.4 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r268 | using System; | |
using System.Collections.Generic; | |||
using System.Text.RegularExpressions; | |||
using Implab.Diagnostics; | |||
namespace Implab.ServiceHost.Unity { | |||
using System.Linq; | |||
cin
|
r269 | using System.Reflection; | |
cin
|
r268 | using System.Text; | |
cin
|
r269 | using global::Unity; | |
cin
|
r272 | using global::Unity.Registration; | |
cin
|
r270 | using Implab.Xml; | |
cin
|
r268 | using static Trace<ConfigurationContext>; | |
public class ConfigurationContext { | |||
cin
|
r270 | readonly TypeResolver m_resolver; | |
cin
|
r272 | readonly UnityContainer m_container; | |
cin
|
r268 | ||
cin
|
r269 | public ConfigurationContext(UnityContainer container) { | |
m_container = container ?? new UnityContainer(); | |||
cin
|
r270 | m_resolver = new TypeResolver(); | |
cin
|
r268 | } | |
cin
|
r270 | public Type Resolve(string typeReference) { | |
return m_resolver.Resolve(TypeReference.Parse(typeReference)); | |||
cin
|
r268 | } | |
cin
|
r272 | internal void Visit(RegisterElement descriptor) { | |
var registrationContext = new RegistrationContext(m_resolver, descriptor.ProvidesType, descriptor.ImplementationType); | |||
if (descriptor.Injectors != null) { | |||
foreach (var injector in descriptor.Injectors) { | |||
injector.Visit(registrationContext); | |||
} | |||
} | |||
m_container.RegisterType( | |||
registrationContext.RegistrationType, | |||
registrationContext.ImplementationType, | |||
descriptor.Name, | |||
descriptor.Lifetime?.GetLifetimeManager(this), | |||
registrationContext.Injections | |||
); | |||
cin
|
r269 | } | |
cin
|
r270 | internal void Visit(NamespaceElement namespaceElement) { | |
m_resolver.AddNamespace(namespaceElement.Name); | |||
} | |||
internal void Visit(AssemblyElement assemblyElement) { | |||
Assembly.Load(assemblyElement.AssemblyName); | |||
} | |||
internal void Visit(IncludeElement includeElement) { | |||
Include(includeElement.Href); | |||
} | |||
cin
|
r269 | public void Include(string file) { | |
cin
|
r270 | var includeContext = new ConfigurationContext(m_container); | |
includeContext.LoadConfig(file); | |||
} | |||
cin
|
r268 | ||
cin
|
r270 | public void LoadConfig(string file) { | |
var config = SerializationHelpers.DeserializeFromFile<ContainerElement>(file); | |||
Visit(config); | |||
} | |||
public void Visit(ContainerElement containerElement) { | |||
foreach (var item in containerElement.Items) | |||
item.Visit(this); | |||
cin
|
r268 | } | |
} | |||
} |