moved SmallInjector to new Project.

This commit is contained in:
Holger Börchers 2019-07-20 22:58:53 +02:00
parent e4ce2a472a
commit 8954cd82fc
9 changed files with 84 additions and 50 deletions

View File

@ -12,7 +12,5 @@ namespace InjectorTest
var actual = Helper.WriteMethodString(new TestClock(), "");
Assert.Equal(expected, actual);
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>

View File

@ -1,45 +1,28 @@
using System;
using System.Collections.Generic;
namespace SmallInjectorDemo
namespace SmallInjector
{
/// <summary>
/// A small dependency injector to demonstrate the pattern.
/// </summary>
public class SmallInjector
public class Container : IContainer
{
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
/// <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>
/// <inheritdoc/>
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
{
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
}
/// <summary>
/// Register types in the resolve container.
/// </summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <inheritdoc/>
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
/// <summary>
/// Resolve service of specified type.
/// </summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <returns>A instance of the service.</returns>
/// <inheritdoc/>
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
/// <summary>
/// Resolve service of specified type.
/// </summary>
/// <param name="serviceInterface">Type of the service.</param>
/// <returns>A instance of the service.</returns>
/// <inheritdoc/>
public object Resolve(Type serviceInterface)
{
if (!_container.TryGetValue(serviceInterface, out var registeredType))

View 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>();
}
}

View 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>

View File

@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallI
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
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|x86.ActiveCfg = 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
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -12,11 +12,6 @@ namespace SmallInjectorDemo
/// </summary>
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>
/// Encode Base64 string
/// </summary>
@ -28,16 +23,6 @@ namespace SmallInjectorDemo
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
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
var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22));

View File

@ -1,4 +1,5 @@
using System;
using SmallInjector;
namespace SmallInjectorDemo
{
@ -7,21 +8,22 @@ namespace SmallInjectorDemo
private static void Main()
{
Console.WriteLine("Small dependency injection example");
var injector = new SmallInjector();
IContainer container = new Container();
container.RegisterType(true, container);
Console.WriteLine("Register " + nameof(SystemClock));
injector.RegisterType<SystemClock, IClock>(true);
container.RegisterType<SystemClock, IClock>(true);
Console.WriteLine("Register " + nameof(ServiceOne));
injector.RegisterType<ServiceOne, IServiceOne>(true);
container.RegisterType<ServiceOne, IServiceOne>(true);
Console.WriteLine("Register " + nameof(ServiceTwo));
injector.RegisterType<ServiceTwo, IServiceTwo>(true);
container.RegisterType<ServiceTwo, IServiceTwo>(true);
Console.WriteLine("Register " + nameof(ServiceConsumer));
injector.RegisterType<ServiceConsumer>(false);
container.RegisterType<ServiceConsumer>(false);
Console.WriteLine();
Console.WriteLine("Resolve class instances:");
var useful1 = injector.Resolve<ServiceConsumer>();
var useful2 = injector.Resolve<ServiceConsumer>();
var useful3 = injector.Resolve<ServiceConsumer>();
var useful1 = container.Resolve<ServiceConsumer>();
var useful2 = container.Resolve<ServiceConsumer>();
var useful3 = container.Resolve<ServiceConsumer>();
Console.WriteLine();
Console.WriteLine("Run test methods:");
useful1.TestTheServices();

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>SmallInjectorDemo</AssemblyName>
<RootNamespace>SmallInjectorDemo</RootNamespace>
<LangVersion>latest</LangVersion>
@ -12,4 +12,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SmallInjector\SmallInjector.csproj" />
</ItemGroup>
</Project>