comparison Implab.Playground/Program.cs @ 272:9d1cca834b05 v3

preview version of Unity xml configuration
author cin
date Thu, 26 Apr 2018 03:14:54 +0300
parents d4d437ec4483
children 79110a16cab7
comparison
equal deleted inserted replaced
271:d4d437ec4483 272:9d1cca834b05
1 using System; 1 using System;
2 using System.Diagnostics; 2 using System.Diagnostics;
3 using System.Linq;
3 using Implab.Diagnostics; 4 using Implab.Diagnostics;
4 using Implab.ServiceHost.Unity; 5 using Implab.ServiceHost.Unity;
5 using Implab.Xml; 6 using Implab.Xml;
6 using Unity; 7 using Unity;
7 using Unity.Injection; 8 using Unity.Injection;
9 using Unity.Registration;
8 10
9 namespace Implab.Playground { 11 namespace Implab.Playground {
10 12
11 public class Foo { 13 public class Foo {
12 14
20 22
21 public string StringValue { get; set; } 23 public string StringValue { get; set; }
22 24
23 } 25 }
24 26
25 public class Container<T> { 27 public interface IContainer<T> {
28 T Instance { get; set; }
29 }
30
31 public class Container<T> : IContainer<T> {
26 public Container() { 32 public Container() {
27 33
28 } 34 }
29 35
30 public Container(T instance) { 36 public Container(T instance) {
47 53
48 var conf = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml"); 54 var conf = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml");
49 55
50 ctx.Visit(conf); 56 ctx.Visit(conf);
51 57
52 Console.WriteLine($"Registrations: {conf.Items.Count}"); 58 DisplayContainerRegistrations(container);
53 59
60 var instace1 = container.Resolve<IContainer<string>>();
61 var instace2 = container.Resolve<IContainer<Foo>>();
62
63 }
64
65 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
66 string regName, regType, mapTo, lifetime;
67 Console.WriteLine("Container has {0} Registrations:",
68 theContainer.Registrations.Count());
69 foreach (ContainerRegistration item in theContainer.Registrations) {
70 regType = item.RegisteredType.FullName;
71 mapTo = item.MappedToType.FullName;
72 regName = item.Name ?? "[default]";
73 lifetime = item.LifetimeManager.LifetimeType.Name;
74 if (mapTo != regType) {
75 mapTo = " -> " + mapTo;
76 } else {
77 mapTo = string.Empty;
78 }
79 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
80 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime);
81 }
54 } 82 }
55 83
56 84
57 } 85 }
58 } 86 }