Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 17, 2024
1 parent ffe65e7 commit 15a5433
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 54 deletions.
1 change: 1 addition & 0 deletions benchmarks/Pure.DI.Benchmarks/Tests/ResolveBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 7 additions & 5 deletions readme/build-up-of-an-existing-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ interface IService
IDependency Dependency { get; }
}

record Service(IDependency Dependency) : IService
{
}
record Service(IDependency Dependency) : IService;

DI.Setup(nameof(Composition))
.RootArg<string>("name")
.Bind().To(_ => Guid.NewGuid())
.Bind<IDependency>().To(ctx =>
.Bind().To(ctx =>
{
var dependency = new Dependency();
ctx.BuildUp(dependency);
return dependency;
})
.Bind<IService>().To<Service>()
.Bind().To<Service>()

// Composition root
.Root<IService>("GetMyService");
Expand Down Expand Up @@ -103,6 +101,7 @@ classDiagram
+SetId(Guid id) : Void
}
Service --|> IService
Service --|> IEquatableᐸServiceᐳ
class Service {
+Service(IDependency Dependency)
}
Expand All @@ -112,6 +111,9 @@ classDiagram
class IService {
<<interface>>
}
class IEquatableᐸServiceᐳ {
<<interface>>
}
Composition ..> Service : IService GetMyService(string name)
Dependency o-- String : Argument "name"
Dependency *-- Guid : Guid
Expand Down
7 changes: 1 addition & 6 deletions readme/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ 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<bool>("FakeArgTag", out var isFake);
if (isFake)
{
return new FakeDependency();
}

ctx.Inject(out Dependency dependency);

// And do something about it.
dependency.Initialize();

// And at the end return an instance
return dependency;

})
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

Expand Down
3 changes: 2 additions & 1 deletion samples/AvaloniaSimpleApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace AvaloniaSimpleApp;
// ReSharper disable UnusedMember.Local
namespace AvaloniaSimpleApp;

using Pure.DI;
using static Pure.DI.Lifetime;
Expand Down
6 changes: 4 additions & 2 deletions samples/AvaloniaSimpleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion samples/Clock.Tests/ViewModelTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Clock.Tests;

using System.Collections.ObjectModel;
using Models;

public class ViewModelTest
Expand Down Expand Up @@ -36,7 +37,7 @@ private class TestViewModel : ViewModel
public TestViewModel() =>
PropertyChanged += (_, args) => { _propertyNames.Add(args.PropertyName); };

public IReadOnlyList<string?> PropertyNames => _propertyNames.AsReadOnly();
public ReadOnlyCollection<string?> PropertyNames => _propertyNames.AsReadOnly();

public void RaiseOnPropertyChanged(string? propertyName = null) =>
OnPropertyChanged(propertyName);
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,10 +1335,10 @@ partial void OnDisposeException<T>(T disposableInstance, global::System.Exceptio
/// <summary>
/// Implement this partial method to handle the exception on disposing.
/// </summary>
/// <param name="asynDisposableInstance">The disposable instance.</param>
/// <param name="asyncDisposableInstance">The disposable instance.</param>
/// <param name="exception">Exception occurring during disposal.</param>
/// <typeparam name="T">The actual type of instance being disposed of.</typeparam>
partial void OnDisposeAsyncException<T>(T asynDisposableInstance, global::System.Exception exception)
partial void OnDisposeAsyncException<T>(T asyncDisposableInstance, global::System.Exception exception)
where T : global::System.IAsyncDisposable;
#endif
}
Expand All @@ -1361,7 +1361,7 @@ public Owned(T value, global::Pure.DI.IOwned owned)
Value = value;
_owned = owned;
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
6 changes: 0 additions & 6 deletions src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 8 additions & 13 deletions src/Pure.DI.Core/Core/Code/FactoryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
4 changes: 0 additions & 4 deletions src/Pure.DI.Core/Core/MermaidUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ private static string JsonEncode(string text)

case '\\':
case '"':
sb.Append('\\');
sb.Append(ch);
break;

case '/':
sb.Append('\\');
sb.Append(ch);
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Models/DpInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ReSharper disable HeapView.ObjectAllocation

// ReSharper disable NotAccessedPositionalProperty.Global
namespace Pure.DI.Core.Models;

internal record DpInitializer(
Expand All @@ -8,7 +8,7 @@ internal record DpInitializer(
in ImmutableArray<DpProperty> Properties,
in ImmutableArray<DpField> Fields)
{
public IEnumerable<string> ToStrings(int indent)
private IEnumerable<string> ToStrings(int indent)
{
var walker = new DependenciesToLinesWalker(indent);
walker.VisitInitializer(Unit.Shared, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down
8 changes: 3 additions & 5 deletions tests/Pure.DI.UsageTests/Basics/BuildUpScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ interface IService
IDependency Dependency { get; }
}

record Service(IDependency Dependency) : IService
{
}
record Service(IDependency Dependency) : IService;
// }

public class Scenario
Expand All @@ -56,13 +54,13 @@ public void Run()
DI.Setup(nameof(Composition))
.RootArg<string>("name")
.Bind().To(_ => Guid.NewGuid())
.Bind<IDependency>().To(ctx =>
.Bind().To(ctx =>
{
var dependency = new Dependency();
ctx.BuildUp(dependency);
return dependency;
})
.Bind<IService>().To<Service>()
.Bind().To<Service>()

// Composition root
.Root<IService>("GetMyService");
Expand Down
5 changes: 1 addition & 4 deletions tests/Pure.DI.UsageTests/Basics/FactoryScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,15 @@ 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<bool>("FakeArgTag", out var isFake);
if (isFake)
{
return new FakeDependency();
}

ctx.Inject(out Dependency dependency);

// And do something about it.
dependency.Initialize();

// And at the end return an instance
return dependency;

})
Expand Down

0 comments on commit 15a5433

Please sign in to comment.