Auto status change to "Under Review"
| @@ -0,0 +1,40 | |||||
|
|
1 | using System.IO; | |||
|
|
2 | using Unity; | |||
|
|
3 | ||||
|
|
4 | namespace Implab.ServiceHost.Unity | |||
|
|
5 | { | |||
|
|
6 | public static class UnityContainerExtensions | |||
|
|
7 | { | |||
|
|
8 | public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file, ContainerConfigurationSchema schema) { | |||
|
|
9 | Safe.ArgumentNotNull(container, nameof(container)); | |||
|
|
10 | var builder = new ContainerBuilder(container,schema); | |||
|
|
11 | builder.LoadConfig(file); | |||
|
|
12 | return builder.Container; | |||
|
|
13 | } | |||
|
|
14 | ||||
|
|
15 | public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream, ContainerConfigurationSchema schema) { | |||
|
|
16 | Safe.ArgumentNotNull(container, nameof(container)); | |||
|
|
17 | Safe.ArgumentNotNull(stream, nameof(stream)); | |||
|
|
18 | ||||
|
|
19 | if (schema == null) | |||
|
|
20 | schema = ContainerConfigurationSchema.Default; | |||
|
|
21 | ||||
|
|
22 | var builder = new ContainerBuilder(container,schema); | |||
|
|
23 | var config = (ContainerElement)schema.Serializer.Deserialize(stream); | |||
|
|
24 | if (config.Items != null) { | |||
|
|
25 | foreach(var item in config.Items) | |||
|
|
26 | item?.Visit(builder); | |||
|
|
27 | } | |||
|
|
28 | ||||
|
|
29 | return builder.Container; | |||
|
|
30 | } | |||
|
|
31 | ||||
|
|
32 | public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream) { | |||
|
|
33 | return LoadXmlConfiguration(container, stream, ContainerConfigurationSchema.Default); | |||
|
|
34 | } | |||
|
|
35 | ||||
|
|
36 | public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file) { | |||
|
|
37 | return LoadXmlConfiguration(container, file, ContainerConfigurationSchema.Default); | |||
|
|
38 | } | |||
|
|
39 | } | |||
|
|
40 | } No newline at end of file | |||
| @@ -94,22 +94,27 namespace Implab.Playground { | |||||
| 94 | source.Switch.Level = SourceLevels.All; |
|
94 | source.Switch.Level = SourceLevels.All; | |
| 95 | source.Listeners.Add(listener); |
|
95 | source.Listeners.Add(listener); | |
| 96 |
|
96 | |||
| 97 |
var |
|
97 | var stopwatch = new Stopwatch(); | |
| 98 | resolver.AddNamespace("System"); |
|
98 | stopwatch.Start(); | |
| 99 | resolver.AddNamespace("System.Collections.Generic"); |
|
99 | ||
| 100 | resolver.AddNamespace("Implab.Playground"); |
|
100 | var container = new UnityContainer(); | |
| 101 | resolver.AddMapping("string", typeof(string)); |
|
101 | ||
| 102 | resolver.AddMapping("listOf`1", typeof(List<>)); |
|
102 | Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); | |
|
|
103 | stopwatch.Restart(); | |||
|
|
104 | ||||
|
|
105 | container.LoadXmlConfiguration("data/sample.xml"); | |||
| 103 |
|
106 | |||
| 104 | var spec = TypeReference.Parse("Container{listOf{string}}+Bar{List{string}}"); |
|
107 | Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); | |
| 105 |
|
108 | |||
| 106 | var t = resolver.Resolve(spec, true); |
|
109 | stopwatch.Restart(); | |
|
|
110 | var instace1 = container.Resolve<IContainer<string>>(); | |||
|
|
111 | Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}"); | |||
| 107 |
|
112 | |||
| 108 | Console.WriteLine("{0}", t); |
|
113 | stopwatch.Restart(); | |
| 109 | Console.WriteLine("Spec: {0}", spec); |
|
114 | var instace2 = container.Resolve<IContainer<Foo>>(); | |
| 110 | Console.WriteLine("IsGenericType: {0}", t.IsGenericType); |
|
115 | Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}"); | |
| 111 | Console.WriteLine("IsGenericTypeDefinition: {0}", t.IsGenericTypeDefinition); |
|
116 | ||
| 112 | Console.WriteLine("ContainsGenericParameters: {0}", t.ContainsGenericParameters); |
|
117 | DisplayContainerRegistrations(container); | |
| 113 | } |
|
118 | } | |
| 114 |
|
119 | |||
| 115 | static void DisplayContainerRegistrations(IUnityContainer theContainer) { |
|
120 | static void DisplayContainerRegistrations(IUnityContainer theContainer) { | |
| @@ -4,7 +4,7 namespace Implab.ServiceHost.Unity { | |||||
| 4 | /// <summary> |
|
4 | /// <summary> | |
| 5 | /// Base class for injections, each injection is applied to the type registration context. |
|
5 | /// Base class for injections, each injection is applied to the type registration context. | |
| 6 | /// </summary> |
|
6 | /// </summary> | |
| 7 | public abstract class AbstractMemberInjection { |
|
7 | public abstract class AbstractMemberInjection : ITypeMemberInjection { | |
| 8 |
|
|
8 | public abstract void Visit(TypeRegistrationBuilder builder); | |
| 9 | } |
|
9 | } | |
| 10 | } No newline at end of file |
|
10 | } | |
| @@ -8,7 +8,7 namespace Implab.ServiceHost.Unity | |||||
| 8 | /// <summary> |
|
8 | /// <summary> | |
| 9 | /// Базовая информаци о регистрации в контейнере: тип, имя и время жизни |
|
9 | /// Базовая информаци о регистрации в контейнере: тип, имя и время жизни | |
| 10 | /// </summary> |
|
10 | /// </summary> | |
| 11 | public abstract class AbstractRegistration : AbstractContainerItem { |
|
11 | public abstract class AbstractRegistration : AbstractContainerItem, IRegistration { | |
| 12 |
|
12 | |||
| 13 | /// <summary> |
|
13 | /// <summary> | |
| 14 | /// An optional name for a registration in the container |
|
14 | /// An optional name for a registration in the container | |
| @@ -30,13 +30,19 namespace Implab.ServiceHost.Unity | |||||
| 30 | [XmlAttribute("type")] |
|
30 | [XmlAttribute("type")] | |
| 31 | public string RegistrationType { get; set; } |
|
31 | public string RegistrationType { get; set; } | |
| 32 |
|
32 | |||
|
|
33 | public virtual LifetimeManager GetLifetime(ContainerBuilder builder) { | |||
|
|
34 | return Lifetime?.GetLifetime(builder); | |||
|
|
35 | } | |||
|
|
36 | ||||
| 33 | public virtual Type GetRegistrationType(Func<string,Type> resolver) { |
|
37 | public virtual Type GetRegistrationType(Func<string,Type> resolver) { | |
| 34 | return resolver(RegistrationType); |
|
38 | return resolver(RegistrationType); | |
| 35 | } |
|
39 | } | |
| 36 |
|
40 | |||
| 37 |
public virtual |
|
41 | public virtual Type GetRegistrationType(ContainerBuilder builder) { | |
| 38 | Lifetime?.Visit(builder); |
|
42 | return builder.ResolveType(RegistrationType); | |
| 39 | } |
|
43 | } | |
| 40 |
|
44 | |||
|
|
45 | ||||
|
|
46 | ||||
| 41 | } |
|
47 | } | |
| 42 | } No newline at end of file |
|
48 | } | |
| @@ -10,8 +10,8 namespace Implab.ServiceHost.Unity { | |||||
| 10 | [XmlElement("array", typeof(ArrayParameterElement))] |
|
10 | [XmlElement("array", typeof(ArrayParameterElement))] | |
| 11 | public InjectionParameterElement[] Parameters { get; set; } |
|
11 | public InjectionParameterElement[] Parameters { get; set; } | |
| 12 |
|
12 | |||
| 13 |
|
|
13 | public override void Visit(TypeRegistrationBuilder builder) { | |
| 14 |
|
|
14 | builder.Visit(this); | |
| 15 | } |
|
15 | } | |
| 16 | } |
|
16 | } | |
| 17 | } No newline at end of file |
|
17 | } | |
| @@ -7,11 +7,11 namespace Implab.ServiceHost.Unity { | |||||
| 7 |
|
7 | |||
| 8 | readonly TypeResolver m_resolver; |
|
8 | readonly TypeResolver m_resolver; | |
| 9 |
|
9 | |||
| 10 | readonly UnityContainer m_container; |
|
10 | readonly IUnityContainer m_container; | |
| 11 |
|
11 | |||
| 12 | readonly ContainerConfigurationSchema m_schema; |
|
12 | readonly ContainerConfigurationSchema m_schema; | |
| 13 |
|
13 | |||
| 14 | public UnityContainer Container { |
|
14 | public IUnityContainer Container { | |
| 15 | get { |
|
15 | get { | |
| 16 | return m_container; |
|
16 | return m_container; | |
| 17 | } |
|
17 | } | |
| @@ -20,66 +20,14 namespace Implab.ServiceHost.Unity { | |||||
| 20 | public ContainerBuilder() : this(null, null) { |
|
20 | public ContainerBuilder() : this(null, null) { | |
| 21 | } |
|
21 | } | |
| 22 |
|
22 | |||
| 23 | public ContainerBuilder(UnityContainer container, ContainerConfigurationSchema schema) { |
|
23 | public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) { | |
| 24 | m_container = container ?? new UnityContainer(); |
|
24 | m_container = container ?? new UnityContainer(); | |
| 25 | m_resolver = new TypeResolver(); |
|
25 | m_resolver = new TypeResolver(); | |
| 26 | m_schema = schema ?? ContainerConfigurationSchema.Default; |
|
26 | m_schema = schema ?? ContainerConfigurationSchema.Default; | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public Type ResolveType(string typeReference) { |
|
29 | public Type ResolveType(string typeReference) { | |
| 30 | return m_resolver.Resolve(typeReference, true); |
|
30 | return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true); | |
| 31 | } |
|
|||
| 32 |
|
||||
| 33 |
|
||||
| 34 | internal void Visit(TypeAbstractRegistration typeRegistration) { |
|
|||
| 35 | var registrationType = typeRegistration.GetRegistrationType(ResolveType); |
|
|||
| 36 | var implementationType = typeRegistration.GetImplementationType(ResolveType) ?? registrationType; |
|
|||
| 37 |
|
||||
| 38 | var registrationContext = new TypeRegistrationBuilder( |
|
|||
| 39 | m_resolver, |
|
|||
| 40 | registrationType, |
|
|||
| 41 | implementationType |
|
|||
| 42 | ); |
|
|||
| 43 |
|
||||
| 44 | typeRegistration.Visit(registrationContext); |
|
|||
| 45 |
|
||||
| 46 | m_container.RegisterType( |
|
|||
| 47 | registrationContext.RegistrationType, |
|
|||
| 48 | registrationContext.ImplementationType, |
|
|||
| 49 | typeRegistration.Name, |
|
|||
| 50 | registrationContext.Lifetime, |
|
|||
| 51 | registrationContext.Injections |
|
|||
| 52 | ); |
|
|||
| 53 | } |
|
|||
| 54 |
|
||||
| 55 | internal void Visit(InstanceAbstractRegistration instanceRegistration) { |
|
|||
| 56 | var registrationType = instanceRegistration.GetRegistrationType(ResolveType); |
|
|||
| 57 |
|
||||
| 58 | var builder = new InstanceRegistrationBuilder(m_resolver, registrationType); |
|
|||
| 59 |
|
||||
| 60 | instanceRegistration.Visit(builder); |
|
|||
| 61 |
|
||||
| 62 | m_container.RegisterInstance( |
|
|||
| 63 | builder.ValueBuilder.ValueType, |
|
|||
| 64 | instanceRegistration.Name, |
|
|||
| 65 | builder.ValueBuilder.Value, |
|
|||
| 66 | builder.Lifetime |
|
|||
| 67 | ); |
|
|||
| 68 | } |
|
|||
| 69 |
|
||||
| 70 | internal void Visit(FactoryAbstractRegistratrion factoryRgistration) { |
|
|||
| 71 | var registrationType = factoryRgistration.GetRegistrationType(ResolveType); |
|
|||
| 72 |
|
||||
| 73 | var builder = new FactoryRegistrationBuilder(registrationType); |
|
|||
| 74 |
|
||||
| 75 | factoryRgistration.Visit(builder); |
|
|||
| 76 |
|
||||
| 77 | m_container.RegisterType( |
|
|||
| 78 | builder.RegistrationType, |
|
|||
| 79 | factoryRgistration.Name, |
|
|||
| 80 | builder.Lifetime, |
|
|||
| 81 | builder.Factory |
|
|||
| 82 | ); |
|
|||
| 83 | } |
|
31 | } | |
| 84 |
|
32 | |||
| 85 | public void Visit(ITypeRegistration registration) { |
|
33 | public void Visit(ITypeRegistration registration) { | |
| @@ -136,25 +84,11 namespace Implab.ServiceHost.Unity { | |||||
| 136 | m_container.RegisterInstance( |
|
84 | m_container.RegisterInstance( | |
| 137 | builder.RegistrationType ?? builder.ValueBuilder.ValueType, |
|
85 | builder.RegistrationType ?? builder.ValueBuilder.ValueType, | |
| 138 | registration.Name, |
|
86 | registration.Name, | |
| 139 |
builder.ValueBuilder. |
|
87 | builder.ValueBuilder.Value, | |
| 140 | builder.Lifetime |
|
88 | builder.Lifetime | |
| 141 | ); |
|
89 | ); | |
| 142 | } |
|
90 | } | |
| 143 |
|
91 | |||
| 144 | public void Visit(IFactoryRegistration registration) { |
|
|||
| 145 | Safe.ArgumentNotNull(registration, nameof(registration)); |
|
|||
| 146 |
|
||||
| 147 | var registrationType = registration.GetRegistrationType(this); |
|
|||
| 148 |
|
||||
| 149 | var builder = new FactoryRegistrationBuilder(registrationType); |
|
|||
| 150 |
|
||||
| 151 | if (registration.MemberInjections != null) { |
|
|||
| 152 | foreach(var member in registration.MemberInjections) |
|
|||
| 153 | member?.Visit(builder); |
|
|||
| 154 | } |
|
|||
| 155 |
|
||||
| 156 | } |
|
|||
| 157 |
|
||||
| 158 | public void AddNamespace(string ns) { |
|
92 | public void AddNamespace(string ns) { | |
| 159 | m_resolver.AddNamespace(ns); |
|
93 | m_resolver.AddNamespace(ns); | |
| 160 | } |
|
94 | } | |
| @@ -3,8 +3,10 using Unity.Lifetime; | |||||
| 3 | namespace Implab.ServiceHost.Unity |
|
3 | namespace Implab.ServiceHost.Unity | |
| 4 | { |
|
4 | { | |
| 5 | public class ContainerLifetimeElement : LifetimeElement { |
|
5 | public class ContainerLifetimeElement : LifetimeElement { | |
| 6 |
public override |
|
6 | public override LifetimeManager GetLifetime(ContainerBuilder builder) { | |
| 7 | builder.Visit(this); |
|
7 | return new ContainerControlledLifetimeManager(); | |
| 8 | } |
|
8 | } | |
|
|
9 | ||||
|
|
10 | ||||
| 9 | } |
|
11 | } | |
| 10 | } No newline at end of file |
|
12 | } | |
| @@ -3,8 +3,8 using Unity.Lifetime; | |||||
| 3 | namespace Implab.ServiceHost.Unity |
|
3 | namespace Implab.ServiceHost.Unity | |
| 4 | { |
|
4 | { | |
| 5 | public class ContextLifetimeElement : LifetimeElement { |
|
5 | public class ContextLifetimeElement : LifetimeElement { | |
| 6 |
public override |
|
6 | public override LifetimeManager GetLifetime(ContainerBuilder builder) { | |
| 7 | builder.Visist(this); |
|
7 | return new PerResolveLifetimeManager(); | |
| 8 | } |
|
8 | } | |
| 9 | } |
|
9 | } | |
| 10 | } No newline at end of file |
|
10 | } | |
| @@ -11,7 +11,7 namespace Implab.ServiceHost.Unity { | |||||
| 11 |
|
11 | |||
| 12 | public override void Visit(InjectionParameterBuilder builder) { |
|
12 | public override void Visit(InjectionParameterBuilder builder) { | |
| 13 | var type = builder.ResolveInjectedValueType(TypeName); |
|
13 | var type = builder.ResolveInjectedValueType(TypeName); | |
| 14 |
builder.SetDependency |
|
14 | builder.SetDependency(type, DependencyName, Optional); | |
| 15 | } |
|
15 | } | |
| 16 | } |
|
16 | } | |
| 17 | } No newline at end of file |
|
17 | } | |
| @@ -1,29 +1,63 | |||||
| 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 : |
|
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 |
|
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 |
|
|
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( |
|
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 | } No newline at end of file |
|
63 | } | |
| @@ -7,7 +7,7 namespace Implab.ServiceHost.Unity { | |||||
| 7 | /// Расширяет стандартную регистрацию типа до фабрики, вместе с регистрацией |
|
7 | /// Расширяет стандартную регистрацию типа до фабрики, вместе с регистрацией | |
| 8 | /// самой фабрики создаются регистрации сервисов, которые она предоставляет. |
|
8 | /// самой фабрики создаются регистрации сервисов, которые она предоставляет. | |
| 9 | /// </summary> |
|
9 | /// </summary> | |
| 10 | public class FactoryElement : RegisterElement { |
|
10 | public class FactoryElement : RegisterElement, ITypeRegistration { | |
| 11 |
|
11 | |||
| 12 | /// <summary> |
|
12 | /// <summary> | |
| 13 | /// Записи о сервисах, которые создаются данной фабрикой. |
|
13 | /// Записи о сервисах, которые создаются данной фабрикой. | |
| @@ -38,7 +38,7 namespace Implab.ServiceHost.Unity { | |||||
| 38 | FactoryName = Name, |
|
38 | FactoryName = Name, | |
| 39 | FactoryType = factoryType |
|
39 | FactoryType = factoryType | |
| 40 | }; |
|
40 | }; | |
| 41 |
|
|
41 | builder.Visit(activator); | |
| 42 | } |
|
42 | } | |
| 43 | } else { |
|
43 | } else { | |
| 44 | // если регистрация явно не задана, в качестве сервиса для регистрации |
|
44 | // если регистрация явно не задана, в качестве сервиса для регистрации | |
| @@ -65,7 +65,7 namespace Implab.ServiceHost.Unity { | |||||
| 65 | FactoryType = factoryType |
|
65 | FactoryType = factoryType | |
| 66 | }; |
|
66 | }; | |
| 67 |
|
67 | |||
| 68 |
|
|
68 | builder.Visit(activator); | |
| 69 | } |
|
69 | } | |
| 70 | } |
|
70 | } | |
| 71 | } |
|
71 | } | |
| @@ -3,8 +3,8 using Unity.Lifetime; | |||||
| 3 | namespace Implab.ServiceHost.Unity |
|
3 | namespace Implab.ServiceHost.Unity | |
| 4 | { |
|
4 | { | |
| 5 | public class HierarchicalLifetimeElement : LifetimeElement { |
|
5 | public class HierarchicalLifetimeElement : LifetimeElement { | |
| 6 |
public override |
|
6 | public override LifetimeManager GetLifetime(ContainerBuilder builder) { | |
| 7 | builder.Visit(this); |
|
7 | return new HierarchicalLifetimeManager(); | |
| 8 | } |
|
8 | } | |
| 9 | } |
|
9 | } | |
| 10 | } No newline at end of file |
|
10 | } | |
| @@ -2,7 +2,7 using System; | |||||
| 2 | using System.Xml.Serialization; |
|
2 | using System.Xml.Serialization; | |
| 3 |
|
3 | |||
| 4 | namespace Implab.ServiceHost.Unity { |
|
4 | namespace Implab.ServiceHost.Unity { | |
| 5 | public abstract class InjectionParameterElement { |
|
5 | public abstract class InjectionParameterElement : IInjectionParameter { | |
| 6 |
|
6 | |||
| 7 | [XmlAttribute("type")] |
|
7 | [XmlAttribute("type")] | |
| 8 | public string TypeName { get; set; } |
|
8 | public string TypeName { get; set; } | |
| @@ -1,4 +1,5 | |||||
| 1 | using System; |
|
1 | using System; | |
|
|
2 | using System.Collections; | |||
| 2 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
| 3 | using System.ComponentModel; |
|
4 | using System.ComponentModel; | |
| 4 | using System.Linq; |
|
5 | using System.Linq; | |
| @@ -15,17 +16,32 namespace Implab.ServiceHost.Unity { | |||||
| 15 |
|
16 | |||
| 16 | public Type ValueType { get; private set; } |
|
17 | public Type ValueType { get; private set; } | |
| 17 |
|
18 | |||
| 18 |
|
|
19 | object m_value; | |
| 19 |
|
20 | |||
| 20 | internal InjectionParameterValue Injection { |
|
21 | public object Value { | |
| 21 | get { |
|
22 | get { | |
| 22 |
if ( |
|
23 | if (!ValueSpecified) | |
| 23 | return InjectionParameterValue.ToParameter(Value); |
|
24 | throw new InvalidOperationException("The regular value must be set (dependency or array are not situable in this context)"); | |
|
|
25 | return m_value; | |||
|
|
26 | } | |||
|
|
27 | } | |||
|
|
28 | ||||
|
|
29 | public bool ValueSpecified { get; private set; } | |||
|
|
30 | ||||
|
|
31 | InjectionParameterValue m_injection; | |||
| 24 |
|
32 | |||
| 25 |
|
|
33 | public InjectionParameterValue Injection { | |
|
|
34 | get { | |||
|
|
35 | if (m_injection == null) | |||
|
|
36 | throw new InvalidOperationException("The injection parameter is not specified"); | |||
|
|
37 | return m_injection; | |||
| 26 | } |
|
38 | } | |
| 27 | } |
|
39 | } | |
| 28 |
|
40 | |||
|
|
41 | public bool InjectionSpecified { | |||
|
|
42 | get { return m_injection != null; } | |||
|
|
43 | } | |||
|
|
44 | ||||
| 29 | internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) { |
|
45 | internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) { | |
| 30 | m_resolver = resolver; |
|
46 | m_resolver = resolver; | |
| 31 | DefaultType = defaultType; |
|
47 | DefaultType = defaultType; | |
| @@ -41,33 +57,42 namespace Implab.ServiceHost.Unity { | |||||
| 41 | } |
|
57 | } | |
| 42 |
|
58 | |||
| 43 | public Type ResolveType(string typeSpec) { |
|
59 | public Type ResolveType(string typeSpec) { | |
| 44 | return m_resolver.Resolve(typeSpec, true); |
|
60 | return string.IsNullOrEmpty(typeSpec) ? null : m_resolver.Resolve(typeSpec, true); | |
| 45 | } |
|
61 | } | |
| 46 |
|
62 | |||
| 47 | public void SetValue(Type type, object value) { |
|
63 | public void SetValue(Type type, object value) { | |
|
|
64 | Safe.ArgumentNotNull(type, nameof(type)); | |||
|
|
65 | ||||
| 48 | ValueType = type; |
|
66 | ValueType = type; | |
| 49 |
|
|
67 | m_value = value; | |
|
|
68 | ValueSpecified = true; | |||
|
|
69 | ||||
|
|
70 | m_injection = new InjectionParameter(type, value); | |||
| 50 | } |
|
71 | } | |
| 51 |
|
72 | |||
| 52 | public void SetValue<T>(T value) { |
|
73 | public void SetDependency(Type type, string name, bool optional) { | |
| 53 | SetValue(typeof(T), value); |
|
74 | Safe.ArgumentNotNull(type, nameof(type)); | |
| 54 | } |
|
|||
| 55 |
|
75 | |||
| 56 | public void SetDependencyReference(Type type, string name, bool optional) { |
|
|||
| 57 | ValueType = type; |
|
76 | ValueType = type; | |
| 58 | Value = optional ? (object)new OptionalParameter(type, name) : new ResolvedParameter(type, name); |
|
77 | ValueSpecified = false; | |
|
|
78 | m_value = null; | |||
|
|
79 | ||||
|
|
80 | m_injection = optional ? (InjectionParameterValue)new OptionalParameter(type, name) : new ResolvedParameter(type, name); | |||
| 59 | } |
|
81 | } | |
| 60 |
|
82 | |||
| 61 | internal void Visit(ArrayParameterElement arrayParameter) { |
|
83 | internal void Visit(ArrayParameterElement arrayParameter) { | |
| 62 | Type itemsType = null; |
|
84 | Type itemsType = null; | |
| 63 | var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName); |
|
85 | var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName); | |
| 64 |
|
86 | |||
|
|
87 | if (arrayType == null) | |||
|
|
88 | arrayType = DefaultType; | |||
|
|
89 | ||||
|
|
90 | ||||
| 65 | if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) { |
|
91 | if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) { | |
| 66 | itemsType = ResolveType(arrayParameter.ItemsType); |
|
92 | itemsType = ResolveType(arrayParameter.ItemsType); | |
| 67 |
|
|
93 | arrayType = itemsType.MakeArrayType(); | |
| 68 | arrayType = itemsType.MakeArrayType(); |
|
|||
| 69 | } else { |
|
94 | } else { | |
| 70 | itemsType = arrayType?.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0]; |
|
95 | itemsType = GetItemsType(arrayType); | |
| 71 | } |
|
96 | } | |
| 72 |
|
97 | |||
| 73 | if (itemsType == null) |
|
98 | if (itemsType == null) | |
| @@ -82,11 +107,31 namespace Implab.ServiceHost.Unity { | |||||
| 82 | .ToArray(); |
|
107 | .ToArray(); | |
| 83 |
|
108 | |||
| 84 | var array = itemsType.IsGenericParameter ? |
|
109 | var array = itemsType.IsGenericParameter ? | |
| 85 |
( |
|
110 | (InjectionParameterValue)new GenericResolvedArrayParameter(itemsType.Name, injections) : | |
| 86 | new ResolvedArrayParameter(itemsType, injections); |
|
111 | new ResolvedArrayParameter(itemsType, injections); | |
| 87 |
|
112 | |||
| 88 | ValueType = arrayType; |
|
113 | ValueType = arrayType; | |
| 89 |
|
|
114 | m_value = null; | |
|
|
115 | ValueSpecified = false; | |||
|
|
116 | ||||
|
|
117 | m_injection = array; | |||
|
|
118 | } | |||
|
|
119 | ||||
|
|
120 | Type GetItemsType(Type collectionType) { | |||
|
|
121 | if (collectionType == null) | |||
|
|
122 | return null; | |||
|
|
123 | ||||
|
|
124 | Type itemsType = null; | |||
|
|
125 | ||||
|
|
126 | if (collectionType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { | |||
|
|
127 | itemsType = collectionType.GetGenericArguments()[0]; | |||
|
|
128 | } else if (collectionType == typeof(IEnumerable)) { | |||
|
|
129 | itemsType = typeof(object); | |||
|
|
130 | } else { | |||
|
|
131 | itemsType = collectionType.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0]; | |||
|
|
132 | } | |||
|
|
133 | ||||
|
|
134 | return itemsType; | |||
| 90 | } |
|
135 | } | |
| 91 | } |
|
136 | } | |
| 92 | } No newline at end of file |
|
137 | } | |
| @@ -1,5 +1,7 | |||||
|
|
1 | using Unity.Lifetime; | |||
|
|
2 | ||||
| 1 | namespace Implab.ServiceHost.Unity { |
|
3 | namespace Implab.ServiceHost.Unity { | |
| 2 | public abstract class LifetimeElement { |
|
4 | public abstract class LifetimeElement { | |
| 3 |
public abstract |
|
5 | public abstract LifetimeManager GetLifetime(ContainerBuilder builder); | |
| 4 | } |
|
6 | } | |
| 5 | } No newline at end of file |
|
7 | } | |
| @@ -13,7 +13,7 namespace Implab.ServiceHost.Unity { | |||||
| 13 | [XmlElement("array", typeof(ArrayParameterElement))] |
|
13 | [XmlElement("array", typeof(ArrayParameterElement))] | |
| 14 | public InjectionParameterElement[] Parameters { get; set; } |
|
14 | public InjectionParameterElement[] Parameters { get; set; } | |
| 15 |
|
15 | |||
| 16 |
|
|
16 | public override void Visit(TypeRegistrationBuilder context) { | |
| 17 | context.Visit(this); |
|
17 | context.Visit(this); | |
| 18 | } |
|
18 | } | |
| 19 | } |
|
19 | } | |
| @@ -13,7 +13,7 namespace Implab.ServiceHost.Unity { | |||||
| 13 | [XmlElement("array", typeof(ArrayParameterElement))] |
|
13 | [XmlElement("array", typeof(ArrayParameterElement))] | |
| 14 | public InjectionParameterElement Value { get; set; } |
|
14 | public InjectionParameterElement Value { get; set; } | |
| 15 |
|
15 | |||
| 16 |
|
|
16 | public override void Visit(TypeRegistrationBuilder context) { | |
| 17 | context.Visit(this); |
|
17 | context.Visit(this); | |
| 18 | } |
|
18 | } | |
| 19 | } |
|
19 | } | |
| @@ -1,4 +1,5 | |||||
| 1 | using System; |
|
1 | using System; | |
|
|
2 | using System.Collections.Generic; | |||
| 2 | using System.Xml.Serialization; |
|
3 | using System.Xml.Serialization; | |
| 3 | using Unity.Lifetime; |
|
4 | using Unity.Lifetime; | |
| 4 | using Unity.Registration; |
|
5 | using Unity.Registration; | |
| @@ -6,7 +7,7 using Unity.Registration; | |||||
| 6 | namespace Implab.ServiceHost.Unity { |
|
7 | namespace Implab.ServiceHost.Unity { | |
| 7 |
|
8 | |||
| 8 | [XmlRoot("register", Namespace = Schema.ContainerConfigurationNamespace)] |
|
9 | [XmlRoot("register", Namespace = Schema.ContainerConfigurationNamespace)] | |
| 9 |
public class RegisterElement : |
|
10 | public class RegisterElement : AbstractRegistration, ITypeRegistration { | |
| 10 |
|
11 | |||
| 11 | /// <summary> |
|
12 | /// <summary> | |
| 12 | /// An optional type which is registered as a service in the container, must be assignable to <see cref="ProvidesType">. |
|
13 | /// An optional type which is registered as a service in the container, must be assignable to <see cref="ProvidesType">. | |
| @@ -20,17 +21,19 namespace Implab.ServiceHost.Unity { | |||||
| 20 | [XmlElement("method", typeof(MethodInjectionElement))] |
|
21 | [XmlElement("method", typeof(MethodInjectionElement))] | |
| 21 | public AbstractMemberInjection[] Injectors { get; set; } |
|
22 | public AbstractMemberInjection[] Injectors { get; set; } | |
| 22 |
|
23 | |||
| 23 | public override Type GetImplementationType(Func<string, Type> resolver) { |
|
24 | IEnumerable<ITypeMemberInjection> ITypeRegistration.MemberInjections { | |
| 24 | return string.IsNullOrEmpty(MapToType) ? null : resolver(MapToType); |
|
25 | get { | |
|
|
26 | return Injectors; | |||
|
|
27 | } | |||
| 25 | } |
|
28 | } | |
| 26 |
|
29 | |||
| 27 |
public |
|
30 | public Type GetImplementationType(ContainerBuilder builder) { | |
| 28 | if(Injectors != null) |
|
31 | return builder.ResolveType(MapToType); | |
| 29 | foreach(var injector in Injectors) |
|
|||
| 30 | injector.Visit(builder); |
|
|||
| 31 | } |
|
32 | } | |
| 32 |
|
33 | |||
| 33 |
|
34 | public override void Visit(ContainerBuilder builder) { | ||
|
|
35 | builder.Visit(this); | |||
|
|
36 | } | |||
| 34 | } |
|
37 | } | |
| 35 |
|
38 | |||
| 36 | } No newline at end of file |
|
39 | } | |
| @@ -1,10 +1,10 | |||||
| 1 | using System; |
|
1 | using System; | |
|
|
2 | using System.Collections.Generic; | |||
| 2 | using System.Xml; |
|
3 | using System.Xml; | |
| 3 | using System.Xml.Serialization; |
|
4 | using System.Xml.Serialization; | |
| 4 |
|
5 | |||
| 5 | namespace Implab.ServiceHost.Unity |
|
6 | namespace Implab.ServiceHost.Unity { | |
| 6 | { |
|
7 | public class SerializedElement : AbstractRegistration, IInstanceRegistration { | |
| 7 | public class SerializedElement : InstanceAbstractRegistration { |
|
|||
| 8 | [XmlAttribute("href")] |
|
8 | [XmlAttribute("href")] | |
| 9 | public string Location { get; set; } |
|
9 | public string Location { get; set; } | |
| 10 |
|
10 | |||
| @@ -15,15 +15,18 namespace Implab.ServiceHost.Unity | |||||
| 15 | [XmlAnyElement] |
|
15 | [XmlAnyElement] | |
| 16 | public XmlElement[] Content { get; set; } |
|
16 | public XmlElement[] Content { get; set; } | |
| 17 |
|
17 | |||
| 18 | public override void Visit(InstanceRegistrationBuilder builder) { |
|
18 | public IEnumerable<IInjectionParameter> MemberInjections { | |
| 19 | base.Visit(builder); |
|
19 | get { | |
|
|
20 | yield return new SerializedParameterElement { | |||
|
|
21 | TypeName = SerializedType, | |||
|
|
22 | Location = Location, | |||
|
|
23 | Content = Content | |||
|
|
24 | }; | |||
|
|
25 | } | |||
|
|
26 | } | |||
| 20 |
|
27 | |||
| 21 | var parameter = new SerializedParameterElement { |
|
28 | public override void Visit(ContainerBuilder builder) { | |
| 22 | TypeName = SerializedType, |
|
29 | builder.Visit(this); | |
| 23 | Location = Location, |
|
|||
| 24 | Content = Content |
|
|||
| 25 | }; |
|
|||
| 26 | parameter.Visit(builder.ValueBuilder); |
|
|||
| 27 | } |
|
30 | } | |
| 28 | } |
|
31 | } | |
| 29 | } No newline at end of file |
|
32 | } | |
| @@ -1,8 +1,10 | |||||
|
|
1 | using Unity.Lifetime; | |||
|
|
2 | ||||
| 1 | namespace Implab.ServiceHost.Unity |
|
3 | namespace Implab.ServiceHost.Unity | |
| 2 | { |
|
4 | { | |
| 3 | public class SingletonLifetimeElement : LifetimeElement { |
|
5 | public class SingletonLifetimeElement : LifetimeElement { | |
| 4 |
public override |
|
6 | public override LifetimeManager GetLifetime(ContainerBuilder builder) { | |
| 5 | builder.Visit(this); |
|
7 | return new SingletonLifetimeManager(); | |
| 6 | } |
|
8 | } | |
| 7 | } |
|
9 | } | |
| 8 | } No newline at end of file |
|
10 | } | |
| @@ -71,5 +71,11 namespace Implab.ServiceHost.Unity { | |||||
| 71 | var injection = new InjectionProperty(propertyInjection.Name, valueContext.Injection); |
|
71 | var injection = new InjectionProperty(propertyInjection.Name, valueContext.Injection); | |
| 72 | m_injections.Add(injection); |
|
72 | m_injections.Add(injection); | |
| 73 | } |
|
73 | } | |
|
|
74 | ||||
|
|
75 | public void AddInjectionMember(InjectionMember injection) { | |||
|
|
76 | Safe.ArgumentNotNull(injection, nameof(injection)); | |||
|
|
77 | ||||
|
|
78 | m_injections.Add(injection); | |||
|
|
79 | } | |||
| 74 | } |
|
80 | } | |
| 75 | } No newline at end of file |
|
81 | } | |
| @@ -27,6 +27,7 namespace Implab.ServiceHost.Unity { | |||||
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public Type Resolve(string typeSpec, bool throwOnFail) { |
|
29 | public Type Resolve(string typeSpec, bool throwOnFail) { | |
|
|
30 | Safe.ArgumentNotEmpty(typeSpec, nameof(typeSpec)); | |||
| 30 | var typeReference = TypeReference.Parse(typeSpec); |
|
31 | var typeReference = TypeReference.Parse(typeSpec); | |
| 31 | return Resolve(typeReference, throwOnFail); |
|
32 | return Resolve(typeReference, throwOnFail); | |
| 32 | } |
|
33 | } | |
| @@ -1,8 +1,9 | |||||
|
|
1 | using System.Collections.Generic; | |||
| 1 | using System.Xml.Serialization; |
|
2 | using System.Xml.Serialization; | |
| 2 |
|
3 | |||
| 3 | namespace Implab.ServiceHost.Unity { |
|
4 | namespace Implab.ServiceHost.Unity { | |
| 4 |
public class ValueElement : |
|
5 | public class ValueElement : AbstractRegistration, IInstanceRegistration { | |
| 5 |
|
6 | |||
| 6 | [XmlAttribute("value")] |
|
7 | [XmlAttribute("value")] | |
| 7 | public string Value { get; set; } |
|
8 | public string Value { get; set; } | |
| 8 |
|
9 | |||
| @@ -19,13 +20,17 namespace Implab.ServiceHost.Unity { | |||||
| 19 | } |
|
20 | } | |
| 20 | } |
|
21 | } | |
| 21 |
|
22 | |||
| 22 | public override void Visit(InstanceRegistrationBuilder builder) { |
|
23 | public IEnumerable<IInjectionParameter> MemberInjections { | |
| 23 | base.Visit(builder); |
|
24 | get { | |
| 24 |
|
|
25 | yield return new ValueParameterElement { | |
| 25 | Value = Value, |
|
26 | Value = Value, | |
| 26 | Text = Text |
|
27 | Text = Text | |
| 27 | }; |
|
28 | }; | |
| 28 | parameter.Visit(builder.ValueBuilder); |
|
29 | } | |
|
|
30 | } | |||
|
|
31 | ||||
|
|
32 | public override void Visit(ContainerBuilder builder) { | |||
|
|
33 | builder.Visit(this); | |||
| 29 | } |
|
34 | } | |
| 30 | } |
|
35 | } | |
| 31 | } No newline at end of file |
|
36 | } | |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now
