code cleanup
This commit is contained in:
parent
9f4e76fa4c
commit
32cc67b25a
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -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
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace SmallInjector
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -2,18 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</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>
|
||||
|
@ -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<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("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("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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Resolve class instances:");
|
||||
var useful1 = container.Resolve<ServiceConsumer>();
|
||||
var useful2 = container.Resolve<ServiceConsumer>();
|
||||
var useful3 = container.Resolve<ServiceConsumer>();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Run test methods:");
|
||||
useful1.TestTheServices();
|
||||
Console.WriteLine();
|
||||
useful2.TestTheServices();
|
||||
Console.WriteLine();
|
||||
useful3.TestTheServices();
|
@ -1,28 +1,27 @@
|
||||
using System;
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />.
|
||||
/// </summary>
|
||||
public class ServiceTwo : IServiceTwo
|
||||
{
|
||||
/// <inheritdoc />
|
||||
private readonly IClock _clock;
|
||||
private readonly string _id;
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />.
|
||||
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
||||
/// </summary>
|
||||
public class ServiceTwo : IServiceTwo
|
||||
public ServiceTwo(IClock clock)
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly string _id;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
||||
/// </summary>
|
||||
public ServiceTwo(IClock clock)
|
||||
{
|
||||
_clock = clock;
|
||||
_id = Guid.NewGuid().EncodeBase64String();
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
_clock = clock;
|
||||
_id = Guid.NewGuid().EncodeBase64String();
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
}
|
@ -1,20 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
||||
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</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>
|
||||
<ProjectReference Include="..\SmallInjector\SmallInjector.csproj" />
|
||||
</ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user