moved SmallInjector to new Project.
This commit is contained in:
parent
e4ce2a472a
commit
8954cd82fc
@ -12,7 +12,5 @@ namespace InjectorTest
|
|||||||
var actual = Helper.WriteMethodString(new TestClock(), "");
|
var actual = Helper.WriteMethodString(new TestClock(), "");
|
||||||
Assert.Equal(expected, actual);
|
Assert.Equal(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
|
||||||
|
@ -1,45 +1,28 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjector
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A small dependency injector to demonstrate the pattern.
|
/// A small dependency injector to demonstrate the pattern.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SmallInjector
|
public class Container : IContainer
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Register types in the resolve container.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TService">Type of the service.</typeparam>
|
|
||||||
/// <typeparam name="TInterface">Type of the interface of the service.</typeparam>
|
|
||||||
/// <param name="isSingleton">True if the service should be singleton. False otherwise.</param>
|
|
||||||
/// <param name="instance">instance of the service</param>
|
|
||||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
||||||
{
|
{
|
||||||
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
|
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Register types in the resolve container.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TService">Type of the service.</typeparam>
|
|
||||||
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
|
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Resolve service of specified type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TService">Type of the service.</typeparam>
|
|
||||||
/// <returns>A instance of the service.</returns>
|
|
||||||
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Resolve service of specified type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceInterface">Type of the service.</param>
|
|
||||||
/// <returns>A instance of the service.</returns>
|
|
||||||
public object Resolve(Type serviceInterface)
|
public object Resolve(Type serviceInterface)
|
||||||
{
|
{
|
||||||
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
34
SmallInjector/IContainer.cs
Normal file
34
SmallInjector/IContainer.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SmallInjector
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// DI Container
|
||||||
|
/// </summary>
|
||||||
|
public interface IContainer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Register types in the resolve container.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||||
|
/// <typeparam name="TInterface">Type of the interface of the service.</typeparam>
|
||||||
|
/// <param name="isSingleton">True if the service should be singleton. False otherwise.</param>
|
||||||
|
/// <param name="instance">instance of the service</param>
|
||||||
|
void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register types in the resolve container.
|
||||||
|
/// </summary>
|
||||||
|
void RegisterType<TService>(bool isSingleton, TService instance = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve service of specified type.
|
||||||
|
/// </summary>
|
||||||
|
object Resolve(Type serviceInterface);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve service of specified type.
|
||||||
|
/// </summary>
|
||||||
|
TService Resolve<TService>();
|
||||||
|
}
|
||||||
|
}
|
15
SmallInjector/SmallInjector.csproj
Normal file
15
SmallInjector/SmallInjector.csproj
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<DocumentationFile>C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<DocumentationFile>C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallI
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -41,6 +43,18 @@ Global
|
|||||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.Build.0 = Release|Any CPU
|
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.ActiveCfg = Release|Any CPU
|
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.Build.0 = Release|Any CPU
|
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -12,11 +12,6 @@ namespace SmallInjectorDemo
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Helper
|
public static class Helper
|
||||||
{
|
{
|
||||||
private const byte ForwardSlashByte = (byte)'/';
|
|
||||||
private const byte DashByte = (byte)'-';
|
|
||||||
private const byte PlusByte = (byte)'+';
|
|
||||||
private const byte UnderscoreByte = (byte)'_';
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Encode Base64 string
|
/// Encode Base64 string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -28,16 +23,6 @@ namespace SmallInjectorDemo
|
|||||||
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
||||||
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
||||||
|
|
||||||
// replace any characters which are not URL safe
|
|
||||||
for (var i = 0; i < 22; i++)
|
|
||||||
{
|
|
||||||
if (encodedBytes[i] == ForwardSlashByte)
|
|
||||||
encodedBytes[i] = DashByte;
|
|
||||||
|
|
||||||
if (encodedBytes[i] == PlusByte)
|
|
||||||
encodedBytes[i] = UnderscoreByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip the last two bytes as these will be '==' padding
|
// skip the last two bytes as these will be '==' padding
|
||||||
var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22));
|
var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22));
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using SmallInjector;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
@ -7,21 +8,22 @@ namespace SmallInjectorDemo
|
|||||||
private static void Main()
|
private static void Main()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Small dependency injection example");
|
Console.WriteLine("Small dependency injection example");
|
||||||
var injector = new SmallInjector();
|
IContainer container = new Container();
|
||||||
|
container.RegisterType(true, container);
|
||||||
Console.WriteLine("Register " + nameof(SystemClock));
|
Console.WriteLine("Register " + nameof(SystemClock));
|
||||||
injector.RegisterType<SystemClock, IClock>(true);
|
container.RegisterType<SystemClock, IClock>(true);
|
||||||
Console.WriteLine("Register " + nameof(ServiceOne));
|
Console.WriteLine("Register " + nameof(ServiceOne));
|
||||||
injector.RegisterType<ServiceOne, IServiceOne>(true);
|
container.RegisterType<ServiceOne, IServiceOne>(true);
|
||||||
Console.WriteLine("Register " + nameof(ServiceTwo));
|
Console.WriteLine("Register " + nameof(ServiceTwo));
|
||||||
injector.RegisterType<ServiceTwo, IServiceTwo>(true);
|
container.RegisterType<ServiceTwo, IServiceTwo>(true);
|
||||||
Console.WriteLine("Register " + nameof(ServiceConsumer));
|
Console.WriteLine("Register " + nameof(ServiceConsumer));
|
||||||
injector.RegisterType<ServiceConsumer>(false);
|
container.RegisterType<ServiceConsumer>(false);
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("Resolve class instances:");
|
Console.WriteLine("Resolve class instances:");
|
||||||
var useful1 = injector.Resolve<ServiceConsumer>();
|
var useful1 = container.Resolve<ServiceConsumer>();
|
||||||
var useful2 = injector.Resolve<ServiceConsumer>();
|
var useful2 = container.Resolve<ServiceConsumer>();
|
||||||
var useful3 = injector.Resolve<ServiceConsumer>();
|
var useful3 = container.Resolve<ServiceConsumer>();
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("Run test methods:");
|
Console.WriteLine("Run test methods:");
|
||||||
useful1.TestTheServices();
|
useful1.TestTheServices();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
||||||
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
@ -12,4 +12,7 @@
|
|||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
<DocumentationFile>bin\Release\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
|
<DocumentationFile>bin\Release\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SmallInjector\SmallInjector.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user