Mercurial > pub > ImplabNet
annotate Implab.ServiceHost/Unity/FactoryActivator.cs @ 281:e0916ddc9950 v3 tip
code cleanup and refactoring
author | cin |
---|---|
date | Fri, 01 Jun 2018 21:35:24 +0300 |
parents | 8714471e8d78 |
children |
rev | line source |
---|---|
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
1 using System; |
279 | 2 using System.Collections.Generic; |
3 using System.Reflection; | |
4 using Implab.Components; | |
5 using Unity; | |
6 using Unity.Injection; | |
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 | 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 | |
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 | 24 public Type RegistrationType { get; set; } |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
25 |
279 | 26 public LifetimeManager Lifetime { get; set; } |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
27 |
279 | 28 public IEnumerable<ITypeMemberInjection> MemberInjections { |
29 get { | |
30 yield return new FactoryInjector { | |
31 Factory = (InjectionFactory)GetType() | |
32 .GetMethod(nameof(CreateInjectionFactory), BindingFlags.Static | BindingFlags.NonPublic) | |
33 .MakeGenericMethod(FactoryType, RegistrationType) | |
34 .Invoke(null, new [] { FactoryName }) | |
35 }; | |
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 | 39 public string Name { get; set; } |
40 | |
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 | 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 } | |
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 } |