Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jan 25, 2025
1 parent 99c2a85 commit c0b901d
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 22 deletions.
2 changes: 2 additions & 0 deletions benchmarks/Pure.DI.Benchmarks/Benchmarks/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#pragma warning disable CA1822
namespace Pure.DI.Benchmarks.Benchmarks;

using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Model;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public partial class Array : BenchmarkBase
{
private static void SetupDI() =>
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Pure.DI.Benchmarks/Benchmarks/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#pragma warning disable CA1822
namespace Pure.DI.Benchmarks.Benchmarks;

using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Model;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public partial class Enum : BenchmarkBase
{
private static void SetupDI() =>
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Pure.DI.Benchmarks/Benchmarks/Func.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#pragma warning disable CA1822
namespace Pure.DI.Benchmarks.Benchmarks;

using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Model;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public partial class Func : BenchmarkBase
{
private static void SetupDI() =>
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Pure.DI.Benchmarks/Benchmarks/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#pragma warning disable CA1822
namespace Pure.DI.Benchmarks.Benchmarks;

using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Model;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public partial class Singleton : BenchmarkBase
{
private static void SetupDI() =>
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Pure.DI.Benchmarks/Benchmarks/Transient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#pragma warning disable CA1822
namespace Pure.DI.Benchmarks.Benchmarks;

using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Model;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public partial class Transient : BenchmarkBase
{
private static void SetupDI() =>
Expand Down
2 changes: 1 addition & 1 deletion readme/build-up-of-an-existing-generic-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService<Guid> GetMyService(string name)
{
if (Object.ReferenceEquals(name, null)) throw new ArgumentNullException(nameof(name));
if (name is null) throw new ArgumentNullException(nameof(name));
Guid transientGuid2 = Guid.NewGuid();
Dependency<Guid> transientDependency1;
Dependency<Guid> localDependency55 = new Dependency<Guid>();
Expand Down
2 changes: 1 addition & 1 deletion readme/build-up-of-an-existing-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService GetMyService(string name)
{
if (Object.ReferenceEquals(name, null)) throw new ArgumentNullException(nameof(name));
if (name is null) throw new ArgumentNullException(nameof(name));
Guid transientGuid2 = Guid.NewGuid();
Dependency transientDependency1;
var localDependency49 = new Dependency();
Expand Down
2 changes: 1 addition & 1 deletion readme/builders-with-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Service BuildUp(Service buildingInstance, Guid serviceId)
{
if (Object.ReferenceEquals(buildingInstance, null)) throw new ArgumentNullException(nameof(buildingInstance));
if (buildingInstance is null) throw new ArgumentNullException(nameof(buildingInstance));
Service transientService0;
Service localBuildingInstance48 = buildingInstance;
localBuildingInstance48.Dependency = new Dependency();
Expand Down
2 changes: 1 addition & 1 deletion readme/builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Service BuildUp(Service buildingInstance)
{
if (Object.ReferenceEquals(buildingInstance, null)) throw new ArgumentNullException(nameof(buildingInstance));
if (buildingInstance is null) throw new ArgumentNullException(nameof(buildingInstance));
Guid transientGuid2 = Guid.NewGuid();
Service transientService0;
Service localBuildingInstance47 = buildingInstance;
Expand Down
2 changes: 1 addition & 1 deletion readme/complex-generic-root-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService<T> GetMyService<T>(MyData<T> complexArg)
{
if (Object.ReferenceEquals(complexArg, null)) throw new ArgumentNullException(nameof(complexArg));
if (complexArg is null) throw new ArgumentNullException(nameof(complexArg));
Service<T> transientService0 = new Service<T>();
transientService0.SetDependency(complexArg);
return transientService0;
Expand Down
2 changes: 1 addition & 1 deletion readme/complex-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ partial class Composition
public Program<T3> GetRoot<T3>(T3 depArg)
where T3: notnull
{
if (Object.ReferenceEquals(depArg, null)) throw new ArgumentNullException(nameof(depArg));
if (depArg is null) throw new ArgumentNullException(nameof(depArg));
if (!_root._singletonDependencyStruct51Created)
{
using (_lock.EnterScope())
Expand Down
2 changes: 1 addition & 1 deletion readme/exposed-roots-via-root-arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Program GetProgram(OtherAssembly.CompositionInOtherProject baseComposition)
{
if (Object.ReferenceEquals(baseComposition, null)) throw new ArgumentNullException(nameof(baseComposition));
if (baseComposition is null) throw new ArgumentNullException(nameof(baseComposition));
OtherAssembly.IMyService transientIMyService1;
OtherAssembly.CompositionInOtherProject localInstance_1182D1277 = baseComposition;
transientIMyService1 = localInstance_1182D1277.MyService;
Expand Down
4 changes: 2 additions & 2 deletions readme/generic-builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface IService<out T, T2>
record Service<T, T2>: IService<T, T2>
where T: struct
{
public T Id { get; private set; } = default(T);
public T Id { get; private set; }

[Dependency]
public IDependency<T2>? Dependency { get; set; }
Expand Down Expand Up @@ -91,7 +91,7 @@ partial class Composition
public Service<T1, T2> BuildUpGeneric<T1, T2>(Service<T1, T2> buildingInstance)
where T1: struct
{
if (Object.ReferenceEquals(buildingInstance, null)) throw new ArgumentNullException(nameof(buildingInstance));
if (buildingInstance is null) throw new ArgumentNullException(nameof(buildingInstance));
T1 transientTTS2 = (T1)(object)Guid.NewGuid();
Service<T1, T2> transientService0;
Service<T1, T2> localBuildingInstance54 = buildingInstance;
Expand Down
2 changes: 1 addition & 1 deletion readme/generic-root-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService<T3> GetMyService<T3>(T3 someArg)
{
if (Object.ReferenceEquals(someArg, null)) throw new ArgumentNullException(nameof(someArg));
if (someArg is null) throw new ArgumentNullException(nameof(someArg));
Service<T3> transientService0 = new Service<T3>();
transientService0.SetDependency(someArg);
return transientService0;
Expand Down
4 changes: 2 additions & 2 deletions readme/root-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService CreateServiceWithArgs(int id, string dependencyName, string serviceName)
{
if (Object.ReferenceEquals(dependencyName, null)) throw new ArgumentNullException(nameof(dependencyName));
if (Object.ReferenceEquals(serviceName, null)) throw new ArgumentNullException(nameof(serviceName));
if (dependencyName is null) throw new ArgumentNullException(nameof(dependencyName));
if (serviceName is null) throw new ArgumentNullException(nameof(serviceName));
return new Service(serviceName, new Dependency(id, dependencyName));
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Pure.DI.Core.Code;
internal class BlockCodeBuilder(
INodeInfo nodeInfo,
ICompilations compilations,
ILocks locks)
ILocks locks,
IBuildTools buildTools)
: ICodeBuilder<Block>
{
public void Build(BuildContext ctx, in Block block)
{
var variable = ctx.Variable;
var compilation = variable.Node.Binding.SemanticModel.Compilation;
var languageVersion = compilations.GetLanguageVersion(compilation);
if (!IsNewInstanceRequired(variable))
{
return;
Expand Down Expand Up @@ -48,9 +48,7 @@ public void Build(BuildContext ctx, in Block block)
{
var checkExpression = variable.InstanceType.IsValueType
? $"!{variable.VariableName}Created"
: languageVersion >= LanguageVersion.CSharp9
? $"{variable.VariableName} is null"
: $"{Names.ObjectTypeName}.ReferenceEquals({variable.VariableName}, null)";
: buildTools.NullCheck(compilation, variable.VariableName);

if (lockIsRequired)
{
Expand Down Expand Up @@ -144,7 +142,7 @@ public void Build(BuildContext ctx, in Block block)
&& code.Count > 11)
{
var localMethodCode = ctx.LocalFunctionsCode;
if (languageVersion >= LanguageVersion.CSharp9)
if (compilations.GetLanguageVersion(compilation) >= LanguageVersion.CSharp9)
{
localMethodCode.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]");
}
Expand Down
11 changes: 10 additions & 1 deletion src/Pure.DI.Core/Core/Code/BuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ internal class BuildTools(
IBaseSymbolsProvider baseSymbolsProvider,
[Tag(Injection)] IIdGenerator idGenerator,
ILocks locks,
ISymbolNames symbolNames)
ISymbolNames symbolNames,
ICompilations compilations)
: IBuildTools
{
public string NullCheck(Compilation compilation, string variableName)
{
var languageVersion = compilations.GetLanguageVersion(compilation);
return languageVersion >= LanguageVersion.CSharp9
? $"{variableName} is null"
: $"{Names.ObjectTypeName}.ReferenceEquals({variableName}, null)";
}

public void AddPureHeader(LinesBuilder code)
{
code.AppendLine("#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP || NET40_OR_GREATER || NET");
Expand Down
2 changes: 2 additions & 0 deletions src/Pure.DI.Core/Core/Code/IBuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Pure.DI.Core.Code;

internal interface IBuildTools
{
string NullCheck(Compilation compilation, string variableName);

void AddPureHeader(LinesBuilder code);

string GetDeclaration(Variable variable, string separator = " ");
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void BuildRoot(CompositionCode composition, Root root)
{
foreach (var arg in root.Args.Where(i => i.InstanceType.IsReferenceType))
{
code.AppendLine($"if ({Names.ObjectTypeName}.ReferenceEquals({arg.VariableName}, null)) throw new {Names.SystemNamespace}ArgumentNullException(nameof({arg.VariableName}));");
code.AppendLine($"if ({buildTools.NullCheck(composition.Source.Source.SemanticModel.Compilation, arg.VariableName)}) throw new {Names.SystemNamespace}ArgumentNullException(nameof({arg.VariableName}));");
}
}
else
Expand Down
1 change: 1 addition & 0 deletions tests/Pure.DI.UsageTests/Basics/BuilderScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable RedundantArgumentDefaultValue
// ReSharper disable UnusedMemberInSuper.Global
namespace Pure.DI.UsageTests.Basics.BuilderScenario;

using Shouldly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable RedundantArgumentDefaultValue
// ReSharper disable UnusedMemberInSuper.Global
namespace Pure.DI.UsageTests.Basics.BuilderWithArgumentsScenario;

using Shouldly;
Expand Down
1 change: 1 addition & 0 deletions tests/Pure.DI.UsageTests/Basics/ResolveMethodsScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Pure.DI.UsageTests.Basics.ResolveMethodsScenario;
//# using Pure.DI;
// }

[SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known")]
public class Scenario
{
[Fact]
Expand Down
5 changes: 3 additions & 2 deletions tests/Pure.DI.UsageTests/Generics/GenericBuilderScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers

// ReSharper disable UnusedTypeParameter
// ReSharper disable UnusedMemberInSuper.Global
namespace Pure.DI.UsageTests.Basics.GenericBuilderScenario;

using Shouldly;
Expand Down Expand Up @@ -55,7 +56,7 @@ interface IService<out T, T2>
record Service<T, T2>: IService<T, T2>
where T: struct
{
public T Id { get; private set; } = default(T);
public T Id { get; private set; }

[Dependency]
public IDependency<T2>? Dependency { get; set; }
Expand Down

0 comments on commit c0b901d

Please sign in to comment.