static async Task Main(string[] args) { var services = new DiServiceCollection(); //services.RegisterSingleton<RandomGuidGenerator>(); //services.RegisterTransient<RandomGuidGenerator>(); services.RegisterTransient <ISomeService, SomeServiceOne>(); services.RegisterSingleton <IRandomGuidProvider, RandomGuidProvider>(); services.RegisterSingleton <MainApp>(); var container = services.GenerateContainer(); var serviceFirst = container.GetService <ISomeService>(); var serviceSecond = container.GetService <ISomeService>(); var mainApp = container.GetService <MainApp>(); serviceFirst.PrintSomething(); serviceSecond.PrintSomething(); //await mainApp.StartAsync(); }
static void Main(string[] args) { var services = new DiServiceCollection(); // services.RegisterSingleton<RandomGuidGenerator>(); // services.RegisterTransient<RandomGuidGenerator>(); services.RegisterSingleton <IRandomGuidProvider, RandomGuidProvider>(); services.RegisterTransient <ISomeService, SomeService1>(); // services.RegisterSingleton(new RandomGuidGenerator()); var container = services.GenerateContainer(); // var service = container.GetService<RandomGuidGenerator>(); // var service1 = container.GetService<RandomGuidGenerator>(); // Console.WriteLine(service.RandomGuid); // Console.WriteLine(service1.RandomGuid); var service = container.GetService <ISomeService>(); var service2 = container.GetService <ISomeService>(); service.PrintSomething(); service2.PrintSomething(); }
static void Main(string[] args) { IPrinter printer = new ConsolePrinter(); var services = new DiServiceCollection(); services.RegisterTransient <RandomGuidGenerator>(); services.RegisterSingleton <ISomeExternalService, SomeExternalServiceOne>(); services.RegisterSingleton <IRandomGuidProvider, RandomGuidProvider>(); var container = services.GenerateContainer(); var randomGuidGeneratorServiceFirst = container.GetService <RandomGuidGenerator>(); var randomGuidGeneratorServiceSecond = container.GetService <RandomGuidGenerator>(); var someServiceFirst = container.GetService <ISomeExternalService>(); var someServiceSecond = container.GetService <ISomeExternalService>(); printer.WriteLine("Only service registered transient:"); printer.WriteLine(randomGuidGeneratorServiceFirst.RandomGuid.ToString()); printer.WriteLine(randomGuidGeneratorServiceSecond.RandomGuid.ToString()); printer.WriteLine("\nService and implementation registered singleton:"); printer.WriteLine(someServiceFirst.PrintSomething()); printer.WriteLine(someServiceSecond.PrintSomething()); printer.ReadLine(); }
static async Task Main(string[] args) { var services = new DiServiceCollection(); services.RegisterTransient <IIlluminationStrategy, FlatShading>(); services.RegisterTransient <ICameraProvider, CameraProvider>(); services.RegisterTransient <ILightProvider, LightProvider>(); services.RegisterTransient <IRayProvider, RayProvider>(); services.RegisterTransient <IRayTracer, MultiThreadRayTracer>(); services.RegisterTransient <ISceneProvider, SceneProvider>(); services.RegisterSingleton <IApp, App>(); var container = services.GenerateContainer(); var app = container.GetService <IApp>(); await Parser.Default.ParseArguments <CommandLineArgs>(args) .WithParsedAsync(async arguments => { try { await app.Run(arguments.FileName, arguments.Output); } catch (Exception e) { Console.WriteLine(e.Message); } }); }
static void DependencyInjectionDemo() { var services = new DiServiceCollection(); //services.RegisterSingleton<RandomGuidGenerator>(); //services.RegisterTransient<RandomGuidGenerator>(); services.RegisterSingleton <ISomeService, SomeServiceOne>(); services.RegisterTransient <IRandomGuidProvider, RandomGuidProvider>(); var container = services.GenerateContainer(); var service1 = container.GetService <ISomeService>(); var service2 = container.GetService <ISomeService>(); service1.PrintSomething(); service2.PrintSomething(); }
static void Main(string[] args) { var services = new DiServiceCollection(); services.RegisterSingleton <ISomeService, SomeService>(); services.RegisterTransient <IRandomGuidProvider, RandomGuidProvider>(); services.RegisterSingleton <IPrinter, ConsolePrinter>(); var container = services.GenerateContainer(); var serviceFirst = container.GetService <ISomeService>(); var serviceSecond = container.GetService <ISomeService>(); serviceFirst.Print(); serviceSecond.Print(); }