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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
1 using System;
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
2 using System.Collections.Generic;
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
3 using System.Diagnostics;
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
4 using System.Linq;
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
5 using Implab.Diagnostics;
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
6 using Implab.ServiceHost.Unity;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
7 using Implab.Xml;
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
8 using Unity;
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
9 using Unity.Injection;
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
10 using Unity.Registration;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
11
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
12 namespace Implab.Playground {
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
13
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
14 public class Foo {
269
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
15
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
16 public class Bar {
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
17
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
18 }
ff581cff7003 Working on Unity container xml configuration
cin
parents: 268
diff changeset
19
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
20 public string Name { get; set; }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
21
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
22 public int IntValue { get; set; }
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
23
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
24 public string StringValue { get; set; }
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
25
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
26 public void AddRange(Foo[] items) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
27 Console.WriteLine($"AddRange: Foo[]");
963b17c275be Refactoring
cin
parents: 274
diff changeset
28 }
963b17c275be Refactoring
cin
parents: 274
diff changeset
29
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
30 }
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
31
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
32 public interface IContainer<T> {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
33 T Instance { get; set; }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
34 }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
35
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
36 public class Container<T> : IContainer<T> {
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
37 public Container() {
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
38
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
39 }
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
40
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
41 public Container(T instance) {
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
42 Instance = instance;
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
43 }
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
44
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
45 public T Instance { get; set; }
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
46
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
47 public void SetInstance(T value) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
48 Instance = value;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents: 269
diff changeset
49 }
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
50
963b17c275be Refactoring
cin
parents: 274
diff changeset
51 public void AddRange(List<T> items) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
52 Console.WriteLine($"AddRange: {typeof(List<T>)}");
963b17c275be Refactoring
cin
parents: 274
diff changeset
53 }
963b17c275be Refactoring
cin
parents: 274
diff changeset
54
963b17c275be Refactoring
cin
parents: 274
diff changeset
55 public void AddRange(T[] items) {
963b17c275be Refactoring
cin
parents: 274
diff changeset
56 Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}");
963b17c275be Refactoring
cin
parents: 274
diff changeset
57 }
267
6b3e5c48131b Working on Unity xml configuration
cin
parents: 260
diff changeset
58 }
255
b00441e04738 Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents: 236
diff changeset
59
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60 public class Program {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
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
963b17c275be Refactoring
cin
parents: 274
diff changeset
63 var listener = new SimpleTraceListener(Console.Out);
963b17c275be Refactoring
cin
parents: 274
diff changeset
64 var source = Trace<TypeResolver>.TraceSource;
963b17c275be Refactoring
cin
parents: 274
diff changeset
65 source.Switch.Level = SourceLevels.All;
963b17c275be Refactoring
cin
parents: 274
diff changeset
66 source.Listeners.Add(listener);
963b17c275be Refactoring
cin
parents: 274
diff changeset
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
22629bf26121 Unity xml configuration, alpha2
cin
parents: 273
diff changeset
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
963b17c275be Refactoring
cin
parents: 274
diff changeset
74 stopwatch.Restart();
963b17c275be Refactoring
cin
parents: 274
diff changeset
75
273
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents: 272
diff changeset
76 ctx.LoadConfig("data/sample.xml");
271
d4d437ec4483 Working on Unity xml configuration
cin
parents: 270
diff changeset
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
963b17c275be Refactoring
cin
parents: 274
diff changeset
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
963b17c275be Refactoring
cin
parents: 274
diff changeset
85
963b17c275be Refactoring
cin
parents: 274
diff changeset
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
d4d437ec4483 Working on Unity xml configuration
cin
parents: 270
diff changeset
89
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
90 DisplayContainerRegistrations(container);
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
91 }
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
92
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
93 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
94 string regName, regType, mapTo, lifetime;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
95 Console.WriteLine("Container has {0} Registrations:",
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
96 theContainer.Registrations.Count());
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
97 foreach (ContainerRegistration item in theContainer.Registrations) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
98 regType = item.RegisteredType.FullName;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
99 mapTo = item.MappedToType.FullName;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
100 regName = item.Name ?? "[default]";
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
101 lifetime = item.LifetimeManager.LifetimeType.Name;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
102 if (mapTo != regType) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
103 mapTo = " -> " + mapTo;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
104 } else {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
105 mapTo = string.Empty;
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
106 }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
107 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
108 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime);
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 271
diff changeset
109 }
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 229
diff changeset
110 }
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
111
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
112
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
113 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
114 }