diff Implab.ServiceHost/Unity/ConfigurationContext.cs @ 270:ade80d94dfb5 v3

Working on Unity container xml configuration
author cin
date Wed, 25 Apr 2018 04:44:40 +0300
parents ff581cff7003
children 9d1cca834b05
line wrap: on
line diff
--- 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<ConfigurationContext>;
 
     public class ConfigurationContext {
-        Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
-        readonly LinkedList<string> m_namespases = new LinkedList<string>();
 
-        LinkedListNode<string> m_insertAt;
+        readonly TypeResolver m_resolver;
+
+        
 
         readonly UnityContainer m_container;
-
+        
         public ConfigurationContext(UnityContainer container) {
             m_container = container ?? new UnityContainer();
-            m_insertAt = new LinkedListNode<string>(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<ContainerElement>(file);
+            Visit(config);
+        }
+
+        public void Visit(ContainerElement containerElement) {
+            foreach (var item in containerElement.Items)
+                item.Visit(this);
         }
 
     }