56
playground/SmallInjectorDemo.sln
Normal file
56
playground/SmallInjectorDemo.sln
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.4.33103.184
|
||||
MinimumVisualStudioVersion = 15.0.26124.0
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo\SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{97F8F459-C4B4-4C40-A4CA-2443A9C245D9}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
.csharpierrc = .csharpierrc
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjector", "..\src\SmallInjector\SmallInjector.csproj", "{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x64.Build.0 = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.Build.0 = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|x64.Build.0 = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{41F6E1B4-C322-4743-9F9E-8A5C57EBA509}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {1A146B09-F1B0-4A1E-B396-739788B6A68E}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
44
playground/SmallInjectorDemo/Helper.cs
Normal file
44
playground/SmallInjectorDemo/Helper.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
using System.Buffers.Text;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <summary>
|
||||
/// Static helper class for generating random numbers.
|
||||
/// </summary>
|
||||
public static class Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Encode Base64 string
|
||||
/// </summary>
|
||||
public static string EncodeBase64String(this Guid guid)
|
||||
{
|
||||
Span<byte> guidBytes = stackalloc byte[16];
|
||||
Span<byte> encodedBytes = stackalloc byte[24];
|
||||
|
||||
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
||||
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
||||
|
||||
// skip the last two bytes as these will be '==' padding
|
||||
var final = Encoding.UTF8.GetString(encodedBytes[..22]);
|
||||
|
||||
return final;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write method string.
|
||||
/// </summary>
|
||||
/// <param name="clock">The current clock</param>
|
||||
/// <param name="id">id of the object</param>
|
||||
/// <param name="memberName">member name</param>
|
||||
/// <param name="filepath">file path</param>
|
||||
/// <returns></returns>
|
||||
public static string WriteMethodString(IClock clock, string id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
|
||||
{
|
||||
var classname = Path.GetFileNameWithoutExtension(filepath);
|
||||
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
||||
}
|
||||
}
|
12
playground/SmallInjectorDemo/Interfaces/IClock.cs
Normal file
12
playground/SmallInjectorDemo/Interfaces/IClock.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace SmallInjectorDemo.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface of the clock.
|
||||
/// </summary>
|
||||
public interface IClock
|
||||
{
|
||||
/// <summary>
|
||||
/// Current Date-Time.
|
||||
/// </summary>
|
||||
string CurrentDateTime { get; }
|
||||
}
|
6
playground/SmallInjectorDemo/Interfaces/IModule.cs
Normal file
6
playground/SmallInjectorDemo/Interfaces/IModule.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace SmallInjectorDemo.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IModule" />
|
||||
/// </summary>
|
||||
public interface IModule { }
|
6
playground/SmallInjectorDemo/Interfaces/IServiceOne.cs
Normal file
6
playground/SmallInjectorDemo/Interfaces/IServiceOne.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace SmallInjectorDemo.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for service one.
|
||||
/// </summary>
|
||||
public interface IServiceOne { }
|
6
playground/SmallInjectorDemo/Interfaces/IServiceTwo.cs
Normal file
6
playground/SmallInjectorDemo/Interfaces/IServiceTwo.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace SmallInjectorDemo.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for service two.
|
||||
/// </summary>
|
||||
public interface IServiceTwo { }
|
8
playground/SmallInjectorDemo/Items/ModuleA.cs
Normal file
8
playground/SmallInjectorDemo/Items/ModuleA.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo.Items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="ModuleA" />
|
||||
/// </summary>
|
||||
public class ModuleA : IModule { }
|
8
playground/SmallInjectorDemo/Items/ModuleB.cs
Normal file
8
playground/SmallInjectorDemo/Items/ModuleB.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo.Items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="ModuleB" />
|
||||
/// </summary>
|
||||
public class ModuleB : IModule { }
|
8
playground/SmallInjectorDemo/Items/ModuleC.cs
Normal file
8
playground/SmallInjectorDemo/Items/ModuleC.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo.Items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="ModuleC" />
|
||||
/// </summary>
|
||||
public class ModuleC : IModule { }
|
40
playground/SmallInjectorDemo/Program.cs
Normal file
40
playground/SmallInjectorDemo/Program.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using SmallInjector;
|
||||
using SmallInjectorDemo;
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
using SmallInjectorDemo.Items;
|
||||
|
||||
IContainer container = new 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 = 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();
|
42
playground/SmallInjectorDemo/ServiceConsumer.cs
Normal file
42
playground/SmallInjectorDemo/ServiceConsumer.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <summary>
|
||||
/// A very useful class.
|
||||
/// </summary>
|
||||
public class ServiceConsumer
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly IServiceOne _service1;
|
||||
private readonly IServiceTwo _service2;
|
||||
private readonly string _id;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceConsumer"/>.
|
||||
/// </summary>
|
||||
/// <param name="clock">The current clock</param>
|
||||
/// <param name="service1">injected service one.</param>
|
||||
/// <param name="service2">injected service two.</param>
|
||||
public ServiceConsumer(IClock clock, IServiceOne service1, IServiceTwo service2)
|
||||
{
|
||||
_clock = clock;
|
||||
_service1 = service1;
|
||||
_service2 = service2;
|
||||
_id = Guid.NewGuid().EncodeBase64String();
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the injected services.
|
||||
/// </summary>
|
||||
public void TestTheServices()
|
||||
{
|
||||
Console.WriteLine(ToString());
|
||||
Console.WriteLine(_service1.ToString());
|
||||
Console.WriteLine(_service2.ToString());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
}
|
26
playground/SmallInjectorDemo/ServiceOne.cs
Normal file
26
playground/SmallInjectorDemo/ServiceOne.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
|
||||
/// </summary>
|
||||
public class ServiceOne : IServiceOne
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly string _id;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceOne"/>.
|
||||
/// </summary>
|
||||
public ServiceOne(IClock clock)
|
||||
{
|
||||
_clock = clock;
|
||||
_id = Guid.NewGuid().EncodeBase64String();
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
}
|
26
playground/SmallInjectorDemo/ServiceTwo.cs
Normal file
26
playground/SmallInjectorDemo/ServiceTwo.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />.
|
||||
/// </summary>
|
||||
public class ServiceTwo : IServiceTwo
|
||||
{
|
||||
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);
|
||||
}
|
13
playground/SmallInjectorDemo/SmallInjectorDemo.csproj
Normal file
13
playground/SmallInjectorDemo/SmallInjectorDemo.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
||||
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\SmallInjector\SmallInjector.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
10
playground/SmallInjectorDemo/SystemClock.cs
Normal file
10
playground/SmallInjectorDemo/SystemClock.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using SmallInjectorDemo.Interfaces;
|
||||
|
||||
namespace SmallInjectorDemo;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class SystemClock : IClock
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string CurrentDateTime => DateTime.Now.ToString("O");
|
||||
}
|
Reference in New Issue
Block a user