Mercurial > pub > ImplabNet
annotate Implab.Playground/Program.cs @ 281:e0916ddc9950 v3 tip
code cleanup and refactoring
author | cin |
---|---|
date | Fri, 01 Jun 2018 21:35:24 +0300 |
parents | f07be402ab02 |
children |
rev | line source |
---|---|
267 | 1 using System; |
277 | 2 using System.Collections.Generic; |
268 | 3 using System.Diagnostics; |
272 | 4 using System.Linq; |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
5 using Implab.Components; |
268 | 6 using Implab.Diagnostics; |
267 | 7 using Implab.ServiceHost.Unity; |
229 | 8 using Implab.Xml; |
267 | 9 using Unity; |
10 using Unity.Injection; | |
272 | 11 using Unity.Registration; |
229 | 12 |
13 namespace Implab.Playground { | |
267 | 14 |
15 public class Foo { | |
269 | 16 |
17 public class Bar { | |
18 | |
19 } | |
20 | |
270 | 21 public string Name { get; set; } |
22 | |
267 | 23 public int IntValue { get; set; } |
24 | |
25 public string StringValue { get; set; } | |
26 | |
277 | 27 public void AddRange(Foo[] items) { |
28 Console.WriteLine($"AddRange: Foo[]"); | |
29 } | |
30 | |
267 | 31 } |
32 | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
33 public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
34 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
35 public bool UseSsl { get; set; } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
36 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
37 public string Connection { get; set; } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
38 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
39 public Foo Create() { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
40 return new Foo() { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
41 Name = "AutoFac" |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
42 }; |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
43 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
44 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
45 Foo.Bar IFactory<Foo.Bar>.Create() { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
46 return new Foo.Bar(); |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
47 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
48 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
49 |
272 | 50 public interface IContainer<T> { |
51 T Instance { get; set; } | |
52 } | |
53 | |
54 public class Container<T> : IContainer<T> { | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
55 public class Bar { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
56 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
57 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
58 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
59 public class Bar<T2> { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
60 public class Baz { |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
61 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
62 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
63 |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
64 } |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
65 |
267 | 66 public Container() { |
67 | |
68 } | |
69 | |
70 public Container(T instance) { | |
71 Instance = instance; | |
72 } | |
73 | |
74 public T Instance { get; set; } | |
270 | 75 |
76 public void SetInstance(T value) { | |
77 Instance = value; | |
78 } | |
277 | 79 |
80 public void AddRange(List<T> items) { | |
81 Console.WriteLine($"AddRange: {typeof(List<T>)}"); | |
82 } | |
83 | |
84 public void AddRange(T[] items) { | |
85 Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}"); | |
86 } | |
267 | 87 } |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
88 |
229 | 89 public class Program { |
90 | |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
91 static void Main(string[] args) { |
280 | 92 var u1 = new Uri("/some/one"); |
93 | |
94 Console.WriteLine($"{u1.IsAbsoluteUri}: {u1}"); | |
95 | |
96 var u2 = new Uri(u1, "../../two"); | |
97 | |
98 Console.WriteLine($"{u2.IsAbsoluteUri}: {u2}"); | |
99 | |
100 } | |
101 | |
102 static void Main2(string[] args) { | |
277 | 103 var listener = new SimpleTraceListener(Console.Out); |
104 var source = Trace<TypeResolver>.TraceSource; | |
105 source.Switch.Level = SourceLevels.All; | |
106 source.Listeners.Add(listener); | |
107 | |
279 | 108 var stopwatch = new Stopwatch(); |
109 stopwatch.Start(); | |
110 | |
111 var container = new UnityContainer(); | |
112 | |
113 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); | |
114 stopwatch.Restart(); | |
115 | |
116 container.LoadXmlConfiguration("data/sample.xml"); | |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
117 |
279 | 118 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
272
diff
changeset
|
119 |
279 | 120 stopwatch.Restart(); |
121 var instace1 = container.Resolve<IContainer<string>>(); | |
122 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}"); | |
277 | 123 |
279 | 124 stopwatch.Restart(); |
125 var instace2 = container.Resolve<IContainer<Foo>>(); | |
126 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}"); | |
127 | |
128 DisplayContainerRegistrations(container); | |
272 | 129 } |
268 | 130 |
272 | 131 static void DisplayContainerRegistrations(IUnityContainer theContainer) { |
132 string regName, regType, mapTo, lifetime; | |
133 Console.WriteLine("Container has {0} Registrations:", | |
134 theContainer.Registrations.Count()); | |
135 foreach (ContainerRegistration item in theContainer.Registrations) { | |
136 regType = item.RegisteredType.FullName; | |
137 mapTo = item.MappedToType.FullName; | |
138 regName = item.Name ?? "[default]"; | |
139 lifetime = item.LifetimeManager.LifetimeType.Name; | |
140 if (mapTo != regType) { | |
141 mapTo = " -> " + mapTo; | |
142 } else { | |
143 mapTo = string.Empty; | |
144 } | |
145 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length); | |
146 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime); | |
147 } | |
233 | 148 } |
229 | 149 |
150 | |
151 } | |
152 } |