Code cleanup and enforce csharpier
This commit is contained in:
parent
32cc67b25a
commit
8884d55380
6
.csharpierrc
Normal file
6
.csharpierrc
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 200,
|
||||||
|
"useTabs": false,
|
||||||
|
"tabWidth": 4,
|
||||||
|
"preprocessorSymbolSets": ["", "DEBUG", "DEBUG,CODE_STYLE"]
|
||||||
|
}
|
3
.filenesting.json
Normal file
3
.filenesting.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"help":"https://go.microsoft.com/fwlink/?linkid=866610"
|
||||||
|
}
|
@ -1,98 +1,90 @@
|
|||||||
using System;
|
namespace SmallInjector;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace SmallInjector
|
/// <summary>
|
||||||
|
/// A small dependency injector to demonstrate the pattern.
|
||||||
|
/// </summary>
|
||||||
|
public class Container : IContainer
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly Dictionary<Type, List<RegisteredType>> _container = new();
|
||||||
/// A small dependency injector to demonstrate the pattern.
|
|
||||||
/// </summary>
|
/// <inheritdoc/>
|
||||||
public class Container : IContainer
|
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Type, List<RegisteredType>> _container = new Dictionary<Type, List<RegisteredType>>();
|
if (!IsRegistered<TInterface>())
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default)
|
|
||||||
where TService : TInterface
|
|
||||||
{
|
{
|
||||||
if (!IsRegistered<TInterface>())
|
_container[typeof(TInterface)] = new List<RegisteredType> { new RegisteredType(typeof(TService), isSingleton, null) };
|
||||||
{
|
|
||||||
_container[typeof(TInterface)] = new List<RegisteredType>
|
|
||||||
{new RegisteredType(typeof(TService), isSingleton, null)};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_container[typeof(TInterface)].Add(new RegisteredType(typeof(TService), isSingleton, null));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
/// <inheritdoc/>
|
|
||||||
public void RegisterType<TService>(bool isSingleton, TService instance = default) =>
|
|
||||||
RegisterType<TService, TService>(isSingleton, instance);
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public TService Resolve<TService>() => (TService) Resolve(typeof(TService));
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsRegistered<TService>() => IsRegistered(typeof(TService));
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsRegistered(Type serviceType) => _container.ContainsKey(serviceType);
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IEnumerable<object> ResolveAny(Type serviceType)
|
|
||||||
{
|
{
|
||||||
if (!_container.TryGetValue(serviceType, out var registeredTypes))
|
_container[typeof(TInterface)].Add(new RegisteredType(typeof(TService), isSingleton, null));
|
||||||
throw new Exception($"Dependency {serviceType.FullName} is not registered");
|
|
||||||
foreach (var registeredType in registeredTypes)
|
|
||||||
{
|
|
||||||
if (registeredType.IsSingleton && registeredType.Instance != null)
|
|
||||||
{
|
|
||||||
yield return registeredType.Instance;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var constructor = registeredType.ServiceType.GetConstructors()[0];
|
|
||||||
var parameters = constructor.GetParameters();
|
|
||||||
var constructorParameters = new object[parameters.Length];
|
|
||||||
for (var i = 0; i < parameters.Length; i++)
|
|
||||||
{
|
|
||||||
var parameterType = parameters[i].ParameterType;
|
|
||||||
if (typeof(Array).IsAssignableFrom(parameterType))
|
|
||||||
{
|
|
||||||
constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var instance = constructor.Invoke(constructorParameters);
|
|
||||||
yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IEnumerable<object> ResolveAny<TService>() => ResolveAny(typeof(TService));
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public object Resolve(Type serviceType) => ResolveAny(serviceType).First();
|
|
||||||
|
|
||||||
private class RegisteredType
|
|
||||||
{
|
|
||||||
public bool IsSingleton { get; }
|
|
||||||
public Type ServiceType { get; }
|
|
||||||
public object Instance { get; set; }
|
|
||||||
|
|
||||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
|
||||||
{
|
|
||||||
ServiceType = serviceType;
|
|
||||||
IsSingleton = isSingleton;
|
|
||||||
Instance = instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => ServiceType.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsRegistered<TService>() => IsRegistered(typeof(TService));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsRegistered(Type serviceType) => _container.ContainsKey(serviceType);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<object> ResolveAny(Type serviceType)
|
||||||
|
{
|
||||||
|
if (!_container.TryGetValue(serviceType, out var registeredTypes))
|
||||||
|
throw new Exception($"Dependency {serviceType.FullName} is not registered");
|
||||||
|
foreach (var registeredType in registeredTypes)
|
||||||
|
{
|
||||||
|
if (registeredType.IsSingleton && registeredType.Instance != null)
|
||||||
|
{
|
||||||
|
yield return registeredType.Instance;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var constructor = registeredType.ServiceType.GetConstructors()[0];
|
||||||
|
var parameters = constructor.GetParameters();
|
||||||
|
var constructorParameters = new object[parameters.Length];
|
||||||
|
for (var i = 0; i < parameters.Length; i++)
|
||||||
|
{
|
||||||
|
var parameterType = parameters[i].ParameterType;
|
||||||
|
if (typeof(Array).IsAssignableFrom(parameterType))
|
||||||
|
{
|
||||||
|
constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var instance = constructor.Invoke(constructorParameters);
|
||||||
|
yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<object> ResolveAny<TService>() => ResolveAny(typeof(TService));
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public object Resolve(Type serviceType) => ResolveAny(serviceType).First();
|
||||||
|
|
||||||
|
private class RegisteredType
|
||||||
|
{
|
||||||
|
public bool IsSingleton { get; }
|
||||||
|
public Type ServiceType { get; }
|
||||||
|
public object Instance { get; set; }
|
||||||
|
|
||||||
|
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||||
|
{
|
||||||
|
ServiceType = serviceType;
|
||||||
|
IsSingleton = isSingleton;
|
||||||
|
Instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => ServiceType.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,55 +1,51 @@
|
|||||||
using System;
|
namespace SmallInjector;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace SmallInjector
|
/// <summary>
|
||||||
|
/// DI Container
|
||||||
|
/// </summary>
|
||||||
|
public interface IContainer
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DI Container
|
/// Register types in the resolve container.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IContainer
|
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||||
{
|
/// <typeparam name="TInterface">Type of the interface of the service.</typeparam>
|
||||||
/// <summary>
|
/// <param name="isSingleton">True if the service should be singleton. False otherwise.</param>
|
||||||
/// Register types in the resolve container.
|
/// <param name="instance">instance of the service</param>
|
||||||
/// </summary>
|
void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface;
|
||||||
/// <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>
|
/// <summary>
|
||||||
/// Register types in the resolve container.
|
/// Register types in the resolve container.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void RegisterType<TService>(bool isSingleton, TService instance = default);
|
void RegisterType<TService>(bool isSingleton, TService instance = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolve service of specified type.
|
/// Resolve service of specified type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
object Resolve(Type serviceType);
|
object Resolve(Type serviceType);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolve service of specified type.
|
/// Resolve service of specified type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TService Resolve<TService>();
|
TService Resolve<TService>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true, if the service is registered. False otherwise.
|
/// Returns true, if the service is registered. False otherwise.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool IsRegistered<TService>();
|
bool IsRegistered<TService>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true, if the service is registered. False otherwise.
|
/// Returns true, if the service is registered. False otherwise.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool IsRegistered(Type serviceType);
|
bool IsRegistered(Type serviceType);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolve any implementation
|
/// Resolve any implementation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IEnumerable<object> ResolveAny(Type serviceType);
|
IEnumerable<object> ResolveAny(Type serviceType);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolve any implementation
|
/// Resolve any implementation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IEnumerable<object> ResolveAny<TService>();
|
IEnumerable<object> ResolveAny<TService>();
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,8 +1,9 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 16
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 16.0.29123.88
|
VisualStudioVersion = 17.4.33103.184
|
||||||
MinimumVisualStudioVersion = 15.0.26124.0
|
MinimumVisualStudioVersion = 15.0.26124.0
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo\SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo\SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{97F8F459-C4B4-4C40-A4CA-2443A9C245D9}"
|
||||||
|
ProjectSection(SolutionItems) = preProject
|
||||||
|
.csharpierrc = .csharpierrc
|
||||||
|
EndProjectSection
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -1,47 +1,44 @@
|
|||||||
using System;
|
|
||||||
using System.Buffers.Text;
|
using System.Buffers.Text;
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjectorDemo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Static helper class for generating random numbers.
|
||||||
|
/// </summary>
|
||||||
|
public static class Helper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Static helper class for generating random numbers.
|
/// Encode Base64 string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Helper
|
public static string EncodeBase64String(this Guid guid)
|
||||||
{
|
{
|
||||||
/// <summary>
|
Span<byte> guidBytes = stackalloc byte[16];
|
||||||
/// Encode Base64 string
|
Span<byte> encodedBytes = stackalloc byte[24];
|
||||||
/// </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
|
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
||||||
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
||||||
|
|
||||||
// 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));
|
||||||
|
|
||||||
return final;
|
return final;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write method string.
|
/// Write method string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clock">The current clock</param>
|
/// <param name="clock">The current clock</param>
|
||||||
/// <param name="id">id of the object</param>
|
/// <param name="id">id of the object</param>
|
||||||
/// <param name="memberName">member name</param>
|
/// <param name="memberName">member name</param>
|
||||||
/// <param name="filepath">file path</param>
|
/// <param name="filepath">file path</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string WriteMethodString(IClock clock, string id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
|
public static string WriteMethodString(IClock clock, string id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
|
||||||
{
|
{
|
||||||
var classname = Path.GetFileNameWithoutExtension(filepath);
|
var classname = Path.GetFileNameWithoutExtension(filepath);
|
||||||
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +1,12 @@
|
|||||||
namespace SmallInjectorDemo.Interfaces
|
namespace SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface of the clock.
|
||||||
|
/// </summary>
|
||||||
|
public interface IClock
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface of the clock.
|
/// Current Date-Time.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IClock
|
string CurrentDateTime { get; }
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Current Date-Time.
|
|
||||||
/// </summary>
|
|
||||||
string CurrentDateTime { get; }
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,9 +1,6 @@
|
|||||||
namespace SmallInjectorDemo.Interfaces
|
namespace SmallInjectorDemo.Interfaces;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the <see cref="IModule" />
|
/// Defines the <see cref="IModule" />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IModule
|
public interface IModule { }
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
namespace SmallInjectorDemo.Interfaces
|
namespace SmallInjectorDemo.Interfaces;
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Interface for service one.
|
|
||||||
/// </summary>
|
|
||||||
public interface IServiceOne
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
/// <summary>
|
||||||
}
|
/// Interface for service one.
|
||||||
|
/// </summary>
|
||||||
|
public interface IServiceOne { }
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
namespace SmallInjectorDemo.Interfaces
|
namespace SmallInjectorDemo.Interfaces;
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Interface for service two.
|
|
||||||
/// </summary>
|
|
||||||
public interface IServiceTwo
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
/// <summary>
|
||||||
}
|
/// Interface for service two.
|
||||||
|
/// </summary>
|
||||||
|
public interface IServiceTwo { }
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo.Items
|
namespace SmallInjectorDemo.Items;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the <see cref="ModuleA" />
|
/// Defines the <see cref="ModuleA" />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ModuleA : IModule
|
public class ModuleA : IModule { }
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo.Items
|
namespace SmallInjectorDemo.Items;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the <see cref="ModuleB" />
|
/// Defines the <see cref="ModuleB" />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ModuleB : IModule
|
public class ModuleB : IModule { }
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo.Items
|
namespace SmallInjectorDemo.Items;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the <see cref="ModuleC" />
|
/// Defines the <see cref="ModuleC" />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ModuleC : IModule
|
public class ModuleC : IModule { }
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,6 @@ container.RegisterType<ModuleA, IModule>(true);
|
|||||||
container.RegisterType<ModuleB, IModule>(true);
|
container.RegisterType<ModuleB, IModule>(true);
|
||||||
container.RegisterType<ModuleC, IModule>(true);
|
container.RegisterType<ModuleC, IModule>(true);
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("Check registrations:");
|
Console.WriteLine("Check registrations:");
|
||||||
Console.WriteLine(nameof(IClock).PadRight(20) + container.IsRegistered<IClock>());
|
Console.WriteLine(nameof(IClock).PadRight(20) + container.IsRegistered<IClock>());
|
||||||
|
@ -1,45 +1,42 @@
|
|||||||
using System;
|
|
||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
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>
|
/// <summary>
|
||||||
/// A very useful class.
|
/// Creates a new instance of <see cref="ServiceConsumer"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServiceConsumer
|
/// <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)
|
||||||
{
|
{
|
||||||
private readonly IClock _clock;
|
_clock = clock;
|
||||||
private readonly IServiceOne _service1;
|
_service1 = service1;
|
||||||
private readonly IServiceTwo _service2;
|
_service2 = service2;
|
||||||
private readonly string _id;
|
_id = Guid.NewGuid().EncodeBase64String();
|
||||||
|
Console.WriteLine(Helper.WriteMethodString(clock, _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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
}
|
}
|
@ -1,29 +1,26 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjectorDemo;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
|
||||||
|
/// </summary>
|
||||||
|
public class ServiceOne : IServiceOne
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
private readonly IClock _clock;
|
||||||
|
private readonly string _id;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
|
/// Creates a new instance of <see cref="ServiceOne"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServiceOne : IServiceOne
|
public ServiceOne(IClock clock)
|
||||||
{
|
{
|
||||||
private readonly IClock _clock;
|
_clock = clock;
|
||||||
private readonly string _id;
|
_id = Guid.NewGuid().EncodeBase64String();
|
||||||
|
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||||
/// <summary>
|
|
||||||
/// Creates a new instance of <see cref="ServiceOne"/>.
|
|
||||||
/// </summary>
|
|
||||||
public ServiceOne(IClock clock, IModule[] modules)
|
|
||||||
{
|
|
||||||
_clock = clock;
|
|
||||||
_id = Guid.NewGuid().EncodeBase64String();
|
|
||||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||||
}
|
}
|
@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using SmallInjectorDemo.Interfaces;
|
using SmallInjectorDemo.Interfaces;
|
||||||
|
|
||||||
namespace SmallInjectorDemo;
|
namespace SmallInjectorDemo;
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
using System;
|
using SmallInjectorDemo.Interfaces;
|
||||||
using SmallInjectorDemo.Interfaces;
|
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjectorDemo;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public class SystemClock : IClock
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class SystemClock : IClock
|
public string CurrentDateTime => DateTime.Now.ToString("O");
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string CurrentDateTime => DateTime.Now.ToString("O");
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user