changeset 272:9d1cca834b05 v3

preview version of Unity xml configuration
author cin
date Thu, 26 Apr 2018 03:14:54 +0300
parents d4d437ec4483
children 79110a16cab7
files Implab.Playground/Program.cs Implab.Playground/data/sample.xml Implab.ServiceHost/Unity/AbstractInjectionElement.cs Implab.ServiceHost/Unity/AbstractRegistration.cs Implab.ServiceHost/Unity/ConfigurationContext.cs Implab.ServiceHost/Unity/ConstructorInjectionElement.cs Implab.ServiceHost/Unity/DefaultParameterElement.cs Implab.ServiceHost/Unity/DependencyParameterElement.cs Implab.ServiceHost/Unity/InjectionParameterElement.cs Implab.ServiceHost/Unity/MethodInjectionElement.cs Implab.ServiceHost/Unity/PropertyInjectionElement.cs Implab.ServiceHost/Unity/RegisterElement.cs Implab.ServiceHost/Unity/RegistrationContext.cs Implab.ServiceHost/Unity/SerializedParameterElement.cs Implab.ServiceHost/Unity/TypeResolver.cs Implab.ServiceHost/Unity/ValueParameterElement.cs Implab/Safe.cs
diffstat 17 files changed, 245 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/Implab.Playground/Program.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.Playground/Program.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,10 +1,12 @@
 using System;
 using System.Diagnostics;
+using System.Linq;
 using Implab.Diagnostics;
 using Implab.ServiceHost.Unity;
 using Implab.Xml;
 using Unity;
 using Unity.Injection;
+using Unity.Registration;
 
 namespace Implab.Playground {
 
@@ -22,7 +24,11 @@
 
     }
 
