diff Implab.ServiceHost/Unity/FactoryActivator.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents 6691aff01de1
children
line wrap: on
line diff
--- a/Implab.ServiceHost/Unity/FactoryActivator.cs	Thu May 03 09:59:44 2018 +0300
+++ b/Implab.ServiceHost/Unity/FactoryActivator.cs	Fri May 04 18:12:42 2018 +0300
@@ -1,29 +1,63 @@
 using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Implab.Components;
+using Unity;
+using Unity.Injection;
+using Unity.Lifetime;
 
 namespace Implab.ServiceHost.Unity {
-    public class FactoryActivator : FactoryAbstractRegistratrion {
+    public class FactoryActivator : ITypeRegistration {
+
+        class FactoryInjector : ITypeMemberInjection {
+            public InjectionFactory Factory { get; set; }
+            public void Visit(TypeRegistrationBuilder builder) {
+                builder.AddInjectionMember(Factory);
+            }
+        }
+
 
         public Type FactoryType { get; set; }
 
         public string FactoryName { get; set; }
 
-        public new Type RegistrationType { get; set; }
+        public Type RegistrationType { get; set; }
 
-        public override void Visit(FactoryRegistrationBuilder builder) {
-            base.Visit(builder);
+        public LifetimeManager Lifetime { get; set; }
 
-            builder.GetType()
-                .GetMethod(
-                    nameof(FactoryRegistrationBuilder.SetFactoryDependency)
-                    , new[] { typeof(string) }
-                )
-                .MakeGenericMethod(FactoryType, RegistrationType)
-                .Invoke(builder, new[] { FactoryName });
+        public IEnumerable<ITypeMemberInjection> MemberInjections {
+            get {
+                yield return new FactoryInjector {
+                    Factory = (InjectionFactory)GetType()
+                        .GetMethod(nameof(CreateInjectionFactory), BindingFlags.Static | BindingFlags.NonPublic)
+                        .MakeGenericMethod(FactoryType, RegistrationType)
+                        .Invoke(null, new [] { FactoryName })
+                };
+            }
         }
 
-        public override Type GetRegistrationType(Func<string, Type> resolver) {
+        public string Name { get; set; }
+
+        public Type GetRegistrationType(ContainerBuilder builder) {
             return RegistrationType;
         }
 
+        public LifetimeManager GetLifetime(ContainerBuilder builder) {
+            return Lifetime;
+        }
+
+        public Type GetImplementationType(ContainerBuilder builder) {
+            return null;
+        }
+
+        /// <summary>
+        /// Указывает зависимость, реализующую интерфейс <see cref="IFactory{TObj}"/>,
+        /// которая будет использоваться в качестве фабрики для создания объектов
+        /// </summary>
+        /// <param name="dependencyName"></param>
+        static InjectionFactory CreateInjectionFactory<TFac, TObj>(string dependencyName) where TFac : IFactory<TObj> {
+            
+            return new InjectionFactory(c => c.Resolve<TFac>(dependencyName).Create());
+        }
     }
 }
\ No newline at end of file