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