diff Implab.ServiceHost/Unity/ContainerBuilder.cs @ 278:6691aff01de1 v3

Implab: added XmlDefaultSeializer (SerializersPool is now obsolete) Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author cin
date Thu, 03 May 2018 09:59:44 +0300
parents 963b17c275be
children 8714471e8d78
line wrap: on
line diff
--- a/Implab.ServiceHost/Unity/ContainerBuilder.cs	Sat Apr 28 18:48:09 2018 +0300
+++ b/Implab.ServiceHost/Unity/ContainerBuilder.cs	Thu May 03 09:59:44 2018 +0300
@@ -1,17 +1,8 @@
 using System;
-using System.Collections.Generic;
-using System.Text.RegularExpressions;
-using Implab.Diagnostics;
+using System.Reflection;
+using Unity;
 
 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<ContainerBuilder>;
-
     public class ContainerBuilder {
 
         readonly TypeResolver m_resolver;
@@ -36,7 +27,7 @@
         }
 
         public Type ResolveType(string typeReference) {
-            return m_resolver.Resolve(typeReference);
+            return m_resolver.Resolve(typeReference, true);
         }
 
 
@@ -91,16 +82,85 @@
             );
         }
 
-        internal void Visit(NamespaceElement namespaceElement) {
-            m_resolver.AddNamespace(namespaceElement.Name);
+        public void Visit(ITypeRegistration registration) {
+            Safe.ArgumentNotNull(registration, nameof(registration));
+
+            var registrationType = registration.GetRegistrationType(this);
+            var implementationType = registration.GetImplementationType(this) ?? registrationType;
+
+            if (registrationType == null)
+                throw new Exception($"A type must be specified for the registration {registration.Name}");
+
+            var builder = new TypeRegistrationBuilder(
+                m_resolver,
+                registrationType,
+                implementationType
+            );
+
+            builder.Lifetime = registration.GetLifetime(this);
+
+            if (registration.MemberInjections != null) {
+                foreach(var member in registration.MemberInjections)
+                    member.Visit(builder);
+            }
+
+            m_container.RegisterType(
+                builder.RegistrationType,
+                builder.ImplementationType,
+                registration.Name,
+                builder.Lifetime,
+                builder.Injections
+            );
         }
 
-        internal void Visit(AssemblyElement assemblyElement) {
-            Assembly.Load(assemblyElement.AssemblyName);
+        public void Visit(IInstanceRegistration registration) {
+            Safe.ArgumentNotNull(registration, nameof(registration));
+
+            var registrationType = registration.GetRegistrationType(this);
+
+            var builder = new InstanceRegistrationBuilder (
+                m_resolver,
+                registrationType
+            );
+
+            builder.Lifetime = registration.GetLifetime(this);
+
+            if (registration.MemberInjections != null) {
+                foreach(var member in registration.MemberInjections)
+                    member.Visit(builder.ValueBuilder);
+            }
+
+            if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
+                throw new Exception($"A type must be specified for the registration {registration.Name}");
+
+            m_container.RegisterInstance(
+                builder.RegistrationType ?? builder.ValueBuilder.ValueType,
+                registration.Name,
+                builder.ValueBuilder.Injection,
+                builder.Lifetime                
+            );
         }
 
-        internal void Visit(IncludeElement includeElement) {
-            Include(includeElement.Href);
+        public void Visit(IFactoryRegistration registration) {
+            Safe.ArgumentNotNull(registration, nameof(registration));
+
+            var registrationType = registration.GetRegistrationType(this);
+
+            var builder = new FactoryRegistrationBuilder(registrationType);
+
+            if (registration.MemberInjections != null) {
+                foreach(var member in registration.MemberInjections)
+                    member?.Visit(builder);
+            }
+
+        }
+
+        public void AddNamespace(string ns) {
+            m_resolver.AddNamespace(ns);
+        }
+
+        public void AddAssembly(string assembly) {
+
         }
 
         public void Include(string file) {
@@ -110,9 +170,8 @@
 
         public void LoadConfig(string file) {
             var config = m_schema.LoadFile(file);
-            
             config.Visit(this);
         }
-        
+
     }
 }
\ No newline at end of file