comparison Implab.Playground/Program.cs @ 278:6691aff01de1 v3

Implab: added XmlDefaultSeializer (SerializersPool is now obsolete) Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author cin
date Thu, 03 May 2018 09:59:44 +0300
parents 963b17c275be
children 8714471e8d78
comparison
equal deleted inserted replaced
277:963b17c275be 278:6691aff01de1
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Diagnostics; 3 using System.Diagnostics;
4 using System.Linq; 4 using System.Linq;
5 using Implab.Components;
5 using Implab.Diagnostics; 6 using Implab.Diagnostics;
6 using Implab.ServiceHost.Unity; 7 using Implab.ServiceHost.Unity;
7 using Implab.Xml; 8 using Implab.Xml;
8 using Unity; 9 using Unity;
9 using Unity.Injection; 10 using Unity.Injection;
27 Console.WriteLine($"AddRange: Foo[]"); 28 Console.WriteLine($"AddRange: Foo[]");
28 } 29 }
29 30
30 } 31 }
31 32
33 public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> {
34
35 public bool UseSsl { get; set; }
36
37 public string Connection { get; set; }
38
39 public Foo Create() {
40 return new Foo() {
41 Name = "AutoFac"
42 };
43 }
44
45 Foo.Bar IFactory<Foo.Bar>.Create() {
46 return new Foo.Bar();
47 }
48 }
49
32 public interface IContainer<T> { 50 public interface IContainer<T> {
33 T Instance { get; set; } 51 T Instance { get; set; }
34 } 52 }
35 53
36 public class Container<T> : IContainer<T> { 54 public class Container<T> : IContainer<T> {
55 public class Bar {
56
57 }
58
59 public class Bar<T2> {
60 public class Baz {
61
62 }
63
64 }
65
37 public Container() { 66 public Container() {
38 67
39 } 68 }
40 69
41 public Container(T instance) { 70 public Container(T instance) {
63 var listener = new SimpleTraceListener(Console.Out); 92 var listener = new SimpleTraceListener(Console.Out);
64 var source = Trace<TypeResolver>.TraceSource; 93 var source = Trace<TypeResolver>.TraceSource;
65 source.Switch.Level = SourceLevels.All; 94 source.Switch.Level = SourceLevels.All;
66 source.Listeners.Add(listener); 95 source.Listeners.Add(listener);
67 96
68 var stopwatch = new Stopwatch(); 97 var resolver = new TypeResolver();
69 stopwatch.Start(); 98 resolver.AddNamespace("System");
99 resolver.AddNamespace("System.Collections.Generic");
100 resolver.AddNamespace("Implab.Playground");
101 resolver.AddMapping("string", typeof(string));
102 resolver.AddMapping("listOf`1", typeof(List<>));
70 103
71 var ctx = new ContainerBuilder(); 104 var spec = TypeReference.Parse("Container{listOf{string}}+Bar{List{string}}");
72 105
73 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); 106 var t = resolver.Resolve(spec, true);
74 stopwatch.Restart();
75
76 ctx.LoadConfig("data/sample.xml");
77 107
78 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); 108 Console.WriteLine("{0}", t);
79 109 Console.WriteLine("Spec: {0}", spec);
80 var container = ctx.Container; 110 Console.WriteLine("IsGenericType: {0}", t.IsGenericType);
81 111 Console.WriteLine("IsGenericTypeDefinition: {0}", t.IsGenericTypeDefinition);
82 stopwatch.Restart(); 112 Console.WriteLine("ContainsGenericParameters: {0}", t.ContainsGenericParameters);
83 var instace1 = container.Resolve<IContainer<string>>();
84 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}");
85
86 stopwatch.Restart();
87 var instace2 = container.Resolve<IContainer<Foo>>();
88 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}");
89
90 DisplayContainerRegistrations(container);
91 } 113 }
92 114
93 static void DisplayContainerRegistrations(IUnityContainer theContainer) { 115 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
94 string regName, regType, mapTo, lifetime; 116 string regName, regType, mapTo, lifetime;
95 Console.WriteLine("Container has {0} Registrations:", 117 Console.WriteLine("Container has {0} Registrations:",