Program.cs
95 lines
| 2.6 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Playground / Program.cs
|
|
r267 | using System; | ||
|
|
r268 | using System.Diagnostics; | ||
|
|
r272 | using System.Linq; | ||
|
|
r268 | using Implab.Diagnostics; | ||
|
|
r267 | using Implab.ServiceHost.Unity; | ||
|
|
r229 | using Implab.Xml; | ||
|
|
r267 | using Unity; | ||
| using Unity.Injection; | ||||
|
|
r272 | using Unity.Registration; | ||
|
|
r229 | |||
| namespace Implab.Playground { | ||||
|
|
r267 | |||
| public class Foo { | ||||
|
|
r269 | |||
| public class Bar { | ||||
| } | ||||
|
|
r270 | public string Name { get; set; } | ||
|
|
r267 | public int IntValue { get; set; } | ||
| public string StringValue { get; set; } | ||||
| } | ||||
|
|
r272 | public interface IContainer<T> { | ||
| T Instance { get; set; } | ||||
| } | ||||
| public class Container<T> : IContainer<T> { | ||||
|
|
r267 | public Container() { | ||
| } | ||||
| public Container(T instance) { | ||||
| Instance = instance; | ||||
| } | ||||
| public T Instance { get; set; } | ||||
|
|
r270 | |||
| public void SetInstance(T value) { | ||||
| Instance = value; | ||||
| } | ||||
|
|
r267 | } | ||
|
|
r255 | |||
|
|
r229 | public class Program { | ||
|
|
r255 | static void Main(string[] args) { | ||
|
|
r273 | var stopwatch = new Stopwatch(); | ||
| stopwatch.Start(); | ||||
|
|
r255 | |||
|
|
r274 | var ctx = new ContainerBuilder(); | ||
|
|
r273 | |||
| Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); | ||||
| ctx.LoadConfig("data/sample.xml"); | ||||
|
|
r271 | |||
|
|
r273 | Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); | ||
| var container = ctx.Container; | ||||
|
|
r233 | |||
|
|
r273 | var instace1 = container.Resolve<IContainer<string>>(); | ||
| Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}"); | ||||
| var instace2 = container.Resolve<IContainer<Foo>>(); | ||||
| Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}"); | ||||
|
|
r271 | |||
|
|
r272 | DisplayContainerRegistrations(container); | ||
| } | ||||
|
|
r268 | |||
|
|
r272 | static void DisplayContainerRegistrations(IUnityContainer theContainer) { | ||
| string regName, regType, mapTo, lifetime; | ||||
| Console.WriteLine("Container has {0} Registrations:", | ||||
| theContainer.Registrations.Count()); | ||||
| foreach (ContainerRegistration item in theContainer.Registrations) { | ||||
| regType = item.RegisteredType.FullName; | ||||
| mapTo = item.MappedToType.FullName; | ||||
| regName = item.Name ?? "[default]"; | ||||
| lifetime = item.LifetimeManager.LifetimeType.Name; | ||||
| if (mapTo != regType) { | ||||
| mapTo = " -> " + mapTo; | ||||
| } else { | ||||
| mapTo = string.Empty; | ||||
| } | ||||
| lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length); | ||||
| Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime); | ||||
| } | ||||
|
|
r233 | } | ||
|
|
r229 | |||
| } | ||||
| } | ||||
