Mercurial > pub > ImplabNet
annotate 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 |
| rev | line source |
|---|---|
| 267 | 1 using System; |
| 268 | 2 using System.Diagnostics; |
| 272 | 3 using System.Linq; |
| 268 | 4 using Implab.Diagnostics; |
| 267 | 5 using Implab.ServiceHost.Unity; |
| 229 | 6 using Implab.Xml; |
| 267 | 7 using Unity; |
| 8 using Unity.Injection; | |
| 272 | 9 using Unity.Registration; |
| 229 | 10 |
| 11 namespace Implab.Playground { | |
| 267 | 12 |
| 13 public class Foo { | |
| 269 | 14 |
| 15 public class Bar { | |
| 16 | |
| 17 } | |
| 18 | |
| 270 | 19 public string Name { get; set; } |
| 20 | |
| 267 | 21 public int IntValue { get; set; } |
| 22 | |
| 23 public string StringValue { get; set; } | |
| 24 | |
| 25 } | |
| 26 | |
| 272 | 27 public interface IContainer<T> { |
| 28 T Instance { get; set; } | |
| 29 } | |
| 30 | |
| 31 public class Container<T> : IContainer<T> { | |
| 267 | 32 public Container() { |
| 33 | |
| 34 } | |
| 35 | |
| 36 public Container(T instance) { | |
| 37 Instance = instance; | |
| 38 } | |
| 39 | |
| 40 public T Instance { get; set; } | |
| 270 | 41 |
| 42 public void SetInstance(T value) { | |
| 43 Instance = value; | |
| 44 } | |
| 267 | 45 } |
|
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
46 |
| 229 | 47 public class Program { |
| 48 | |
|
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
49 static void Main(string[] args) { |
| 267 | 50 var container = new UnityContainer(); |
|
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
236
diff
changeset
|
51 |
| 271 | 52 var ctx = new ConfigurationContext(container); |
| 53 | |
| 269 | 54 var conf = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml"); |
| 233 | 55 |
| 271 | 56 ctx.Visit(conf); |
| 57 | |
| 272 | 58 DisplayContainerRegistrations(container); |
| 59 | |
| 60 var instace1 = container.Resolve<IContainer<string>>(); | |
| 61 var instace2 = container.Resolve<IContainer<Foo>>(); | |
| 62 | |
| 63 } | |
| 268 | 64 |
| 272 | 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 } | |
| 233 | 82 } |
| 229 | 83 |
| 84 | |
| 85 } | |
| 86 } |
