54 lines
2.3 KiB
C#
54 lines
2.3 KiB
C#
using System;
|
|
using CommonServiceLocator;
|
|
using SmallInjector;
|
|
using SmallInjectorDemo.Interfaces;
|
|
using SmallInjectorDemo.Items;
|
|
|
|
namespace SmallInjectorDemo
|
|
{
|
|
internal static class Program
|
|
{
|
|
private static void Main()
|
|
{
|
|
|
|
IContainer container = new Container();
|
|
ServiceLocator.SetLocatorProvider(() => new ServiceLocatorWrapper(container));
|
|
|
|
Console.WriteLine("Small dependency injection example");
|
|
container.RegisterType(true, container);
|
|
Console.WriteLine("Register " + nameof(SystemClock));
|
|
container.RegisterType<SystemClock, IClock>(true);
|
|
Console.WriteLine("Register " + nameof(ServiceOne));
|
|
container.RegisterType<ServiceOne, IServiceOne>(true);
|
|
Console.WriteLine("Register " + nameof(ServiceTwo));
|
|
container.RegisterType<ServiceTwo, IServiceTwo>(true);
|
|
Console.WriteLine("Register " + nameof(ServiceConsumer));
|
|
container.RegisterType<ServiceConsumer>(false);
|
|
container.RegisterType<ModuleA, IModule>(true);
|
|
container.RegisterType<ModuleB, IModule>(true);
|
|
container.RegisterType<ModuleC, IModule>(true);
|
|
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("Check registrations:");
|
|
Console.WriteLine(nameof(IClock).PadRight(20) + container.IsRegistered<IClock>());
|
|
Console.WriteLine(nameof(IServiceOne).PadRight(20) + container.IsRegistered<IServiceOne>());
|
|
Console.WriteLine(nameof(IServiceTwo).PadRight(20) + container.IsRegistered<IServiceTwo>());
|
|
Console.WriteLine(nameof(ServiceConsumer).PadRight(20) + container.IsRegistered<ServiceConsumer>());
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("Resolve class instances:");
|
|
var useful1 = ServiceLocator.Current.GetInstance<ServiceConsumer>();
|
|
var useful2 = ServiceLocator.Current.GetInstance<ServiceConsumer>();
|
|
var useful3 = ServiceLocator.Current.GetInstance<ServiceConsumer>();
|
|
Console.WriteLine();
|
|
Console.WriteLine("Run test methods:");
|
|
useful1.TestTheServices();
|
|
Console.WriteLine();
|
|
useful2.TestTheServices();
|
|
Console.WriteLine();
|
|
useful3.TestTheServices();
|
|
//Console.ReadLine();
|
|
}
|
|
}
|
|
} |