-    public class Container<T> {
+    public interface IContainer<T> {
+        T Instance { get; set; }
+    }
+
+    public class Container<T> : IContainer<T> {
         public Container() {
 
         }
@@ -49,8 +55,30 @@
 
             ctx.Visit(conf);
 
-            Console.WriteLine($"Registrations: {conf.Items.Count}");
+            DisplayContainerRegistrations(container);
+
+            var instace1 = container.Resolve<IContainer<string>>();
+            var instace2 = container.Resolve<IContainer<Foo>>();
+
+        }
 
+        static void DisplayContainerRegistrations(IUnityContainer theContainer) {
+            string regName, regType, mapTo, lifetime;
+            Console.WriteLine("Container has {0} Registrations:",
+                    theContainer.Registrations.Count());
+            foreach (ContainerRegistration item in theContainer.Registrations) {
+                regType = item.RegisteredType.FullName;
+                mapTo = item.MappedToType.FullName;
+                regName = item.Name ?? "[default]";
+                lifetime = item.LifetimeManager.LifetimeType.Name;
+                if (mapTo != regType) {
+                    mapTo = " -> " + mapTo;
+                } else {
+                    mapTo = string.Empty;
+                }
+                lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
+                Console.WriteLine("+ {0}{1}  '{2}'  {3}", regType, mapTo, regName, lifetime);
+            }
         }
 
 
--- a/Implab.Playground/data/sample.xml	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.Playground/data/sample.xml	Thu Apr 26 03:14:54 2018 +0300
@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <container xmlns="http://implab.org/schemas/servicehost/unity.v1.xsd">
+    <namespace name="System"/>
     <namespace name="Implab.Playground"/>
-
+    
     <!-- foo1 -->
     <register name="foo1" type="Foo">
         <property name="Name">
@@ -22,17 +23,18 @@
     <register type="Foo">
     </register>
 
-    <register type="Container{}">
+    <register provides="IContainer{}" type="Container{}">
         <constructor/>
         <method name="SetInstance">
             <dependency type="T"/>
         </method>
     </register>
 
-    <register type="Container{String}">
-        <property name="Instance">
-            <value>Hello!</value>
-        </property>
+    <register provides="IContainer{String}" type="Container{String}">
+        <constructor/>
+        <method name="SetInstance">
+            <value type="String">Hello!</value>
+        </method>
     </register>
 
 </container>
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/AbstractInjectionElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/AbstractInjectionElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,7 +1,8 @@
+using System;
+
 namespace Implab.ServiceHost.Unity
 {
-    public abstract class AbstractInjectionElement
-    {
-        
+    public abstract class AbstractInjectionElement {
+        internal abstract void Visit(RegistrationContext context);
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/AbstractRegistration.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/AbstractRegistration.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -27,8 +27,6 @@
         [XmlAttribute("provides")]
         public string ProvidesType { get; set; }
 
-        public void Visit(ConfigurationContext context) {
-            context.Visit(this);
-        }
+        public abstract void Visit(ConfigurationContext context);
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/ConfigurationContext.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/ConfigurationContext.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -8,6 +8,7 @@
     using System.Reflection;
     using System.Text;
     using global::Unity;
+    using global::Unity.Registration;
     using Implab.Xml;
     using static Trace<ConfigurationContext>;
 
@@ -15,10 +16,8 @@
 
         readonly TypeResolver m_resolver;
 
-        
+        readonly UnityContainer m_container;
 
-        readonly UnityContainer m_container;
-        
         public ConfigurationContext(UnityContainer container) {
             m_container = container ?? new UnityContainer();
             m_resolver = new TypeResolver();
@@ -29,8 +28,23 @@
             return m_resolver.Resolve(TypeReference.Parse(typeReference));
         }
 
-        internal void Visit(AbstractRegistration descriptor) {
-            
+        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
+            );
+
         }
 
         internal void Visit(NamespaceElement namespaceElement) {
--- a/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -8,5 +8,9 @@
         [XmlElement("serialized", typeof(SerializedParameterElement))]
         [XmlElement("default", typeof(DefaultParameterElement))]
         public InjectionParameterElement[] Parameters { get; set; }
+
+        internal override void Visit(RegistrationContext context) {
+            context.Visit(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/DefaultParameterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/DefaultParameterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,7 +1,8 @@
 namespace Implab.ServiceHost.Unity
 {
-    public class DefaultParameterElement : InjectionParameterElement
-    {
-        
+    public class DefaultParameterElement : InjectionParameterElement {
+        internal override object Resolve(RegistrationContext context) {
+            return context.Resolve(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/DependencyParameterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/DependencyParameterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,7 +1,16 @@
-namespace Implab.ServiceHost.Unity
-{
-    public class DependencyParameterElement : InjectionParameterElement
-    {
-        
+using System.Xml.Serialization;
+
+namespace Implab.ServiceHost.Unity {
+    public class DependencyParameterElement : InjectionParameterElement {
+
+        [XmlAttribute("name")]
+        public string DependencyName { get; set; }
+
+        [XmlAttribute("optional")]
+        public bool Optional { get; set; }
+
+        internal override object Resolve(RegistrationContext context) {
+            return context.Resolve(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/InjectionParameterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/InjectionParameterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,9 +1,11 @@
 using System.Xml.Serialization;
 
 namespace Implab.ServiceHost.Unity {
-    public class InjectionParameterElement {
+    public abstract class InjectionParameterElement {
         
         [XmlAttribute("type")]
         public string TypeName { get; set; }
+
+        internal abstract object Resolve(RegistrationContext context);
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/MethodInjectionElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/MethodInjectionElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -12,5 +12,8 @@
         [XmlElement("default", typeof(DefaultParameterElement))]
         public InjectionParameterElement[] Parameters { get; set; }
 
+        internal override void Visit(RegistrationContext context) {
+            context.Visit(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/PropertyInjectionElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/PropertyInjectionElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -3,10 +3,17 @@
 namespace Implab.ServiceHost.Unity {
     public class PropertyInjectionElement : 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 Value { get; set; }
+
+        internal override void Visit(RegistrationContext context) {
+            context.Visit(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/RegisterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/RegisterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -12,13 +12,17 @@
         /// An optional type which is registered as a service in the container, must be assignable to <see cref="ProvidesType">.
         /// </summary>
         [XmlAttribute("type")]
-        public string ImplementedType { get; set; }
+        public string ImplementationType { get; set; }
 
 
         [XmlElement("constructor", typeof(ConstructorInjectionElement))]
         [XmlElement("property", typeof(PropertyInjectionElement))]
         [XmlElement("method", typeof(MethodInjectionElement))]
         public AbstractInjectionElement[] Injectors { get; set; }
+
+        public override void Visit(ConfigurationContext context) {
+            context.Visit(this);
+        }
     }
     
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab.ServiceHost/Unity/RegistrationContext.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -0,0 +1,119 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Xml.Serialization;
+using Implab.Xml;
+using Unity.Injection;
+using Unity.Registration;
+
+namespace Implab.ServiceHost.Unity {
+    class RegistrationContext {
+        readonly TypeResolver m_resolver;
+
+        List<InjectionMember> m_injections = new List<InjectionMember>();
+
+        Type m_defaultType;
+
+        public Type RegistrationType {
+            get;
+            private set;
+        }
+
+        public Type ImplementationType {
+            get;
+            private set;
+        }
+
+        public RegistrationContext(TypeResolver resolver, string typeSpec, string implSpec) {
+            RegistrationType = resolver.Resolve(string.IsNullOrEmpty(typeSpec) ? implSpec : typeSpec);
+            
+
+            ImplementationType = string.IsNullOrEmpty(implSpec) ? RegistrationType : resolver.Resolve(implSpec);
+
+            if (RegistrationType.IsGenericTypeDefinition) {
+                m_resolver = new TypeResolver(resolver);
+
+                foreach (var p in ImplementationType.GetGenericArguments())
+                    m_resolver.AddMapping(p.Name, p);
+            } else {
+                m_resolver = resolver;
+            }
+
+            
+        }
+
+        public InjectionMember[] Injections {
+            get {
+                return m_injections.ToArray();
+            }
+        }
+
+        public void Visit(ConstructorInjectionElement constructorInjection) {
+            var parameters = constructorInjection.Parameters?.Select(x => x.Resolve(this)).ToArray();
+
+            var injection = parameters != null ? new InjectionConstructor(parameters) : new InjectionConstructor();
+            m_injections.Add(injection);
+        }
+
+        public void Visit(MethodInjectionElement methodInjection) {
+            var parameters = methodInjection.Parameters?.Select(x => x.Resolve(this)).ToArray();
+
+            var injection = parameters != null ? new InjectionMethod(methodInjection.Name, parameters) : new InjectionMethod(methodInjection.Name);
+            m_injections.Add(injection);
+        }
+
+        public void Visit(PropertyInjectionElement propertyInjection) {
+            if (propertyInjection.Value == null)
+                throw new Exception($"A value value must be specified for the property '{propertyInjection.Name}'");
+
+            try {
+                m_defaultType = RegistrationType.GetProperty(propertyInjection.Name)?.PropertyType;
+
+                var parameter = propertyInjection.Value.Resolve(this);
+                var injection = new InjectionProperty(propertyInjection.Name, parameter);
+                m_injections.Add(injection);
+
+            } finally {
+                m_defaultType = null;
+            }
+
+        }
+
+        Type ResolveParameterType(InjectionParameterElement injectionParameter) {
+            if (string.IsNullOrEmpty(injectionParameter.TypeName)) {
+                if (m_defaultType == null)
+                    throw new Exception($"A type must be specified for the parameter {injectionParameter}");
+                return m_defaultType;
+            }
+            return m_resolver.Resolve(injectionParameter.TypeName);
+        }
+
+        public object Resolve(DefaultParameterElement defaultParameter) {
+            var type = ResolveParameterType(defaultParameter);
+
+            return Safe.CreateDefaultValue(type);
+        }
+
+        public object Resolve(ValueParameterElement valueParameter) {
+            var type = ResolveParameterType(valueParameter);
+
+            return TypeDescriptor.GetConverter(type).ConvertFromString(valueParameter.Value);
+        }
+
+        public object Resolve(SerializedParameterElement serializedParameter) {
+            var type = ResolveParameterType(serializedParameter);
+            if (serializedParameter.Content == null || serializedParameter.Content.Length == 0)
+                return Safe.CreateDefaultValue(type);
+
+            var serializer = new XmlSerializer(type);
+            using (var reader = serializedParameter.Content[0].CreateNavigator().ReadSubtree())
+                return serializer.Deserialize(reader);
+        }
+
+        public InjectionParameterValue Resolve(DependencyParameterElement dependencyParameter) {
+            var type = ResolveParameterType(dependencyParameter);
+            return new ResolvedParameter(type, dependencyParameter.DependencyName);
+        }
+    }
+}
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/SerializedParameterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/SerializedParameterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -8,5 +8,9 @@
 
         [XmlAnyElement]
         public XmlElement[] Content { get; set; }
+
+        internal override object Resolve(RegistrationContext context) {
+            return context.Resolve(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab.ServiceHost/Unity/TypeResolver.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/TypeResolver.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -24,6 +24,7 @@
         }
 
         public TypeResolver(TypeResolver parent) {
+            m_parent = parent;
             m_insertAt = new LinkedListNode<string>(string.Empty);
             m_namespases.AddFirst(m_insertAt);
         }
@@ -58,6 +59,10 @@
             return resolved;
         }
 
+        public Type Resolve(string typeSpec) {
+            return Resolve(TypeReference.Parse(typeSpec));
+        }
+
         Type ResolveInternal(TypeReference reference, Type[] args, int argc) {
             var resolved = ProbeInNamespaces(
                 String.Join(".", new [] { reference.Namespace, reference.TypeName }.Where(x => !string.IsNullOrEmpty(x)) ),
--- a/Implab.ServiceHost/Unity/ValueParameterElement.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab.ServiceHost/Unity/ValueParameterElement.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -1,7 +1,14 @@
+using System.Xml.Serialization;
+
 namespace Implab.ServiceHost.Unity
 {
     public class ValueParameterElement : InjectionParameterElement
     {
-        
+        [XmlText]
+        public string Value { get; set; }
+
+        internal override object Resolve(RegistrationContext context) {
+            return context.Resolve(this);
+        }
     }
 }
\ No newline at end of file
--- a/Implab/Safe.cs	Wed Apr 25 19:23:35 2018 +0300
+++ b/Implab/Safe.cs	Thu Apr 26 03:14:54 2018 +0300
@@ -55,6 +55,13 @@
                 throw new ArgumentOutOfRangeException(paramName);
         }
 
+        public static object CreateDefaultValue(Type type) {
+            if (type.IsValueType)
+                return Activator.CreateInstance(type);
+
+            return null;
+        }
+
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static void ArgumentInRange(bool condition, string paramName) {
             if (!condition)