Mercurial > pub > ImplabNet
annotate Implab.Playground/Program.cs @ 274:22629bf26121 v3
Unity xml configuration, alpha2
author | cin |
---|---|
date | Fri, 27 Apr 2018 04:47:52 +0300 |
parents | 79110a16cab7 |
children | 963b17c275be |
rev | line source |
---|---|
267 | 1 using System; |
268 | 2 using System.Diagnostics; |
272 | 3 using System.Linq; |
268 | 4 using Implab.Diagnostics; |
267 | 5 using Implab.ServiceHost.Unity; |
229 | 6 using Implab.Xml; |
267 | 7 using Unity; |
8 using Unity.Injection; | |
272 | 9 using Unity.Registration; |
229 | 10 |
11 namespace Implab.Playground { | |
267 | 12 |
13 public class Foo { | |
269 | 14 |
15 public class Bar { | |
16 | |
17 } | |
18 | |
270 | 19 public string Name { get; set; } |
20 | |
267 | 21 public int IntValue { get; set; } |
22 | |
23 public string StringValue { get; set; } | |
24 | |
25 } | |
26 | |
272 | 27 public interface IContainer<T> { |
28 T Instance { get; set; } | |
29 } | |
30 | |
31 public class Container<T> : IContainer<T> { | |
267 | 32 public Container() { |
33 | |
34 } | |
35 | |
36 public Container(T instance) { | |
37 Instance = instance; | |
38 } | |
39 | |
40 public T Instance { get; set; } | |
270 | 41 |
42 public void SetInstance(T value) { | |
43 Instance = value; | |
44 } | |
267 | 45 } |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
46 |
229 | 47 public class Program { |
48 | |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
49 static void Main(string[] args) { |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
50 var stopwatch = new Stopwatch(); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
51 stopwatch.Start(); |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
52 |
274 | 53 var ctx = new ContainerBuilder(); |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
54 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
55 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
56 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
57 ctx.LoadConfig("data/sample.xml"); |
271 | 58 |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
59 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
60 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
61 var container = ctx.Container; |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
62 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
63 |
233 | 64 |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
65 var instace1 = container.Resolve<IContainer<string>>(); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
66 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
67 var instace2 = container.Resolve<IContainer<Foo>>(); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
68 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
69 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}"); |
271 | 70 |
272 | 71 DisplayContainerRegistrations(container); |
72 } | |
268 | 73 |
272 | 74 static void DisplayContainerRegistrations(IUnityContainer theContainer) { |
75 string regName, regType, mapTo, lifetime; | |
76 Console.WriteLine("Container has {0} Registrations:", | |
77 theContainer.Registrations.Count()); | |
78 foreach (ContainerRegistration item in theContainer.Registrations) { | |
79 regType = item.RegisteredType.FullName; | |
80 mapTo = item.MappedToType.FullName; | |
81 regName = item.Name ?? "[default]"; | |
82 lifetime = item.LifetimeManager.LifetimeType.Name; | |
83 if (mapTo != regType) { | |
84 mapTo = " -> " + mapTo; | |
85 } else { | |
86 mapTo = string.Empty; | |
87 } | |
88 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length); | |
89 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime); | |
90 } | |
233 | 91 } |
229 | 92 |
93 | |
94 } | |
95 } |