Mercurial > pub > ImplabNet
annotate Implab.ServiceHost/Unity/FactoryElement.cs @ 280:f07be402ab02 v3
Added Trace<T>.Debug(...) method for debug messages
Added ContainerBuilde.LoadConfig(Uri) method
| author | cin |
|---|---|
| date | Fri, 25 May 2018 19:15:26 +0300 |
| parents | 8714471e8d78 |
| children | e0916ddc9950 |
| rev | line source |
|---|---|
|
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
1 using System; |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
2 using System.Xml.Serialization; |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
3 using Implab.Components; |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
4 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
5 namespace Implab.ServiceHost.Unity { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
6 /// <summary> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
7 /// Расширяет стандартную регистрацию типа до фабрики, вместе с регистрацией |
|
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 /// </summary> |
| 279 | 10 public class FactoryElement : RegisterElement, ITypeRegistration { |
|
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
11 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
12 /// <summary> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
13 /// Записи о сервисах, которые создаются данной фабрикой. |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
14 /// </summary> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
15 /// <remarks> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
16 /// Сервисы, которые указаны в регистарциях они должны соответсвовать тому, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
17 /// что фабрика возвращает, но это остается на откуп контейнера |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
18 /// </remarks> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
19 [XmlElement("provides")] |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
20 public ProvidesElement[] Provides { 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 /// <summary> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
23 /// Переопределяет стандарное поведение регистрации типа в контейнере, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
24 /// дополняя его регистрацией фабричных методов для получения типов. |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
25 /// </summary> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
26 /// <param name="builder">Объект для конфигурирования контейнера.</param> |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
27 public override void Visit(ContainerBuilder builder) { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
28 var factoryType = GetRegistrationType(builder.ResolveType); |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
29 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
30 base.Visit(builder); |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
31 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
32 if (Provides != null && Provides.Length > 0) { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
33 // если регистрации явно заданы, используеися информация из них |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
34 foreach(var item in Provides) { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
35 var activator = new FactoryActivator { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
36 Name = item.RegistrationName, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
37 RegistrationType = builder.ResolveType(item.RegistrationType), |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
38 FactoryName = Name, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
39 FactoryType = factoryType |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
40 }; |
| 279 | 41 builder.Visit(activator); |
|
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
42 } |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
43 } else { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
44 // если регистрация явно не задана, в качестве сервиса для регистрации |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
45 // используется тип создаваемый фабрикой, который будет добавлен в контейнер |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
46 // с темже именем, что и сама фабрика (разные типы могут иметь одно имя для регистрации) |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
47 var providedType = ( |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
48 factoryType.IsGenericType && factoryType.GetGenericTypeDefinition() == typeof(IFactory<>) ? |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
49 factoryType : |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
50 factoryType.GetInterface(typeof(IFactory<>).FullName) |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
51 )? |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
52 .GetGenericArguments()[0]; |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
53 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
54 // не удалось определеить тип |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
55 if (providedType == null) |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
56 throw new ArgumentException("Failed to determine a type provided by the factory"); |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
57 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
58 if (providedType.IsGenericParameter) |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
59 throw new ArgumentException("Can't register a generic type paramter as a service"); |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
60 |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
61 var activator = new FactoryActivator { |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
62 Name = Name, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
63 RegistrationType = providedType, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
64 FactoryName = Name, |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
65 FactoryType = factoryType |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
66 }; |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
67 |
| 279 | 68 builder.Visit(activator); |
|
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
69 } |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
70 } |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
71 } |
|
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff
changeset
|
72 } |
