code cleanup

This commit is contained in:
Holger Börchers 2022-11-13 14:32:56 +01:00
parent 9f4e76fa4c
commit 32cc67b25a
7 changed files with 56 additions and 118 deletions

2
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "build", "preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path. // 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": [], "args": [],
"cwd": "${workspaceFolder}/SmallInjectorDemo", "cwd": "${workspaceFolder}/SmallInjectorDemo",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
namespace SmallInjector namespace SmallInjector

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
namespace SmallInjector
{
/// <summary>
/// Wrapper for Service Locator
/// </summary>
public class ServiceLocatorWrapper : CommonServiceLocator.ServiceLocatorImplBase
{
private readonly IContainer _container;
/// <summary>
/// Creates new <see cref="ServiceLocatorWrapper"/> instance
/// </summary>
public ServiceLocatorWrapper(IContainer container)
{
_container = container;
}
/// <inheritdoc />
protected override object DoGetInstance(Type serviceType, string key) => _container.Resolve(serviceType);
/// <inheritdoc />
protected override IEnumerable<object> DoGetAllInstances(Type serviceType) => _container.ResolveAny(serviceType);
}
}

View File

@ -2,18 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\debug\SmallInjector.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\release\SmallInjector.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="2.0.4" />
</ItemGroup>
</Project> </Project>

View File

@ -1,18 +1,9 @@
using System; using SmallInjector;
using CommonServiceLocator; using SmallInjectorDemo;
using SmallInjector;
using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Interfaces;
using SmallInjectorDemo.Items; 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"); Console.WriteLine("Small dependency injection example");
container.RegisterType(true, container); container.RegisterType(true, container);
@ -38,9 +29,9 @@ namespace SmallInjectorDemo
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Resolve class instances:"); Console.WriteLine("Resolve class instances:");
var useful1 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful1 = container.Resolve<ServiceConsumer>();
var useful2 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful2 = container.Resolve<ServiceConsumer>();
var useful3 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful3 = container.Resolve<ServiceConsumer>();
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Run test methods:"); Console.WriteLine("Run test methods:");
useful1.TestTheServices(); useful1.TestTheServices();
@ -48,7 +39,3 @@ namespace SmallInjectorDemo
useful2.TestTheServices(); useful2.TestTheServices();
Console.WriteLine(); Console.WriteLine();
useful3.TestTheServices(); useful3.TestTheServices();
//Console.ReadLine();
}
}
}

View File

@ -1,8 +1,8 @@
using System; using System;
using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo;
{
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />. /// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />.
@ -25,4 +25,3 @@ namespace SmallInjectorDemo
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id); public override string ToString() => Helper.WriteMethodString(_clock, _id);
} }
}

View File

@ -1,20 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<AssemblyName>SmallInjectorDemo</AssemblyName> <AssemblyName>SmallInjectorDemo</AssemblyName>
<RootNamespace>SmallInjectorDemo</RootNamespace> <RootNamespace>SmallInjectorDemo</RootNamespace>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="2.0.4" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SmallInjector\SmallInjector.csproj" /> <ProjectReference Include="..\SmallInjector\SmallInjector.csproj" />
</ItemGroup> </ItemGroup>