diff --git a/Implab.Playground/Program.cs b/Implab.Playground/Program.cs --- a/Implab.Playground/Program.cs +++ b/Implab.Playground/Program.cs @@ -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 @@ namespace Implab.Playground { } - public class Container { + public interface IContainer { + T Instance { get; set; } + } + + public class Container : IContainer { public Container() { } @@ -49,8 +55,30 @@ namespace Implab.Playground { ctx.Visit(conf); - Console.WriteLine($"Registrations: {conf.Items.Count}"); + DisplayContainerRegistrations(container); + + var instace1 = container.Resolve>(); + var instace2 = container.Resolve>(); + + } + 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); + } } diff --git a/Implab.Playground/data/sample.xml b/Implab.Playground/data/sample.xml --- a/Implab.Playground/data/sample.xml +++ b/Implab.Playground/data/sample.xml @@ -1,7 +1,8 @@ + - + @@ -22,17 +23,18 @@ - + - - - Hello! - + + + + Hello! + \ No newline at end of file diff --git a/Implab.ServiceHost/Unity/AbstractInjectionElement.cs b/Implab.ServiceHost/Unity/AbstractInjectionElement.cs --- a/Implab.ServiceHost/Unity/AbstractInjectionElement.cs +++ b/Implab.ServiceHost/Unity/AbstractInjectionElement.cs @@ -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 diff --git a/Implab.ServiceHost/Unity/AbstractRegistration.cs b/Implab.ServiceHost/Unity/AbstractRegistration.cs --- a/Implab.ServiceHost/Unity/AbstractRegistration.cs +++ b/Implab.ServiceHost/Unity/AbstractRegistration.cs @@ -27,8 +27,6 @@ namespace Implab.ServiceHost.Unity [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 diff --git a/Implab.ServiceHost/Unity/ConfigurationContext.cs b/Implab.ServiceHost/Unity/ConfigurationContext.cs --- a/Implab.ServiceHost/Unity/ConfigurationContext.cs +++ b/Implab.ServiceHost/Unity/ConfigurationContext.cs @@ -8,6 +8,7 @@ namespace Implab.ServiceHost.Unity { using System.Reflection; using System.Text; using global::Unity; + using global::Unity.Registration; using Implab.Xml; using static Trace; @@ -15,10 +16,8 @@ namespace Implab.ServiceHost.Unity { 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 @@ namespace Implab.ServiceHost.Unity { 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) { diff --git a/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs b/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs --- a/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs +++ b/Implab.ServiceHost/Unity/ConstructorInjectionElement.cs @@ -8,5 +8,9 @@ namespace Implab.ServiceHost.Unity { [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 diff --git a/Implab.ServiceHost/Unity/DefaultParameterElement.cs b/Implab.ServiceHost/Unity/DefaultParameterElement.cs --- a/Implab.ServiceHost/Unity/DefaultParameterElement.cs +++ b/Implab.ServiceHost/Unity/DefaultParameterElement.cs @@ -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 diff --git a/Implab.ServiceHost/Unity/DependencyParameterElement.cs b/Implab.ServiceHost/Unity/DependencyParameterElement.cs --- a/Implab.ServiceHost/Unity/DependencyParameterElement.cs +++ b/Implab.ServiceHost/Unity/DependencyParameterElement.cs @@ -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 diff --git a/Implab.ServiceHost/Unity/InjectionParameterElement.cs b/Implab.ServiceHost/Unity/InjectionParameterElement.cs --- a/Implab.ServiceHost/Unity/InjectionParameterElement.cs +++ b/Implab.ServiceHost/Unity/InjectionParameterElement.cs @@ -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 diff --git a/Implab.ServiceHost/Unity/MethodInjectionElement.cs b/Implab.ServiceHost/Unity/MethodInjectionElement.cs --- a/Implab.ServiceHost/Unity/MethodInjectionElement.cs +++ b/Implab.ServiceHost/Unity/MethodInjectionElement.cs @@ -12,5 +12,8 @@ namespace Implab.ServiceHost.Unity { [XmlElement("default", typeof(DefaultParameterElement))] public InjectionParameterElement[] Parameters { get; set; } + internal override void Visit(RegistrationContext context) { + context.Visit(this); + } } } \ No newline at end of file diff --git a/Implab.ServiceHost/Unity/PropertyInjectionElement.cs b/Implab.ServiceHost/Unity/PropertyInjectionElement.cs --- a/Implab.ServiceHost/Unity/PropertyInjectionElement.cs +++ b/Implab.ServiceHost/Unity/PropertyInjectionElement.cs @@ -3,10 +3,17 @@ using System.Xml.Serialization; 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 diff --git a/Implab.ServiceHost/Unity/RegisterElement.cs b/Implab.ServiceHost/Unity/RegisterElement.cs --- a/Implab.ServiceHost/Unity/RegisterElement.cs +++ b/Implab.ServiceHost/Unity/RegisterElement.cs @@ -12,13 +12,17 @@ namespace Implab.ServiceHost.Unity { /// An optional type which is registered as a service in the container, must be assignable to . /// [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 diff --git a/Implab.ServiceHost/Unity/RegistrationContext.cs b/Implab.ServiceHost/Unity/RegistrationContext.cs new file mode 100644 --- /dev/null +++ b/Implab.ServiceHost/Unity/RegistrationContext.cs @@ -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 m_injections = new List(); + + 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 diff --git a/Implab.ServiceHost/Unity/SerializedParameterElement.cs b/Implab.ServiceHost/Unity/SerializedParameterElement.cs --- a/Implab.ServiceHost/Unity/SerializedParameterElement.cs +++ b/Implab.ServiceHost/Unity/SerializedParameterElement.cs @@ -8,5 +8,9 @@ namespace Implab.ServiceHost.Unity [XmlAnyElement] public XmlElement[] Content { get; set; } + + internal override object Resolve(RegistrationContext context) { + return context.Resolve(this); + } } } \ No newline at end of file diff --git a/Implab.ServiceHost/Unity/TypeResolver.cs b/Implab.ServiceHost/Unity/TypeResolver.cs --- a/Implab.ServiceHost/Unity/TypeResolver.cs +++ b/Implab.ServiceHost/Unity/TypeResolver.cs @@ -24,6 +24,7 @@ namespace Implab.ServiceHost.Unity } public TypeResolver(TypeResolver parent) { + m_parent = parent; m_insertAt = new LinkedListNode(string.Empty); m_namespases.AddFirst(m_insertAt); } @@ -58,6 +59,10 @@ namespace Implab.ServiceHost.Unity 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)) ), diff --git a/Implab.ServiceHost/Unity/ValueParameterElement.cs b/Implab.ServiceHost/Unity/ValueParameterElement.cs --- a/Implab.ServiceHost/Unity/ValueParameterElement.cs +++ b/Implab.ServiceHost/Unity/ValueParameterElement.cs @@ -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 diff --git a/Implab/Safe.cs b/Implab/Safe.cs --- a/Implab/Safe.cs +++ b/Implab/Safe.cs @@ -55,6 +55,13 @@ namespace Implab 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)