ContainerContext.cs
110 lines
| 3.3 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r273 | using System; | |
using System.Collections.Generic; | |||
using System.Text.RegularExpressions; | |||
using Implab.Diagnostics; | |||
namespace Implab.ServiceHost.Unity { | |||
using System.Linq; | |||
using System.Reflection; | |||
using System.Text; | |||
using global::Unity; | |||
using global::Unity.Registration; | |||
using Implab.Xml; | |||
using static Trace<ContainerContext>; | |||
public class ContainerContext { | |||
readonly TypeResolver m_resolver; | |||
readonly UnityContainer m_container; | |||
readonly ContainerConfigurationSchema m_schema; | |||
public UnityContainer Container { | |||
get { | |||
return m_container; | |||
} | |||
} | |||
public ContainerContext() : this(null, null) { | |||
} | |||
public ContainerContext(UnityContainer container, ContainerConfigurationSchema schema) { | |||
m_container = container ?? new UnityContainer(); | |||
m_resolver = new TypeResolver(); | |||
m_schema = schema ?? ContainerConfigurationSchema.Default; | |||
} | |||
public Type Resolve(string typeReference) { | |||
return m_resolver.Resolve(typeReference); | |||
} | |||
internal void Visit(RegisterElement registerElement) { | |||
var registrationContext = new TypeRegistrationContext( | |||
m_resolver, | |||
registerElement.ResolveRegistrationType(this), | |||
registerElement.ResolveImplementationType(this) | |||
); | |||
if (registerElement.Injectors != null) { | |||
foreach (var injector in registerElement.Injectors) { | |||
injector.Visit(registrationContext); | |||
} | |||
} | |||
m_container.RegisterType( | |||
registrationContext.RegistrationType, | |||
registrationContext.ImplementationType, | |||
registerElement.Name, | |||
registerElement.Lifetime?.GetLifetimeManager(this), | |||
registrationContext.Injections | |||
); | |||
} | |||
internal void Visit(SerializedElement serializedElement) { | |||
var registrationType = serializedElement.ResolveRegistrationType(this); | |||
var instace = serializedElement.GetValue(this); | |||
m_container.RegisterInstance( | |||
registrationType, | |||
serializedElement.Name, | |||
instace, | |||
serializedElement.Lifetime?.GetLifetimeManager(this) | |||
); | |||
} | |||
internal void Visit(ValueElement valueElement) { | |||
} | |||
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); | |||
} | |||
public void Include(string file) { | |||
var includeContext = new ContainerContext(m_container, m_schema); | |||
includeContext.LoadConfig(file); | |||
} | |||
public void LoadConfig(string file) { | |||
var config = m_schema.LoadFile(file); | |||
Visit(config); | |||
} | |||
internal void Visit(ContainerElement containerElement) { | |||
foreach (var item in containerElement.Items) | |||
item.Visit(this); | |||
} | |||
} | |||
} |