| @@ -0,0 +1,64 | |||||
|
|
1 | <?xml version="1.0"?> | |||
|
|
2 | <container xmlns="http://implab.org/schemas/servicehost/unity.v1.xsd"> | |||
|
|
3 | <namespace name="System"/> | |||
|
|
4 | <namespace name="System.Collections.Generic"/> | |||
|
|
5 | <namespace name="Implab.Components"/> | |||
|
|
6 | <namespace name="Implab.ServiceHost.Test.Mock"/> | |||
|
|
7 | ||||
|
|
8 | <register type="Baz"> | |||
|
|
9 | <property name="Nuts"> | |||
|
|
10 | <!-- should resolve to all registered named dependencies (THIS IS A UNITY FEATURE) --> | |||
|
|
11 | <dependency /> | |||
|
|
12 | </property> | |||
|
|
13 | </register> | |||
|
|
14 | ||||
|
|
15 | <register name="baz2" type="Baz"> | |||
|
|
16 | <property name="Nuts"> | |||
|
|
17 | <!-- should resolve as is --> | |||
|
|
18 | <array> | |||
|
|
19 | <dependency/> | |||
|
|
20 | <dependency name="Big"/> | |||
|
|
21 | <dependency name="Big"/> | |||
|
|
22 | <dependency name="Small"/> | |||
|
|
23 | <dependency name="Mid"/> | |||
|
|
24 | </array> | |||
|
|
25 | </property> | |||
|
|
26 | </register> | |||
|
|
27 | ||||
|
|
28 | <!-- useless --> | |||
|
|
29 | <array> | |||
|
|
30 | <dependency name="Big"/> | |||
|
|
31 | <dependency name="Big"/> | |||
|
|
32 | <dependency name="Big"/> | |||
|
|
33 | </array> | |||
|
|
34 | ||||
|
|
35 | <array name="a1"> | |||
|
|
36 | <dependency name="Big"/> | |||
|
|
37 | <dependency name="Big"/> | |||
|
|
38 | <dependency name="Big"/> | |||
|
|
39 | </array> | |||
|
|
40 | ||||
|
|
41 | <register name="Big" type="Baz+Nut"> | |||
|
|
42 | <property name="Size"> | |||
|
|
43 | <value>5</value> | |||
|
|
44 | </property> | |||
|
|
45 | </register> | |||
|
|
46 | <register name="Mid" type="Baz+Nut"> | |||
|
|
47 | <property name="Size"> | |||
|
|
48 | <value>3</value> | |||
|
|
49 | </property> | |||
|
|
50 | </register> | |||
|
|
51 | ||||
|
|
52 | <register name="Small" type="Baz+Nut"> | |||
|
|
53 | <property name="Size"> | |||
|
|
54 | <value>1</value> | |||
|
|
55 | </property> | |||
|
|
56 | </register> | |||
|
|
57 | ||||
|
|
58 | <register type="Baz+Nut"> | |||
|
|
59 | <property name="Size"> | |||
|
|
60 | <value>2</value> | |||
|
|
61 | </property> | |||
|
|
62 | </register> | |||
|
|
63 | ||||
|
|
64 | </container> No newline at end of file | |||
| @@ -1,37 +1,37 | |||||
| 1 | { |
|
1 | { | |
| 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 |
|
2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 | |
| 3 | // for the documentation about the tasks.json format |
|
3 | // for the documentation about the tasks.json format | |
| 4 |
"version": " |
|
4 | "version": "2.0.0", | |
| 5 | "command": "dotnet", |
|
5 | "command": "dotnet", | |
| 6 | "args": [ |
|
6 | "args": [ | |
| 7 | ], |
|
7 | ], | |
| 8 | "showOutput": "silent", |
|
|||
| 9 | "tasks": [ |
|
8 | "tasks": [ | |
| 10 | { |
|
9 | { | |
| 11 |
" |
|
10 | "label": "build", | |
| 12 | // Show the output window only if unrecognized errors occur. |
|
11 | "command": "dotnet", | |
| 13 |
"s |
|
12 | "args": [ | |
| 14 | // Use the standard MS compiler pattern to detect errors, warnings and infos |
|
13 | "build", | |
|
|
14 | "/p:Configuration=Debug" | |||
|
|
15 | ], | |||
| 15 | "problemMatcher": "$msCompile", |
|
16 | "problemMatcher": "$msCompile", | |
| 16 |
|
17 | "group": "build" | ||
| 17 | "args" : [ |
|
|||
| 18 | "/p:Configuration=Debug" |
|
|||
| 19 | ] |
|
|||
| 20 | }, |
|
18 | }, | |
| 21 | { |
|
19 | { | |
| 22 |
" |
|
20 | "label": "clean", | |
| 23 | // Show the output window only if unrecognized errors occur. |
|
21 | "command": "dotnet", | |
| 24 |
"s |
|
22 | "args": [ | |
| 25 | // Use the standard MS compiler pattern to detect errors, warnings and infos |
|
23 | "clean" | |
|
|
24 | ], | |||
| 26 | "problemMatcher": "$msCompile" |
|
25 | "problemMatcher": "$msCompile" | |
| 27 | }, |
|
26 | }, | |
| 28 | { |
|
27 | { | |
| 29 |
" |
|
28 | "label": "test", | |
| 30 |
" |
|
29 | "command": "dotnet", | |
| 31 | // Show the output window only if unrecognized errors occur. |
|
30 | "args": [ | |
| 32 | "showOutput": "always", |
|
31 | "test" | |
| 33 | // Use the standard MS compiler pattern to detect errors, warnings and infos |
|
32 | ], | |
| 34 | "problemMatcher": "$msCompile" |
|
33 | "problemMatcher": "$msCompile", | |
|
|
34 | "group": "test" | |||
| 35 | } |
|
35 | } | |
| 36 | ] |
|
36 | ] | |
| 37 | } No newline at end of file |
|
37 | } | |
| @@ -1,27 +1,27 | |||||
| 1 | ο»Ώ<Project Sdk="Microsoft.NET.Sdk"> |
|
1 | ο»Ώ<Project Sdk="Microsoft.NET.Sdk"> | |
| 2 | <PropertyGroup Condition="'$(OSTYPE)'=='linux'"> |
|
2 | <PropertyGroup Condition="'$(OSTYPE)'=='linux'"> | |
| 3 |
<TargetFrameworks>netcoreapp |
|
3 | <TargetFrameworks>netcoreapp3.1;net46</TargetFrameworks> | |
| 4 | <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46'">/usr/lib/mono/4.6-api/</FrameworkPathOverride> |
|
4 | <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46'">/usr/lib/mono/4.6-api/</FrameworkPathOverride> | |
| 5 | </PropertyGroup> |
|
5 | </PropertyGroup> | |
| 6 |
|
6 | |||
| 7 | <PropertyGroup Condition="'$(OSTYPE)'=='windows'"> |
|
7 | <PropertyGroup Condition="'$(OSTYPE)'=='windows'"> | |
| 8 | <TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks> |
|
8 | <TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks> | |
| 9 | </PropertyGroup> |
|
9 | </PropertyGroup> | |
| 10 |
|
10 | |||
| 11 | <PropertyGroup> |
|
11 | <PropertyGroup> | |
| 12 | <OutputType>Exe</OutputType> |
|
12 | <OutputType>Exe</OutputType> | |
| 13 | <IsPackable>false</IsPackable> |
|
13 | <IsPackable>false</IsPackable> | |
| 14 | </PropertyGroup> |
|
14 | </PropertyGroup> | |
| 15 |
|
15 | |||
| 16 | <ItemGroup> |
|
16 | <ItemGroup> | |
| 17 | <ProjectReference Include="../Implab/Implab.csproj" /> |
|
17 | <ProjectReference Include="../Implab/Implab.csproj" /> | |
| 18 | <ProjectReference Include="..\Implab.ServiceHost\Implab.ServiceHost.csproj" /> |
|
18 | <ProjectReference Include="..\Implab.ServiceHost\Implab.ServiceHost.csproj" /> | |
| 19 | </ItemGroup> |
|
19 | </ItemGroup> | |
| 20 |
|
20 | |||
| 21 | <ItemGroup> |
|
21 | <ItemGroup> | |
| 22 | <PackageReference Include="Unity" Version="5.8.6" /> |
|
22 | <PackageReference Include="Unity" Version="5.8.6" /> | |
| 23 | <PackageReference Include="System.Reactive" Version="4.0.0" /> |
|
23 | <PackageReference Include="System.Reactive" Version="4.0.0" /> | |
| 24 |
|
24 | |||
| 25 | </ItemGroup> |
|
25 | </ItemGroup> | |
| 26 |
|
26 | |||
| 27 | </Project> |
|
27 | </Project> | |
| @@ -1,94 +1,125 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.IO; |
|
3 | using System.IO; | |
| 4 | using System.Linq; |
|
4 | using System.Linq; | |
| 5 | using System.Threading; |
|
5 | using System.Threading; | |
| 6 | using System.Threading.Tasks; |
|
6 | using System.Threading.Tasks; | |
| 7 | using Implab.Components; |
|
7 | using Implab.Components; | |
| 8 | using Implab.Diagnostics; |
|
8 | using Implab.Diagnostics; | |
| 9 | using Implab.ServiceHost.Test.Mock; |
|
9 | using Implab.ServiceHost.Test.Mock; | |
| 10 | using Implab.ServiceHost.Unity; |
|
10 | using Implab.ServiceHost.Unity; | |
| 11 | using Unity; |
|
11 | using Unity; | |
| 12 | using Xunit; |
|
12 | using Xunit; | |
| 13 |
|
13 | |||
| 14 | namespace Implab.Test { |
|
14 | namespace Implab.Test { | |
| 15 |
|
15 | |||
| 16 | public class UnityConfigTest { |
|
16 | public class UnityConfigTest { | |
| 17 |
|
17 | |||
| 18 | [Fact] |
|
18 | [Fact] | |
| 19 | public void CreateContainer() { |
|
19 | public void CreateContainer() { | |
| 20 | var container = new UnityContainer(); |
|
20 | var container = new UnityContainer(); | |
| 21 |
|
21 | |||
| 22 | container.LoadXmlConfiguration(Path.Combine("data","container","empty.xml")); |
|
22 | container.LoadXmlConfiguration(Path.Combine("data","container","empty.xml")); | |
| 23 | } |
|
23 | } | |
| 24 |
|
24 | |||
| 25 | [Fact] |
|
25 | [Fact] | |
| 26 | public void SimpleServicesContainer() { |
|
26 | public void SimpleServicesContainer() { | |
| 27 | var container = new UnityContainer(); |
|
27 | var container = new UnityContainer(); | |
| 28 |
|
28 | |||
| 29 | container.LoadXmlConfiguration(Path.Combine("data","container","simple.services.xml")); |
|
29 | container.LoadXmlConfiguration(Path.Combine("data","container","simple.services.xml")); | |
| 30 |
|
30 | |||
| 31 | // named service registration |
|
31 | // named service registration | |
| 32 | var baz1 = container.Resolve<Baz>("Baz1"); |
|
32 | var baz1 = container.Resolve<Baz>("Baz1"); | |
| 33 |
|
33 | |||
| 34 | Assert.Equal(new [] {1,2,3}, baz1.Numbers); |
|
34 | Assert.Equal(new [] {1,2,3}, baz1.Numbers); | |
| 35 |
|
35 | |||
| 36 | // default service registration |
|
36 | // default service registration | |
| 37 | var baz = container.Resolve<Baz>(); |
|
37 | var baz = container.Resolve<Baz>(); | |
| 38 |
|
38 | |||
| 39 | Assert.Equal(new [] {2,5,5,1,3}, baz.Nuts.Select(x => x.Size)); |
|
39 | Assert.Equal(new [] {2,5,5,1,3}, baz.Nuts.Select(x => x.Size)); | |
| 40 |
|
40 | |||
| 41 | // ServiceFactory registered without a name |
|
41 | // ServiceFactory registered without a name | |
| 42 | // explicitly provides 'Baz2' service |
|
42 | // explicitly provides 'Baz2' service | |
| 43 | var baz2 = container.Resolve<Baz>("Baz2"); |
|
43 | var baz2 = container.Resolve<Baz>("Baz2"); | |
| 44 |
|
44 | |||
| 45 | // ServiceFactory registered with name 'Baz3' |
|
45 | // ServiceFactory registered with name 'Baz3' | |
| 46 | // provides by default the service registration with same name |
|
46 | // provides by default the service registration with same name | |
| 47 | var baz3 = container.Resolve<Baz>("Baz3"); |
|
47 | var baz3 = container.Resolve<Baz>("Baz3"); | |
| 48 |
|
48 | |||
| 49 | // This factory uses GuidGenerator registration |
|
49 | // This factory uses GuidGenerator registration | |
| 50 | // to generate a new id value |
|
50 | // to generate a new id value | |
| 51 | Assert.NotEqual(Guid.Empty, baz3.Id); |
|
51 | Assert.NotEqual(Guid.Empty, baz3.Id); | |
| 52 | } |
|
52 | } | |
| 53 |
|
53 | |||
| 54 | [Fact] |
|
54 | [Fact] | |
| 55 | public void GenericServicesContainer() { |
|
55 | public void GenericServicesContainer() { | |
| 56 | var container = new UnityContainer(); |
|
56 | var container = new UnityContainer(); | |
| 57 | container.LoadXmlConfiguration(Path.Combine("data","container","generic.services.xml")); |
|
57 | container.LoadXmlConfiguration(Path.Combine("data","container","generic.services.xml")); | |
| 58 |
|
58 | |||
| 59 | // resolve using a generig interface mapping |
|
59 | // resolve using a generig interface mapping | |
| 60 | var box = container.Resolve<IBox<Baz.Nut>>(); |
|
60 | var box = container.Resolve<IBox<Baz.Nut>>(); | |
| 61 |
|
61 | |||
| 62 | Assert.NotNull(box.GetBoxValue()); |
|
62 | Assert.NotNull(box.GetBoxValue()); | |
| 63 |
|
63 | |||
| 64 | // registered generic defines dependency of type {T} |
|
64 | // registered generic defines dependency of type {T} | |
| 65 | // in this case it should resolve to the default Nut with Size=2 |
|
65 | // in this case it should resolve to the default Nut with Size=2 | |
| 66 | Assert.Equal(box.GetBoxValue().Size,2); |
|
66 | Assert.Equal(box.GetBoxValue().Size,2); | |
| 67 |
|
67 | |||
| 68 | // generic factory which provides generic services |
|
68 | // generic factory which provides generic services | |
| 69 | var box2 = container.Resolve<IBox<Baz.Nut>>(); |
|
69 | var box2 = container.Resolve<IBox<Baz.Nut>>(); | |
| 70 |
|
70 | |||
| 71 | var set1 = container.Resolve<SetOfBoxes<Baz.Nut>>(); |
|
71 | var set1 = container.Resolve<SetOfBoxes<Baz.Nut>>(); | |
| 72 |
|
72 | |||
| 73 | Assert.Equal(new int?[] {null,2,1}, set1.Boxes?.Select(x => x.GetBoxValue()?.Size)); |
|
73 | Assert.Equal(new int?[] {null,2,1}, set1.Boxes?.Select(x => x.GetBoxValue()?.Size)); | |
| 74 | } |
|
74 | } | |
| 75 |
|
75 | |||
| 76 | [Fact] |
|
76 | [Fact] | |
| 77 | public void SerializedInstances() { |
|
77 | public void SerializedInstances() { | |
| 78 | var container = new UnityContainer(); |
|
78 | var container = new UnityContainer(); | |
| 79 | container.LoadXmlConfiguration(Path.Combine("data","container","serialized.instances.xml")); |
|
79 | container.LoadXmlConfiguration(Path.Combine("data","container","serialized.instances.xml")); | |
| 80 |
|
80 | |||
| 81 | var p1 = container.Resolve<Person>("p1"); |
|
81 | var p1 = container.Resolve<Person>("p1"); | |
| 82 | var p2 = container.Resolve<Person>("p2"); |
|
82 | var p2 = container.Resolve<Person>("p2"); | |
| 83 |
|
83 | |||
| 84 | Assert.Equal("Com", p1.FirstName); |
|
84 | Assert.Equal("Com", p1.FirstName); | |
| 85 | Assert.True(p1.AgeSpecified); |
|
85 | Assert.True(p1.AgeSpecified); | |
| 86 | Assert.Equal(99, p1.Age); |
|
86 | Assert.Equal(99, p1.Age); | |
| 87 |
|
87 | |||
| 88 | Assert.Equal("Javolta", p2.LastName); |
|
88 | Assert.Equal("Javolta", p2.LastName); | |
| 89 | Assert.True(p2.AgeSpecified); |
|
89 | Assert.True(p2.AgeSpecified); | |
| 90 | Assert.Equal(88, p2.Age); |
|
90 | Assert.Equal(88, p2.Age); | |
| 91 | } |
|
91 | } | |
| 92 |
|
92 | |||
|
|
93 | [Fact] | |||
|
|
94 | public void ArrayDependency() { | |||
|
|
95 | var container = new UnityContainer(); | |||
|
|
96 | ||||
|
|
97 | container.LoadXmlConfiguration(Path.Combine("data","container","array.dependency.xml")); | |||
|
|
98 | ||||
|
|
99 | // IEnumerable<T> and T[] are special cases: | |||
|
|
100 | // - T[] resolves to an array of all named registrations | |||
|
|
101 | // - IEnumerable<T> resolves to all registrations (including the default one) | |||
|
|
102 | // | |||
|
|
103 | // This rule works always regardless explicit registrations were provided | |||
|
|
104 | ||||
|
|
105 | var arr = container.Resolve<Baz.Nut[]>(); | |||
|
|
106 | Assert.Equal(new [] {5,3,1}, arr?.Select(x => x.Size)); | |||
|
|
107 | ||||
|
|
108 | var a1 = container.Resolve<Baz.Nut[]>("a1"); | |||
|
|
109 | Assert.Equal(new [] {5,3,1}, a1?.Select(x => x.Size)); | |||
|
|
110 | ||||
|
|
111 | var enm = container.Resolve<IEnumerable<Baz.Nut>>(); | |||
|
|
112 | Assert.Equal(new [] {5,3,1,2}, enm?.Select(x => x.Size)); | |||
|
|
113 | ||||
|
|
114 | // default service registration | |||
|
|
115 | var baz = container.Resolve<Baz>(); | |||
|
|
116 | ||||
|
|
117 | Assert.Equal(new [] {5,3,1}, baz.Nuts.Select(x => x.Size)); | |||
|
|
118 | ||||
|
|
119 | var baz2 = container.Resolve<Baz>("baz2"); | |||
|
|
120 | ||||
|
|
121 | Assert.Equal(new [] {2,5,5,1,3}, baz2.Nuts.Select(x => x.Size)); | |||
|
|
122 | } | |||
|
|
123 | ||||
| 93 | } |
|
124 | } | |
| 94 | } No newline at end of file |
|
125 | } | |
| @@ -1,36 +1,27 | |||||
| 1 | <Project Sdk="Microsoft.NET.Sdk"> |
|
1 | <Project Sdk="Microsoft.NET.Sdk"> | |
| 2 |
|
2 | |||
| 3 | <PropertyGroup> |
|
3 | <PropertyGroup> | |
| 4 | <Authors>Sergey Smirnov</Authors> |
|
4 | <Authors>Sergey Smirnov</Authors> | |
| 5 | <Title>Implab.ServiceHost</Title> |
|
5 | <Title>Implab.ServiceHost</Title> | |
| 6 | <Description>The configurable application host. |
|
6 | <Description>The configurable application host. | |
| 7 | Provides simple and flexible Xml configuration for UnityContainer. |
|
7 | Provides simple and flexible Xml configuration for UnityContainer. | |
| 8 | </Description> |
|
8 | </Description> | |
| 9 | <Copyright>2012-2021 Sergey Smirnov</Copyright> |
|
9 | <Copyright>2012-2021 Sergey Smirnov</Copyright> | |
| 10 | <Version>1.0.4</Version> |
|
10 | <Version>1.0.4</Version> | |
| 11 | <PackageLicenseUrl>https://code.implab.org/implab/ImplabNet/files/default/Implab/license.txt</PackageLicenseUrl> |
|
11 | <PackageLicenseUrl>https://code.implab.org/implab/ImplabNet/files/default/Implab/license.txt</PackageLicenseUrl> | |
| 12 | <PackageProjectUrl>https://code.implab.org/implab/ImplabNet/</PackageProjectUrl> |
|
12 | <PackageProjectUrl>https://code.implab.org/implab/ImplabNet/</PackageProjectUrl> | |
| 13 | <RepositoryUrl>https://code.implab.org/implab/ImplabNet/</RepositoryUrl> |
|
13 | <RepositoryUrl>https://code.implab.org/implab/ImplabNet/</RepositoryUrl> | |
| 14 | <RepositoryType>mercurial</RepositoryType> |
|
14 | <RepositoryType>mercurial</RepositoryType> | |
| 15 | <PackageTags>Implab;Xml configuration;IoC;Unity container</PackageTags> |
|
15 | <PackageTags>Implab;Xml configuration;IoC;Unity container</PackageTags> | |
| 16 | </PropertyGroup> |
|
16 | <TargetFrameworks>netcoreapp3.1;net46</TargetFrameworks> | |
| 17 |
|
||||
| 18 |
|
||||
| 19 | <PropertyGroup Condition="'$(OSTYPE)'=='linux'"> |
|
|||
| 20 | <TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks> |
|
|||
| 21 | <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46'">/usr/lib/mono/4.5/</FrameworkPathOverride> |
|
|||
| 22 | </PropertyGroup> |
|
|||
| 23 |
|
||||
| 24 | <PropertyGroup Condition="'$(OSTYPE)'=='windows'"> |
|
|||
| 25 | <TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks> |
|
|||
| 26 | </PropertyGroup> |
|
17 | </PropertyGroup> | |
| 27 |
|
18 | |||
| 28 | <ItemGroup> |
|
19 | <ItemGroup> | |
| 29 | <PackageReference Include="Unity" Version="5.8.6" /> |
|
20 | <PackageReference Include="Unity" Version="5.8.6" /> | |
| 30 | </ItemGroup> |
|
21 | </ItemGroup> | |
| 31 |
|
22 | |||
| 32 | <ItemGroup> |
|
23 | <ItemGroup> | |
| 33 | <ProjectReference Include="..\Implab\Implab.csproj" /> |
|
24 | <ProjectReference Include="..\Implab\Implab.csproj" /> | |
| 34 | </ItemGroup> |
|
25 | </ItemGroup> | |
| 35 |
|
26 | |||
| 36 | </Project> |
|
27 | </Project> | |
General Comments 0
You need to be logged in to leave comments.
Login now
