From 15a54333a4079ae5dc53937390f0a13a87c1fc9f Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Sun, 17 Nov 2024 20:29:15 +0300 Subject: [PATCH] Minor --- .../Tests/ResolveBenchmark.cs | 1 + readme/build-up-of-an-existing-object.md | 12 ++++++----- readme/factory.md | 7 +------ ...able-instances-with-different-lifetimes.md | 5 ++++- samples/AvaloniaSimpleApp/Composition.cs | 3 ++- samples/AvaloniaSimpleApp/Program.cs | 6 ++++-- samples/Clock.Tests/ViewModelTest.cs | 3 ++- src/Pure.DI.Core/Components/Api.g.cs | 6 +++--- .../Core/Code/FactoryCodeBuilder.cs | 6 ------ src/Pure.DI.Core/Core/Code/FactoryRewriter.cs | 21 +++++++------------ src/Pure.DI.Core/Core/MermaidUrlBuilder.cs | 4 ---- src/Pure.DI.Core/Core/Models/DpInitializer.cs | 4 ++-- ...isposableWithDifferentLifetimesScenario.cs | 5 ++++- .../Basics/BuildUpScenario.cs | 8 +++---- .../Basics/FactoryScenario.cs | 5 +---- 15 files changed, 42 insertions(+), 54 deletions(-) diff --git a/benchmarks/Pure.DI.Benchmarks/Tests/ResolveBenchmark.cs b/benchmarks/Pure.DI.Benchmarks/Tests/ResolveBenchmark.cs index 72f443088..29afe7f6e 100644 --- a/benchmarks/Pure.DI.Benchmarks/Tests/ResolveBenchmark.cs +++ b/benchmarks/Pure.DI.Benchmarks/Tests/ResolveBenchmark.cs @@ -2,6 +2,7 @@ // ReSharper disable MemberCanBeMadeStatic.Local // ReSharper disable RedundantNameQualifier // ReSharper disable UnusedMethodReturnValue.Local +// ReSharper disable ReturnValueOfPureMethodIsNotUsed #pragma warning disable CS8500 namespace Pure.DI.Benchmarks.Tests; diff --git a/readme/build-up-of-an-existing-object.md b/readme/build-up-of-an-existing-object.md index 31e7a961f..ac55a5084 100644 --- a/readme/build-up-of-an-existing-object.md +++ b/readme/build-up-of-an-existing-object.md @@ -31,20 +31,18 @@ interface IService IDependency Dependency { get; } } -record Service(IDependency Dependency) : IService -{ -} +record Service(IDependency Dependency) : IService; DI.Setup(nameof(Composition)) .RootArg("name") .Bind().To(_ => Guid.NewGuid()) - .Bind().To(ctx => + .Bind().To(ctx => { var dependency = new Dependency(); ctx.BuildUp(dependency); return dependency; }) - .Bind().To() + .Bind().To() // Composition root .Root("GetMyService"); @@ -103,6 +101,7 @@ classDiagram +SetId(Guid id) : Void } Service --|> IService + Service --|> IEquatableᐸServiceᐳ class Service { +Service(IDependency Dependency) } @@ -112,6 +111,9 @@ classDiagram class IService { <> } + class IEquatableᐸServiceᐳ { + <> + } Composition ..> Service : IService GetMyService(string name) Dependency o-- String : Argument "name" Dependency *-- Guid : Guid diff --git a/readme/factory.md b/readme/factory.md index ecb1e28b0..33c8a943f 100644 --- a/readme/factory.md +++ b/readme/factory.md @@ -51,6 +51,7 @@ DI.Setup(nameof(Composition)) // Some custom logic for creating an instance. // For example, here's how you can inject and initialize // an instance of a particular type: + ctx.Inject("FakeArgTag", out var isFake); if (isFake) { @@ -58,11 +59,7 @@ DI.Setup(nameof(Composition)) } ctx.Inject(out Dependency dependency); - - // And do something about it. dependency.Initialize(); - - // And at the end return an instance return dependency; }) @@ -120,9 +117,7 @@ partial class Composition goto transientIDependency1Finish; } } Dependency localDependency35 = new Dependency(transientDateTimeOffset3); - // And do something about it. localDependency35.Initialize(); - // And at the end return an instance transientIDependency1 = localDependency35; transientIDependency1Finish:; return new Service(transientIDependency1); diff --git a/readme/tracking-disposable-instances-with-different-lifetimes.md b/readme/tracking-disposable-instances-with-different-lifetimes.md index 52a415e3d..5199eae49 100644 --- a/readme/tracking-disposable-instances-with-different-lifetimes.md +++ b/readme/tracking-disposable-instances-with-different-lifetimes.md @@ -57,12 +57,15 @@ partial class Composition var composition = new Composition(); var root1 = composition.Root; var root2 = composition.Root; +root1.Dependency.ShouldNotBe(root2.Dependency); +root1.SingleDependency.ShouldBe(root2.SingleDependency); root2.Dispose(); // Checks that the disposable instances // associated with root1 have been disposed of root2.Dependency.IsDisposed.ShouldBeTrue(); + // But the singleton is still not disposed of root2.SingleDependency.IsDisposed.ShouldBeFalse(); @@ -76,11 +79,11 @@ root1.Dispose(); // Checks that the disposable instances // associated with root2 have been disposed of root1.Dependency.IsDisposed.ShouldBeTrue(); + // But the singleton is still not disposed of root1.SingleDependency.IsDisposed.ShouldBeFalse(); composition.Dispose(); -root2.SingleDependency.IsDisposed.ShouldBeTrue(); root1.SingleDependency.IsDisposed.ShouldBeTrue(); ``` diff --git a/samples/AvaloniaSimpleApp/Composition.cs b/samples/AvaloniaSimpleApp/Composition.cs index 5717f8dac..382709258 100644 --- a/samples/AvaloniaSimpleApp/Composition.cs +++ b/samples/AvaloniaSimpleApp/Composition.cs @@ -1,4 +1,5 @@ -namespace AvaloniaSimpleApp; +// ReSharper disable UnusedMember.Local +namespace AvaloniaSimpleApp; using Pure.DI; using static Pure.DI.Lifetime; diff --git a/samples/AvaloniaSimpleApp/Program.cs b/samples/AvaloniaSimpleApp/Program.cs index cd2df4a86..c98fcc254 100644 --- a/samples/AvaloniaSimpleApp/Program.cs +++ b/samples/AvaloniaSimpleApp/Program.cs @@ -1,9 +1,11 @@ using Avalonia; -using System; + +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable ClassNeverInstantiated.Global namespace AvaloniaSimpleApp; -class Program +public class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized diff --git a/samples/Clock.Tests/ViewModelTest.cs b/samples/Clock.Tests/ViewModelTest.cs index 059d27b09..0c231a9b5 100644 --- a/samples/Clock.Tests/ViewModelTest.cs +++ b/samples/Clock.Tests/ViewModelTest.cs @@ -1,5 +1,6 @@ namespace Clock.Tests; +using System.Collections.ObjectModel; using Models; public class ViewModelTest @@ -36,7 +37,7 @@ private class TestViewModel : ViewModel public TestViewModel() => PropertyChanged += (_, args) => { _propertyNames.Add(args.PropertyName); }; - public IReadOnlyList PropertyNames => _propertyNames.AsReadOnly(); + public ReadOnlyCollection PropertyNames => _propertyNames.AsReadOnly(); public void RaiseOnPropertyChanged(string? propertyName = null) => OnPropertyChanged(propertyName); diff --git a/src/Pure.DI.Core/Components/Api.g.cs b/src/Pure.DI.Core/Components/Api.g.cs index 9680b2e70..b05aa74ec 100644 --- a/src/Pure.DI.Core/Components/Api.g.cs +++ b/src/Pure.DI.Core/Components/Api.g.cs @@ -1335,10 +1335,10 @@ partial void OnDisposeException(T disposableInstance, global::System.Exceptio /// /// Implement this partial method to handle the exception on disposing. /// - /// The disposable instance. + /// The disposable instance. /// Exception occurring during disposal. /// The actual type of instance being disposed of. - partial void OnDisposeAsyncException(T asynDisposableInstance, global::System.Exception exception) + partial void OnDisposeAsyncException(T asyncDisposableInstance, global::System.Exception exception) where T : global::System.IAsyncDisposable; #endif } @@ -1361,7 +1361,7 @@ public Owned(T value, global::Pure.DI.IOwned owned) Value = value; _owned = owned; } - + /// public void Dispose() { diff --git a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs index fb2992574..40e1a00b0 100644 --- a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs @@ -75,12 +75,6 @@ public void Build(BuildContext ctx, in DpFactory factory) switch (member) { case IFieldSymbol: - value = SyntaxFactory.MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - instance, - SyntaxFactory.IdentifierName(member.Name)); - break; - case IPropertySymbol: value = SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, diff --git a/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs b/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs index 97aea7c89..938d36183 100644 --- a/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs +++ b/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs @@ -142,17 +142,12 @@ public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax no return node; } - var name = ""; - switch (memberAccessExpression.Name) + var name = memberAccessExpression.Name switch { - case GenericNameSyntax { TypeArgumentList.Arguments.Count: 1 } genericName: - name = genericName.Identifier.Text; - break; - - case IdentifierNameSyntax identifierName: - name = identifierName.Identifier.Text; - break; - } + GenericNameSyntax { TypeArgumentList.Arguments.Count: 1 } genericName => genericName.Identifier.Text, + IdentifierNameSyntax identifierName => identifierName.Identifier.Text, + _ => "" + }; ExpressionSyntax? expressionSyntax = default; var processed = name switch @@ -225,14 +220,14 @@ private bool TryInitialize( switch (value) { case IdentifierNameSyntax identifierName: - initializers.Add(new Initializer(identifierName.Identifier.Text, false)); + initializers.Add(new Initializer(identifierName.Identifier.Text)); { expressionSyntax = triviaTools.PreserveTrivia(InitializationMarkerExpression, invocation); return true; } case DeclarationExpressionSyntax { Designation: SingleVariableDesignationSyntax singleVariableDesignationSyntax }: - initializers.Add(new Initializer(singleVariableDesignationSyntax.Identifier.Text, true)); + initializers.Add(new Initializer(singleVariableDesignationSyntax.Identifier.Text)); { expressionSyntax = triviaTools.PreserveTrivia(InitializationMarkerExpression, invocation); return true; @@ -276,5 +271,5 @@ private bool TryInitialize( internal record Injection(string VariableName, bool DeclarationRequired); - internal record Initializer(string VariableName, bool DeclarationRequired); + internal record Initializer(string VariableName); } \ No newline at end of file diff --git a/src/Pure.DI.Core/Core/MermaidUrlBuilder.cs b/src/Pure.DI.Core/Core/MermaidUrlBuilder.cs index 33e9cf9d6..82ac8c593 100644 --- a/src/Pure.DI.Core/Core/MermaidUrlBuilder.cs +++ b/src/Pure.DI.Core/Core/MermaidUrlBuilder.cs @@ -45,10 +45,6 @@ private static string JsonEncode(string text) case '\\': case '"': - sb.Append('\\'); - sb.Append(ch); - break; - case '/': sb.Append('\\'); sb.Append(ch); diff --git a/src/Pure.DI.Core/Core/Models/DpInitializer.cs b/src/Pure.DI.Core/Core/Models/DpInitializer.cs index bad86803f..51443b78b 100644 --- a/src/Pure.DI.Core/Core/Models/DpInitializer.cs +++ b/src/Pure.DI.Core/Core/Models/DpInitializer.cs @@ -1,5 +1,5 @@ // ReSharper disable HeapView.ObjectAllocation - +// ReSharper disable NotAccessedPositionalProperty.Global namespace Pure.DI.Core.Models; internal record DpInitializer( @@ -8,7 +8,7 @@ internal record DpInitializer( in ImmutableArray Properties, in ImmutableArray Fields) { - public IEnumerable ToStrings(int indent) + private IEnumerable ToStrings(int indent) { var walker = new DependenciesToLinesWalker(indent); walker.VisitInitializer(Unit.Shared, this); diff --git a/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithDifferentLifetimesScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithDifferentLifetimesScenario.cs index 81fee71fd..0937edf83 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithDifferentLifetimesScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithDifferentLifetimesScenario.cs @@ -76,12 +76,15 @@ public void Run() var composition = new Composition(); var root1 = composition.Root; var root2 = composition.Root; + root1.Dependency.ShouldNotBe(root2.Dependency); + root1.SingleDependency.ShouldBe(root2.SingleDependency); root2.Dispose(); // Checks that the disposable instances // associated with root1 have been disposed of root2.Dependency.IsDisposed.ShouldBeTrue(); + // But the singleton is still not disposed of root2.SingleDependency.IsDisposed.ShouldBeFalse(); @@ -95,11 +98,11 @@ public void Run() // Checks that the disposable instances // associated with root2 have been disposed of root1.Dependency.IsDisposed.ShouldBeTrue(); + // But the singleton is still not disposed of root1.SingleDependency.IsDisposed.ShouldBeFalse(); composition.Dispose(); - root2.SingleDependency.IsDisposed.ShouldBeTrue(); root1.SingleDependency.IsDisposed.ShouldBeTrue(); // } new Composition().SaveClassDiagram(); diff --git a/tests/Pure.DI.UsageTests/Basics/BuildUpScenario.cs b/tests/Pure.DI.UsageTests/Basics/BuildUpScenario.cs index 43e38894d..6c5c43643 100644 --- a/tests/Pure.DI.UsageTests/Basics/BuildUpScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/BuildUpScenario.cs @@ -41,9 +41,7 @@ interface IService IDependency Dependency { get; } } -record Service(IDependency Dependency) : IService -{ -} +record Service(IDependency Dependency) : IService; // } public class Scenario @@ -56,13 +54,13 @@ public void Run() DI.Setup(nameof(Composition)) .RootArg("name") .Bind().To(_ => Guid.NewGuid()) - .Bind().To(ctx => + .Bind().To(ctx => { var dependency = new Dependency(); ctx.BuildUp(dependency); return dependency; }) - .Bind().To() + .Bind().To() // Composition root .Root("GetMyService"); diff --git a/tests/Pure.DI.UsageTests/Basics/FactoryScenario.cs b/tests/Pure.DI.UsageTests/Basics/FactoryScenario.cs index 6e7cd9324..75b5b980a 100644 --- a/tests/Pure.DI.UsageTests/Basics/FactoryScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/FactoryScenario.cs @@ -73,6 +73,7 @@ public void Run() // Some custom logic for creating an instance. // For example, here's how you can inject and initialize // an instance of a particular type: + ctx.Inject("FakeArgTag", out var isFake); if (isFake) { @@ -80,11 +81,7 @@ public void Run() } ctx.Inject(out Dependency dependency); - - // And do something about it. dependency.Initialize(); - - // And at the end return an instance return dependency; })