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