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