From 32cc67b25ab007afd5b7e907655c65734e3ecc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Sun, 13 Nov 2022 14:32:56 +0100 Subject: [PATCH] code cleanup --- .vscode/launch.json | 2 +- SmallInjector/Container.cs | 2 - SmallInjector/ServiceLocatorWrapper.cs | 27 -------- SmallInjector/SmallInjector.csproj | 13 +--- SmallInjectorDemo/Program.cs | 81 +++++++++------------- SmallInjectorDemo/ServiceTwo.cs | 37 +++++----- SmallInjectorDemo/SmallInjectorDemo.csproj | 12 +--- 7 files changed, 56 insertions(+), 118 deletions(-) delete mode 100644 SmallInjector/ServiceLocatorWrapper.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 3291fe9..13694a8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/SmallInjectorDemo/bin/Debug/netcoreapp2.2/SmallInjectorDemo.dll", + "program": "${workspaceFolder}/SmallInjectorDemo/bin/Debug/net7.0/SmallInjectorDemo.dll", "args": [], "cwd": "${workspaceFolder}/SmallInjectorDemo", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console diff --git a/SmallInjector/Container.cs b/SmallInjector/Container.cs index 7f5205a..770f27a 100644 --- a/SmallInjector/Container.cs +++ b/SmallInjector/Container.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; namespace SmallInjector diff --git a/SmallInjector/ServiceLocatorWrapper.cs b/SmallInjector/ServiceLocatorWrapper.cs deleted file mode 100644 index fb6b830..0000000 --- a/SmallInjector/ServiceLocatorWrapper.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace SmallInjector -{ - /// - /// Wrapper for Service Locator - /// - public class ServiceLocatorWrapper : CommonServiceLocator.ServiceLocatorImplBase - { - private readonly IContainer _container; - - /// - /// Creates new instance - /// - public ServiceLocatorWrapper(IContainer container) - { - _container = container; - } - - /// - protected override object DoGetInstance(Type serviceType, string key) => _container.Resolve(serviceType); - - /// - protected override IEnumerable DoGetAllInstances(Type serviceType) => _container.ResolveAny(serviceType); - } -} \ No newline at end of file diff --git a/SmallInjector/SmallInjector.csproj b/SmallInjector/SmallInjector.csproj index 2d42b55..1a6f299 100644 --- a/SmallInjector/SmallInjector.csproj +++ b/SmallInjector/SmallInjector.csproj @@ -2,18 +2,7 @@ netstandard2.0 + latest - - bin\debug\SmallInjector.xml - - - - bin\release\SmallInjector.xml - - - - - - diff --git a/SmallInjectorDemo/Program.cs b/SmallInjectorDemo/Program.cs index 2fe6ea4..1bb1424 100644 --- a/SmallInjectorDemo/Program.cs +++ b/SmallInjectorDemo/Program.cs @@ -1,54 +1,41 @@ -using System; -using CommonServiceLocator; -using SmallInjector; +using SmallInjector; +using SmallInjectorDemo; using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Items; -namespace SmallInjectorDemo -{ - internal static class Program - { - private static void Main() - { +IContainer container = new Container(); - 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(true); - Console.WriteLine("Register " + nameof(ServiceOne)); - container.RegisterType(true); - Console.WriteLine("Register " + nameof(ServiceTwo)); - container.RegisterType(true); - Console.WriteLine("Register " + nameof(ServiceConsumer)); - container.RegisterType(false); - container.RegisterType(true); - container.RegisterType(true); - container.RegisterType(true); +Console.WriteLine("Small dependency injection example"); +container.RegisterType(true, container); +Console.WriteLine("Register " + nameof(SystemClock)); +container.RegisterType(true); +Console.WriteLine("Register " + nameof(ServiceOne)); +container.RegisterType(true); +Console.WriteLine("Register " + nameof(ServiceTwo)); +container.RegisterType(true); +Console.WriteLine("Register " + nameof(ServiceConsumer)); +container.RegisterType(false); +container.RegisterType(true); +container.RegisterType(true); +container.RegisterType(true); - Console.WriteLine(); - Console.WriteLine("Check registrations:"); - Console.WriteLine(nameof(IClock).PadRight(20) + container.IsRegistered()); - Console.WriteLine(nameof(IServiceOne).PadRight(20) + container.IsRegistered()); - Console.WriteLine(nameof(IServiceTwo).PadRight(20) + container.IsRegistered()); - Console.WriteLine(nameof(ServiceConsumer).PadRight(20) + container.IsRegistered()); +Console.WriteLine(); +Console.WriteLine("Check registrations:"); +Console.WriteLine(nameof(IClock).PadRight(20) + container.IsRegistered()); +Console.WriteLine(nameof(IServiceOne).PadRight(20) + container.IsRegistered()); +Console.WriteLine(nameof(IServiceTwo).PadRight(20) + container.IsRegistered()); +Console.WriteLine(nameof(ServiceConsumer).PadRight(20) + container.IsRegistered()); - Console.WriteLine(); - Console.WriteLine("Resolve class instances:"); - var useful1 = ServiceLocator.Current.GetInstance(); - var useful2 = ServiceLocator.Current.GetInstance(); - var useful3 = ServiceLocator.Current.GetInstance(); - Console.WriteLine(); - Console.WriteLine("Run test methods:"); - useful1.TestTheServices(); - Console.WriteLine(); - useful2.TestTheServices(); - Console.WriteLine(); - useful3.TestTheServices(); - //Console.ReadLine(); - } - } -} \ No newline at end of file +Console.WriteLine(); +Console.WriteLine("Resolve class instances:"); +var useful1 = container.Resolve(); +var useful2 = container.Resolve(); +var useful3 = container.Resolve(); +Console.WriteLine(); +Console.WriteLine("Run test methods:"); +useful1.TestTheServices(); +Console.WriteLine(); +useful2.TestTheServices(); +Console.WriteLine(); +useful3.TestTheServices(); \ No newline at end of file diff --git a/SmallInjectorDemo/ServiceTwo.cs b/SmallInjectorDemo/ServiceTwo.cs index 24c5587..081c6a8 100644 --- a/SmallInjectorDemo/ServiceTwo.cs +++ b/SmallInjectorDemo/ServiceTwo.cs @@ -1,28 +1,27 @@ using System; using SmallInjectorDemo.Interfaces; -namespace SmallInjectorDemo +namespace SmallInjectorDemo; + +/// +/// +/// Implementation of . +/// +public class ServiceTwo : IServiceTwo { - /// + private readonly IClock _clock; + private readonly string _id; + /// - /// Implementation of . + /// Creates a new instance of . /// - public class ServiceTwo : IServiceTwo + public ServiceTwo(IClock clock) { - private readonly IClock _clock; - private readonly string _id; - - /// - /// Creates a new instance of . - /// - public ServiceTwo(IClock clock) - { - _clock = clock; - _id = Guid.NewGuid().EncodeBase64String(); - Console.WriteLine(Helper.WriteMethodString(clock, _id)); - } - - /// - public override string ToString() => Helper.WriteMethodString(_clock, _id); + _clock = clock; + _id = Guid.NewGuid().EncodeBase64String(); + Console.WriteLine(Helper.WriteMethodString(clock, _id)); } + + /// + public override string ToString() => Helper.WriteMethodString(_clock, _id); } \ No newline at end of file diff --git a/SmallInjectorDemo/SmallInjectorDemo.csproj b/SmallInjectorDemo/SmallInjectorDemo.csproj index 9c116b3..b829db8 100644 --- a/SmallInjectorDemo/SmallInjectorDemo.csproj +++ b/SmallInjectorDemo/SmallInjectorDemo.csproj @@ -1,20 +1,12 @@  Exe - netcoreapp2.1 + net7.0 SmallInjectorDemo SmallInjectorDemo latest + enable - - bin\Debug\netcoreapp2.1\SmallInjectorDemo.xml - - - bin\Release\netcoreapp2.1\SmallInjectorDemo.xml - - - -