comparison 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
comparison
equal deleted inserted replaced
278:6691aff01de1 279:8714471e8d78
1 using System; 1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using Implab.Components;
5 using Unity;
6 using Unity.Injection;
7 using Unity.Lifetime;
2 8
3 namespace Implab.ServiceHost.Unity { 9 namespace Implab.ServiceHost.Unity {
4 public class FactoryActivator : FactoryAbstractRegistratrion { 10 public class FactoryActivator : ITypeRegistration {
11
12 class FactoryInjector : ITypeMemberInjection {
13 public InjectionFactory Factory { get; set; }
14 public void Visit(TypeRegistrationBuilder builder) {
15 builder.AddInjectionMember(Factory);
16 }
17 }
18
5 19
6 public Type FactoryType { get; set; } 20 public Type FactoryType { get; set; }
7 21
8 public string FactoryName { get; set; } 22 public string FactoryName { get; set; }
9 23
10 public new Type RegistrationType { get; set; } 24 public Type RegistrationType { get; set; }
11 25
12 public override void Visit(FactoryRegistrationBuilder builder) { 26 public LifetimeManager Lifetime { get; set; }
13 base.Visit(builder);
14 27
15 builder.GetType() 28 public IEnumerable<ITypeMemberInjection> MemberInjections {
16 .GetMethod( 29 get {
17 nameof(FactoryRegistrationBuilder.SetFactoryDependency) 30 yield return new FactoryInjector {
18 , new[] { typeof(string) } 31 Factory = (InjectionFactory)GetType()
19 ) 32 .GetMethod(nameof(CreateInjectionFactory), BindingFlags.Static | BindingFlags.NonPublic)
20 .MakeGenericMethod(FactoryType, RegistrationType) 33 .MakeGenericMethod(FactoryType, RegistrationType)
21 .Invoke(builder, new[] { FactoryName }); 34 .Invoke(null, new [] { FactoryName })
35 };
36 }
22 } 37 }
23 38
24 public override Type GetRegistrationType(Func<string, Type> resolver) { 39 public string Name { get; set; }
40
41 public Type GetRegistrationType(ContainerBuilder builder) {
25 return RegistrationType; 42 return RegistrationType;
26 } 43 }
27 44
45 public LifetimeManager GetLifetime(ContainerBuilder builder) {
46 return Lifetime;
47 }
48
49 public Type GetImplementationType(ContainerBuilder builder) {
50 return null;
51 }
52
53 /// <summary>
54 /// Указывает зависимость, реализующую интерфейс <see cref="IFactory{TObj}"/>,
55 /// которая будет использоваться в качестве фабрики для создания объектов
56 /// </summary>
57 /// <param name="dependencyName"></param>
58 static InjectionFactory CreateInjectionFactory<TFac, TObj>(string dependencyName) where TFac : IFactory<TObj> {
59
60 return new InjectionFactory(c => c.Resolve<TFac>(dependencyName).Create());
61 }
28 } 62 }
29 } 63 }