Code cleanup and enforce csharpier

This commit is contained in:
Holger Börchers 2022-11-13 21:03:26 +01:00
parent 32cc67b25a
commit 8884d55380
19 changed files with 281 additions and 312 deletions

6
.csharpierrc Normal file
View File

@ -0,0 +1,6 @@
{
"printWidth": 200,
"useTabs": false,
"tabWidth": 4,
"preprocessorSymbolSets": ["", "DEBUG", "DEBUG,CODE_STYLE"]
}

3
.filenesting.json Normal file
View File

@ -0,0 +1,3 @@
{
"help":"https://go.microsoft.com/fwlink/?linkid=866610"
}

View File

@ -1,24 +1,18 @@
using System; namespace SmallInjector;
using System.Collections.Generic;
using System.Linq;
namespace SmallInjector
{
/// <summary> /// <summary>
/// A small dependency injector to demonstrate the pattern. /// A small dependency injector to demonstrate the pattern.
/// </summary> /// </summary>
public class Container : IContainer public class Container : IContainer
{ {
private readonly Dictionary<Type, List<RegisteredType>> _container = new Dictionary<Type, List<RegisteredType>>(); private readonly Dictionary<Type, List<RegisteredType>> _container = new();
/// <inheritdoc/> /// <inheritdoc/>
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
where TService : TInterface
{ {
if (!IsRegistered<TInterface>()) if (!IsRegistered<TInterface>())
{ {
_container[typeof(TInterface)] = new List<RegisteredType> _container[typeof(TInterface)] = new List<RegisteredType> { new RegisteredType(typeof(TService), isSingleton, null) };
{new RegisteredType(typeof(TService), isSingleton, null)};
} }
else else
{ {
@ -27,8 +21,7 @@ namespace SmallInjector
} }
/// <inheritdoc/> /// <inheritdoc/>
public void RegisterType<TService>(bool isSingleton, TService instance = default) => public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
RegisterType<TService, TService>(isSingleton, instance);
/// <inheritdoc/> /// <inheritdoc/>
public TService Resolve<TService>() => (TService)Resolve(typeof(TService)); public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
@ -95,4 +88,3 @@ namespace SmallInjector
public override string ToString() => ServiceType.ToString(); public override string ToString() => ServiceType.ToString();
} }
} }
}

View File

@ -1,8 +1,5 @@
using System; namespace SmallInjector;
using System.Collections.Generic;
namespace SmallInjector
{
/// <summary> /// <summary>
/// DI Container /// DI Container
/// </summary> /// </summary>
@ -52,4 +49,3 @@ namespace SmallInjector
/// </summary> /// </summary>
IEnumerable<object> ResolveAny<TService>(); IEnumerable<object> ResolveAny<TService>();
} }
}

View File

@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

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

View File

@ -1,13 +1,11 @@
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> /// <summary>
/// Static helper class for generating random numbers. /// Static helper class for generating random numbers.
/// </summary> /// </summary>
@ -44,4 +42,3 @@ namespace SmallInjectorDemo
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}"; return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
} }
} }
}

View File

@ -1,5 +1,5 @@
namespace SmallInjectorDemo.Interfaces namespace SmallInjectorDemo.Interfaces;
{
/// <summary> /// <summary>
/// Interface of the clock. /// Interface of the clock.
/// </summary> /// </summary>
@ -10,4 +10,3 @@ namespace SmallInjectorDemo.Interfaces
/// </summary> /// </summary>
string CurrentDateTime { get; } string CurrentDateTime { get; }
} }
}

View File

@ -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 { }
{
}
}

View File

@ -1,10 +1,6 @@
namespace SmallInjectorDemo.Interfaces namespace SmallInjectorDemo.Interfaces;
{
/// <summary> /// <summary>
/// Interface for service one. /// Interface for service one.
/// </summary> /// </summary>
public interface IServiceOne public interface IServiceOne { }
{
}
}

View File

@ -1,10 +1,6 @@
namespace SmallInjectorDemo.Interfaces namespace SmallInjectorDemo.Interfaces;
{
/// <summary> /// <summary>
/// Interface for service two. /// Interface for service two.
/// </summary> /// </summary>
public interface IServiceTwo public interface IServiceTwo { }
{
}
}

View File

@ -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 { }
{
}
}

View File

@ -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 { }
{
}
}

View File

@ -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 { }
{
}
}

View File

@ -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>());

View File

@ -1,8 +1,7 @@
using System;
using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo;
{
/// <summary> /// <summary>
/// A very useful class. /// A very useful class.
/// </summary> /// </summary>
@ -40,6 +39,4 @@ namespace SmallInjectorDemo
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id); public override string ToString() => Helper.WriteMethodString(_clock, _id);
}
} }

View File

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo;
{
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />. /// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
@ -16,7 +14,7 @@ namespace SmallInjectorDemo
/// <summary> /// <summary>
/// Creates a new instance of <see cref="ServiceOne"/>. /// Creates a new instance of <see cref="ServiceOne"/>.
/// </summary> /// </summary>
public ServiceOne(IClock clock, IModule[] modules) public ServiceOne(IClock clock)
{ {
_clock = clock; _clock = clock;
_id = Guid.NewGuid().EncodeBase64String(); _id = Guid.NewGuid().EncodeBase64String();
@ -26,4 +24,3 @@ namespace SmallInjectorDemo
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id); public override string ToString() => Helper.WriteMethodString(_clock, _id);
} }
}

View File

@ -1,4 +1,3 @@
using System;
using SmallInjectorDemo.Interfaces; using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo; namespace SmallInjectorDemo;

View File

@ -1,12 +1,10 @@
using System; using SmallInjectorDemo.Interfaces;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo;
namespace SmallInjectorDemo
{
/// <inheritdoc /> /// <inheritdoc />
public class SystemClock : IClock public class SystemClock : IClock
{ {
/// <inheritdoc /> /// <inheritdoc />
public string CurrentDateTime => DateTime.Now.ToString("O"); public string CurrentDateTime => DateTime.Now.ToString("O");
} }
}