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;
using System.Collections.Generic;
using System.Linq;
namespace SmallInjector;
namespace SmallInjector
/// <summary>
/// A small dependency injector to demonstrate the pattern.
/// </summary>
public class Container : IContainer
{
/// <summary>
/// A small dependency injector to demonstrate the pattern.
/// </summary>
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/>
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
{
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
{
@ -27,11 +21,10 @@ namespace SmallInjector
}
/// <inheritdoc/>
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);
/// <inheritdoc/>
public TService Resolve<TService>() => (TService) Resolve(typeof(TService));
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
/// <inheritdoc />
public bool IsRegistered<TService>() => IsRegistered(typeof(TService));
@ -94,5 +87,4 @@ namespace SmallInjector
public override string ToString() => ServiceType.ToString();
}
}
}

View File

@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
namespace SmallInjector;
namespace SmallInjector
/// <summary>
/// DI Container
/// </summary>
public interface IContainer
{
/// <summary>
/// DI Container
/// </summary>
public interface IContainer
{
/// <summary>
/// Register types in the resolve container.
/// </summary>
@ -51,5 +48,4 @@ namespace SmallInjector
/// Resolve any implementation
/// </summary>
IEnumerable<object> ResolveAny<TService>();
}
}

View File

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

View File

@ -1,12 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29123.88
# 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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{97F8F459-C4B4-4C40-A4CA-2443A9C245D9}"
ProjectSection(SolutionItems) = preProject
.csharpierrc = .csharpierrc
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU

View File

@ -1,18 +1,16 @@
using System;
using System.Buffers.Text;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo
namespace SmallInjectorDemo;
/// <summary>
/// Static helper class for generating random numbers.
/// </summary>
public static class Helper
{
/// <summary>
/// Static helper class for generating random numbers.
/// </summary>
public static class Helper
{
/// <summary>
/// Encode Base64 string
/// </summary>
@ -43,5 +41,4 @@ namespace SmallInjectorDemo
var classname = Path.GetFileNameWithoutExtension(filepath);
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
}
}
}

View File

@ -1,13 +1,12 @@
namespace SmallInjectorDemo.Interfaces
namespace SmallInjectorDemo.Interfaces;
/// <summary>
/// Interface of the clock.
/// </summary>
public interface IClock
{
/// <summary>
/// Interface of the clock.
/// </summary>
public interface IClock
{
/// <summary>
/// Current Date-Time.
/// </summary>
string CurrentDateTime { get; }
}
}

View File

@ -1,9 +1,6 @@
namespace SmallInjectorDemo.Interfaces
{
/// <summary>
/// Defines the <see cref="IModule" />
/// </summary>
public interface IModule
{
}
}
namespace SmallInjectorDemo.Interfaces;
/// <summary>
/// Defines the <see cref="IModule" />
/// </summary>
public interface IModule { }

View File

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

View File

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

View File

@ -1,11 +1,8 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
/// <summary>
/// Defines the <see cref="ModuleA" />
/// </summary>
public class ModuleA : IModule
{
}
}
namespace SmallInjectorDemo.Items;
/// <summary>
/// Defines the <see cref="ModuleA" />
/// </summary>
public class ModuleA : IModule { }

View File

@ -1,11 +1,8 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
/// <summary>
/// Defines the <see cref="ModuleB" />
/// </summary>
public class ModuleB : IModule
{
}
}
namespace SmallInjectorDemo.Items;
/// <summary>
/// Defines the <see cref="ModuleB" />
/// </summary>
public class ModuleB : IModule { }

View File

@ -1,11 +1,8 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
/// <summary>
/// Defines the <see cref="ModuleC" />
/// </summary>
public class ModuleC : IModule
{
}
}
namespace SmallInjectorDemo.Items;
/// <summary>
/// Defines the <see cref="ModuleC" />
/// </summary>
public class ModuleC : IModule { }

View File

@ -19,7 +19,6 @@ 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>());

View File

@ -1,13 +1,12 @@
using System;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo
namespace SmallInjectorDemo;
/// <summary>
/// A very useful class.
/// </summary>
public class ServiceConsumer
{
/// <summary>
/// A very useful class.
/// </summary>
public class ServiceConsumer
{
private readonly IClock _clock;
private readonly IServiceOne _service1;
private readonly IServiceTwo _service2;
@ -40,6 +39,4 @@ namespace SmallInjectorDemo
/// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id);
}
}

View File

@ -1,22 +1,20 @@
using System;
using System.Collections.Generic;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo
namespace SmallInjectorDemo;
/// <inheritdoc />
/// <summary>
/// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
/// </summary>
public class ServiceOne : IServiceOne
{
/// <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, IModule[] modules)
public ServiceOne(IClock clock)
{
_clock = clock;
_id = Guid.NewGuid().EncodeBase64String();
@ -25,5 +23,4 @@ namespace SmallInjectorDemo
/// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id);
}
}

View File

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

View File

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