# HG changeset patch # User cin # Date 1524620680 -10800 # Node ID ade80d94dfb5f48e774c6dabb16645cc617d928a # Parent ff581cff700324d0f69372982cb6d171ea0898ed Working on Unity container xml configuration diff -r ff581cff7003 -r ade80d94dfb5 Implab.Playground/Program.cs --- a/Implab.Playground/Program.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.Playground/Program.cs Wed Apr 25 04:44:40 2018 +0300 @@ -14,6 +14,8 @@ } + public string Name { get; set; } + public int IntValue { get; set; } public string StringValue { get; set; } @@ -30,6 +32,10 @@ } public T Instance { get; set; } + + public void SetInstance(T value) { + Instance = value; + } } public class Program { @@ -39,7 +45,7 @@ var conf = SerializationHelpers.DeserializeFromFile("data/sample.xml"); - Console.WriteLine($"Registrations: {conf.Registrations.Count}"); + Console.WriteLine($"Registrations: {conf.Items.Count}"); } diff -r ff581cff7003 -r ade80d94dfb5 Implab.Playground/data/sample.xml --- a/Implab.Playground/data/sample.xml Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.Playground/data/sample.xml Wed Apr 25 04:44:40 2018 +0300 @@ -1,10 +1,38 @@ + + + + FOO! + + + GOOD + + + 2 + + + + + + + + + + + + + + + Hello! + + + \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/AbstractInjectionElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/AbstractInjectionElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,7 @@ +namespace Implab.ServiceHost.Unity +{ + public abstract class AbstractInjectionElement + { + + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/AbstractInjectorElement.cs --- a/Implab.ServiceHost/Unity/AbstractInjectorElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -namespace Implab.ServiceHost.Unity -{ - public abstract class AbstractInjectorElement - { - - } -} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/AbstractRegistration.cs --- a/Implab.ServiceHost/Unity/AbstractRegistration.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/AbstractRegistration.cs Wed Apr 25 04:44:40 2018 +0300 @@ -16,7 +16,7 @@ } public void Visit(ConfigurationContext context) { - context.Visist(this); + context.Visit(this); } } } \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/AssemblyElement.cs --- a/Implab.ServiceHost/Unity/AssemblyElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/AssemblyElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -8,7 +8,7 @@ public string AssemblyName { get; set; } public void Visit(ConfigurationContext context) { - throw new System.NotImplementedException(); + context.Visit(this); } } } \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/ConfigurationContext.cs --- a/Implab.ServiceHost/Unity/ConfigurationContext.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/ConfigurationContext.cs Wed Apr 25 04:44:40 2018 +0300 @@ -8,93 +8,56 @@ using System.Reflection; using System.Text; using global::Unity; + using Implab.Xml; using static Trace; public class ConfigurationContext { - Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled); - readonly LinkedList m_namespases = new LinkedList(); - LinkedListNode m_insertAt; + readonly TypeResolver m_resolver; + + readonly UnityContainer m_container; - + public ConfigurationContext(UnityContainer container) { m_container = container ?? new UnityContainer(); - m_insertAt = new LinkedListNode(string.Empty); - m_namespases.AddFirst(m_insertAt); - } - - public void AddNamespace(string ns) { - Safe.ArgumentMatch(ns, nameof(ns), _nsRx); - if (m_insertAt != null) - m_namespases.AddAfter(m_insertAt, ns); - else - m_namespases.AddFirst(ns); + m_resolver = new TypeResolver(); } - public Type Resolve(TypeReference reference) { - Safe.ArgumentNotNull(reference, nameof(reference)); - var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null; - var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0; - - foreach (var ns in m_namespases) { - var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray); - - var resolved = ProbeType(typeName); - if (resolved != null) { - Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.AssemblyQualifiedName); - return resolved; - } else { - Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName); - } - } - - throw new Exception($"Failed to resolve: {reference}"); + public Type Resolve(string typeReference) { + return m_resolver.Resolve(TypeReference.Parse(typeReference)); } - Type ProbeType(string typeName) { - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - - foreach(var assembly in assemblies) { - var type = assembly.GetType(typeName); - if (type != null) - return type; - } - return null; - } - - string FormatName(string[] parts, int argc, Type[] args, bool isArray) { - var builder = new StringBuilder(); - - builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x)))); - if (argc > 0) { - builder.Append('`'); - builder.Append(argc); - } - - if (args!= null && args.Length > 0) { - builder.Append('['); - builder.Append(string.Join(",", args.Select(x => $"[{x.AssemblyQualifiedName}]"))); - builder.Append(']'); - } - - if(isArray) - builder.Append("[]"); - - return builder.ToString(); - } - - public Type Resolve(string typeReference) { - return Resolve(TypeReference.Parse(typeReference)); - } - - public void Visist(AbstractRegistration descriptor) { + internal void Visit(AbstractRegistration descriptor) { } + 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 ConfigurationContext(m_container); + includeContext.LoadConfig(file); + } + public void LoadConfig(string file) { + var config = SerializationHelpers.DeserializeFromFile(file); + Visit(config); + } + + public void Visit(ContainerElement containerElement) { + foreach (var item in containerElement.Items) + item.Visit(this); } } diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/ConstructorInjectionElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,12 @@ +using System.Xml.Serialization; + +namespace Implab.ServiceHost.Unity { + public class ConstructorInjectionElement : AbstractInjectionElement { + + [XmlElement("dependency", typeof(DependencyParameterElement))] + [XmlElement("value", typeof(ValueParameterElement))] + [XmlElement("serialized", typeof(SerializedParameterElement))] + [XmlElement("default", typeof(DefaultParameterElement))] + public InjectionParameterElement[] Parameters { get; set; } + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/ConstructorInjectorElement.cs --- a/Implab.ServiceHost/Unity/ConstructorInjectorElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -namespace Implab.ServiceHost.Unity { - public class ConstructorInjectorElement : AbstractInjectorElement { - - } -} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/ContainerElement.cs --- a/Implab.ServiceHost/Unity/ContainerElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/ContainerElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -8,7 +8,7 @@ [XmlRoot("container", Namespace = Schema.ContainerConfigurationNamespace)] public class ContainerElement : IXmlSerializable { - public List Registrations {get; set; } = new List(); + public List Items {get; set; } = new List(); public XmlSchema GetSchema() { return null; @@ -17,7 +17,7 @@ public void ReadXml(XmlReader reader) { while(reader.Read() && reader.NodeType != XmlNodeType.EndElement) { var registration = ConfigurationSchema.Default.Deserialize(reader); - Registrations.Add(registration); + Items.Add(registration); } } diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/DefaultParameterElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/DefaultParameterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,7 @@ +namespace Implab.ServiceHost.Unity +{ + public class DefaultParameterElement : InjectionParameterElement + { + + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/DependencyParameterElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/DependencyParameterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,7 @@ +namespace Implab.ServiceHost.Unity +{ + public class DependencyParameterElement : InjectionParameterElement + { + + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/IncludeElement.cs --- a/Implab.ServiceHost/Unity/IncludeElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/IncludeElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -7,7 +7,7 @@ public string Href { get; set; } public void Visit(ConfigurationContext context) { - context.Include(Href); + context.Visit(this); } } } \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/InjectionParameterElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/InjectionParameterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,9 @@ +using System.Xml.Serialization; + +namespace Implab.ServiceHost.Unity { + public class InjectionParameterElement { + + [XmlAttribute("type")] + public string TypeName { get; set; } + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/MethodInjectionElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/MethodInjectionElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,16 @@ +using System.Xml.Serialization; + +namespace Implab.ServiceHost.Unity { + public class MethodInjectionElement : AbstractInjectionElement { + + [XmlAttribute("name")] + public string Name { get; set; } + + [XmlElement("dependency", typeof(DependencyParameterElement))] + [XmlElement("value", typeof(ValueParameterElement))] + [XmlElement("serialized", typeof(SerializedParameterElement))] + [XmlElement("default", typeof(DefaultParameterElement))] + public InjectionParameterElement[] Parameters { get; set; } + + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/MethodInjectorElement.cs --- a/Implab.ServiceHost/Unity/MethodInjectorElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -using System.Xml.Serialization; - -namespace Implab.ServiceHost.Unity { - public class MethodInjectorElement : AbstractInjectorElement { - - [XmlAttribute("name")] - public string Name { get; set; } - - [XmlElement("params")] - public MethodInjectorParameter[] Parameters { get; set; } - - } -} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/MethodInjectorParameter.cs --- a/Implab.ServiceHost/Unity/MethodInjectorParameter.cs Tue Apr 24 01:46:02 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -namespace Implab.ServiceHost.Unity -{ - public class MethodInjectorParameter - { - - } -} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/NamespaceElement.cs --- a/Implab.ServiceHost/Unity/NamespaceElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/NamespaceElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -9,7 +9,7 @@ public string Name { get; set; } public void Visit(ConfigurationContext context) { - throw new System.NotImplementedException(); + context.Visit(this); } } } \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/PropertyInjectionElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/PropertyInjectionElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,12 @@ +using System.Xml.Serialization; + +namespace Implab.ServiceHost.Unity { + public class PropertyInjectionElement : AbstractInjectionElement { + + [XmlElement("dependency", typeof(DependencyParameterElement))] + [XmlElement("value", typeof(ValueParameterElement))] + [XmlElement("serialized", typeof(SerializedParameterElement))] + [XmlElement("default", typeof(DefaultParameterElement))] + public InjectionParameterElement Value { get; set; } + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/PropertyInjectorElement.cs --- a/Implab.ServiceHost/Unity/PropertyInjectorElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -namespace Implab.ServiceHost.Unity { - public class PropertyInjectorElement : AbstractInjectorElement { - - - } -} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/RegisterElement.cs --- a/Implab.ServiceHost/Unity/RegisterElement.cs Tue Apr 24 01:46:02 2018 +0300 +++ b/Implab.ServiceHost/Unity/RegisterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -27,10 +27,10 @@ [XmlElement("hierarchy", typeof(HierarchicalLifetimeElement))] public LifetimeElement Lifetime {get; set;} - [XmlElement("constructor", typeof(ConstructorInjectorElement))] - [XmlElement("property", typeof(PropertyInjectorElement))] - [XmlElement("method", typeof(MethodInjectorElement))] - public AbstractInjectorElement[] Injectors { get; set; } + [XmlElement("constructor", typeof(ConstructorInjectionElement))] + [XmlElement("property", typeof(PropertyInjectionElement))] + [XmlElement("method", typeof(MethodInjectionElement))] + public AbstractInjectionElement[] Injectors { get; set; } } } \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/SerializedParameterElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/SerializedParameterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,7 @@ +namespace Implab.ServiceHost.Unity +{ + public class SerializedParameterElement : InjectionParameterElement + { + + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/TypeResolver.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/TypeResolver.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using Implab.Diagnostics; + +namespace Implab.ServiceHost.Unity +{ + using static Trace; + public class TypeResolver + { + readonly Dictionary m_cache = new Dictionary(); + + Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled); + readonly LinkedList m_namespases = new LinkedList(); + + LinkedListNode m_insertAt; + + readonly TypeResolver m_parent; + + public TypeResolver() : this(null) { + + } + + public TypeResolver(TypeResolver parent) { + m_insertAt = new LinkedListNode(string.Empty); + m_namespases.AddFirst(m_insertAt); + } + + public void AddNamespace(string ns) { + Safe.ArgumentMatch(ns, nameof(ns), _nsRx); + if (m_insertAt != null) + m_namespases.AddAfter(m_insertAt, ns); + else + m_namespases.AddFirst(ns); + } + + public void AddMapping(string typeName, Type type) { + Safe.ArgumentNotEmpty(typeName, nameof(typeName)); + Safe.ArgumentNotNull(type, nameof(type)); + + m_cache[typeName] = type; + } + + public Type Resolve(TypeReference reference) { + var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null; + var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0; + + Type resolved; + if(!m_cache.TryGetValue(reference.ToString(), out resolved)) { + resolved = ResolveInternal(reference, args, argc); + if (resolved == null) + throw new Exception($"Failed to resolve {reference}"); + m_cache[reference.ToString()] = resolved; + } + + return resolved; + } + + Type ResolveInternal(TypeReference reference, Type[] args, int argc) { + var resolved = ProbeInNamespaces( + String.Join(".", new [] { reference.Namespace, reference.TypeName }.Where(x => !string.IsNullOrEmpty(x)) ), + args, + argc, + reference.IsArray, + reference.ToString() + ); + + if (resolved == null && m_parent != null) + resolved = m_parent.Resolve(reference); + + return resolved; + } + + public Type ProbeInNamespaces(string localName, Type[] args, int argc, bool isArray, string referenceName) { + foreach (var ns in m_namespases) { + var typeName = FormatName(new [] { ns, localName}, argc, args, isArray); + + var resolved = Probe(typeName); + if (resolved != null) { + Log("Probe succeed {0} in '{1}': {2} -> {3}", referenceName, ns, typeName, resolved.AssemblyQualifiedName); + return resolved; + } else { + Log("Probe failed {0} in '{1}': {2}", referenceName, ns, typeName); + } + } + + return null; + } + + Type Probe(string typeName) { + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + + foreach(var assembly in assemblies) { + var type = assembly.GetType(typeName); + if (type != null) + return type; + } + return null; + } + + string FormatName(string[] parts, int argc, Type[] args, bool isArray) { + var builder = new StringBuilder(); + + builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x)))); + if (argc > 0) { + builder.Append('`'); + builder.Append(argc); + } + + if (args!= null && args.Length > 0) { + builder.Append('['); + builder.Append(string.Join(",", args.Select(x => $"[{x.AssemblyQualifiedName}]"))); + builder.Append(']'); + } + + if(isArray) + builder.Append("[]"); + + return builder.ToString(); + } + } +} \ No newline at end of file diff -r ff581cff7003 -r ade80d94dfb5 Implab.ServiceHost/Unity/ValueParameterElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/ValueParameterElement.cs Wed Apr 25 04:44:40 2018 +0300 @@ -0,0 +1,7 @@ +namespace Implab.ServiceHost.Unity +{ + public class ValueParameterElement : InjectionParameterElement + { + + } +} \ No newline at end of file