Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 14, 2024
1 parent 2bf3c68 commit 401b2f5
Show file tree
Hide file tree
Showing 58 changed files with 89 additions and 98 deletions.
6 changes: 3 additions & 3 deletions readme/a-few-partial-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ class Service(IDependency dependency) : IService;
partial class Composition
{
// This method will not be called in runtime
void Setup1() =>
static void Setup1() =>
DI.Setup()
.Bind<IDependency>().To<Dependency>();
}

partial class Composition
{
// This method will not be called in runtime
void Setup2() =>
static void Setup2() =>
DI.Setup()
.Bind<IService>().To<Service>();
}

partial class Composition
{
// This method will not be called in runtime
private void Setup3() =>
private static void Setup3() =>
DI.Setup()
.Root<IService>("Root");
}
Expand Down
2 changes: 1 addition & 1 deletion readme/async-disposable-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Program(Func<Session> sessionFactory)

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
Expand Down
2 changes: 1 addition & 1 deletion readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Program(Func<IService> serviceFactory)

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
Expand Down
2 changes: 1 addition & 1 deletion readme/check-for-a-root.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ partial class Composition
internal static bool HasRoot(Type type, object? key = default) =>
Roots.Contains((type, key));

void Setup() =>
static void Setup() =>
DI.Setup()
// Specifies to use the partial OnNewRoot method
// to register each root
Expand Down
4 changes: 2 additions & 2 deletions readme/global-compositions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ When the `Setup(name, kind)` method is called, the second optional parameter spe
```c#
class MyGlobalComposition
{
void Setup() =>
static void Setup() =>
DI.Setup(kind: CompositionKind.Global)
.Hint(Hint.ToString, "Off")
.Hint(Hint.FormatCode, "Off");
}

class MyGlobalComposition2
{
void Setup() =>
static void Setup() =>
DI.Setup(kind: CompositionKind.Global)
.Hint(Hint.ToString, "On");
}
Expand Down
2 changes: 1 addition & 1 deletion readme/resolve-hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DI.Setup(nameof(Composition))
.Bind().To<Dependency>()
.Root<IDependency>("DependencyRoot")
.Bind().To<Service>()
.Root<IService>("Root");;
.Root<IService>("Root");

var composition = new Composition();
var service = composition.Root;
Expand Down
2 changes: 1 addition & 1 deletion readme/scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Program(Func<Session> sessionFactory)

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
Expand Down
2 changes: 1 addition & 1 deletion readme/service-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ partial class Composition: ServiceProviderFactory<Composition>
public IServiceCollection ServiceCollection =>
CreateServiceCollection(this);

void Setup() =>
static void Setup() =>
DI.Setup()
.DependsOn(Base)
.Bind<IDependency>("Dependency Key").As(Lifetime.Singleton).To<Dependency>()
Expand Down
2 changes: 1 addition & 1 deletion readme/service-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Service(IDependency dependency) : IService

partial class Composition: IServiceProvider
{
void Setup() =>
static void Setup() =>
DI.Setup()
// The following hint overrides the name of the
// "object Resolve(Type type)" method in "GetService",
Expand Down
2 changes: 1 addition & 1 deletion readme/threadsafe-hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DI.Setup(nameof(Composition))
.Hint(ThreadSafe, "Off")
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>("Root");;
.Root<IService>("Root");

var composition = new Composition();
var service = composition.Root;
Expand Down
2 changes: 1 addition & 1 deletion readme/tracking-async-disposable-instances-in-delegates.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Service(Func<Owned<IDependency>> dependencyFactory)

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
.Bind<IDependency>().To<Dependency>()
.Bind().To<Service>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Service(IDependency dependency) : IService

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
.Bind().To<Dependency>()
.Bind().To<Service>()
Expand Down
2 changes: 1 addition & 1 deletion readme/tracking-disposable-instances-in-delegates.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Service(Func<Owned<IDependency>> dependencyFactory)

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
.Bind().To<Dependency>()
.Bind().To<Service>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Service(IDependency dependency) : IService

partial class Composition
{
void Setup() =>
static void Setup() =>
DI.Setup()
.Bind().To<Dependency>()
.Bind().To<Service>()
Expand Down
2 changes: 1 addition & 1 deletion samples/GrpcService/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace GrpcService;

internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
Expand Down
2 changes: 1 addition & 1 deletion samples/ShroedingersCat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal partial class Composition
// [Conditional("DI")] attribute avoids generating IL code for the method that follows it.
// Since this method is needed only at the compile time.
[Conditional("DI")]
void Setup() => DI.Setup()
static void Setup() => DI.Setup()
// Models a random subatomic event that may or may not occur
.Bind().As(Singleton).To<Random>()
// Quantum superposition of two states: Alive or Dead
Expand Down
1 change: 0 additions & 1 deletion samples/SingleRootAvaloniaApp/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace AvaloniaApp;
using Clock.Models;
using Clock.ViewModels;
using Pure.DI;
using Views;
using static Pure.DI.Lifetime;

internal partial class Composition
Expand Down
2 changes: 1 addition & 1 deletion samples/WebAPI/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace WebAPI;

internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
Expand Down
2 changes: 1 addition & 1 deletion samples/WebApp/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace WebApp;

internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
Expand Down
3 changes: 0 additions & 3 deletions src/Pure.DI.Core/Core/AsyncDisposableSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ internal class AsyncDisposableSettings(ICache<Compilation, INamedTypeSymbol?> as
asyncDisposableTypes.Get(
compilation,
i => i.GetTypeByMetadataName(Names.IAsyncDisposableInterfaceShortName));

public bool IsEnabled(Compilation compilation) =>
TryGetAsyncDisposableType(compilation) is not null;
}
2 changes: 0 additions & 2 deletions src/Pure.DI.Core/Core/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// ReSharper disable HeapView.DelegateAllocation
namespace Pure.DI.Core;

using Microsoft.CodeAnalysis.Operations;

internal class Attributes(ISemantic semantic)
: IAttributes
{
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/Code/ClassCommenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ IReadOnlyCollection<string> CreateRootTerms(Root root)
var term = new StringBuilder();
if (root.IsPublic)
{
term.Append(formatter.FormatRef(composition.Source.Source, root.Injection.Type));
term.Append(formatter.FormatRef(root.Injection.Type));
term.Append(' ');
term.Append(formatter.FormatRef(root));
}
else
{
term.Append("Private composition root of type ");
term.Append(formatter.FormatRef(composition.Source.Source, root.Injection.Type));
term.Append(formatter.FormatRef(root.Injection.Type));
term.Append('.');
}

Expand Down Expand Up @@ -113,14 +113,14 @@ IReadOnlyCollection<string> CreateRootTerms(Root root)
IReadOnlyCollection<string> CreateRootDescriptions(Root root) =>
root.Source.Comments.Count > 0
? root.Source.Comments.Select(comments.Escape).ToList()
: [$"Provides a composition root of type {formatter.FormatRef(composition.Source.Source, root.Node.Type)}."];
: [$"Provides a composition root of type {formatter.FormatRef(root.Node.Type)}."];
}

var root = orderedRoots.FirstOrDefault(i => i.IsPublic);
if (root is not null)
{
code.AppendLine("/// <example>");
code.AppendLine($"/// This example shows how to get an instance of type {formatter.FormatRef(composition.Source.Source, root.Node.Type)} using the composition root {formatter.FormatRef(root)}:");
code.AppendLine($"/// This example shows how to get an instance of type {formatter.FormatRef(root.Node.Type)} using the composition root {formatter.FormatRef(root)}:");
code.AppendLine("/// <code>");
code.AppendLine($"/// {(composition.TotalDisposablesCount == 0 ? "" : "using ")}var composition = new {composition.Source.Source.Name.ClassName}({string.Join(", ", composition.Args.Where(i => i.Node.Arg?.Source.Kind == ArgKind.Class).Select(arg => arg.VariableDeclarationName))});");
code.AppendLine($"/// var instance = composition.{formatter.Format(root)};");
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public void Build(BuildContext ctx, in DpFactory factory)

switch (member)
{
case IFieldSymbol fieldSymbol:
case IFieldSymbol:
value = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
instance,
SyntaxFactory.IdentifierName(member.Name));
break;

case IPropertySymbol propertySymbol:
case IPropertySymbol:
value = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
instance,
Expand All @@ -97,7 +97,7 @@ public void Build(BuildContext ctx, in DpFactory factory)
// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
foreach (var typeArg in methodSymbol.TypeArguments)
{
var argType = typeConstructor.ConstructReversed(setup, binding.SemanticModel.Compilation, typeArg);
var argType = typeConstructor.ConstructReversed(typeArg);
if (binding.TypeConstructor is { } bindingTypeConstructor)
{
argType = bindingTypeConstructor.Construct(setup, binding.SemanticModel.Compilation, argType);
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public string FormatRef(string text)
return $"<see cref=\"{text}\"/>";
}

public string FormatRef(MdSetup setup, ITypeSymbol type)
public string FormatRef(ITypeSymbol type)
{
#if ROSLYN4_8_OR_GREATER
var originalDefinitionTypeName = type.OriginalDefinition.ToDisplayString(NullableFlowState.None, RefFormat);
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/IFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ internal interface IFormatter

string FormatRef(string text);

string FormatRef(MdSetup setup, ITypeSymbol type);
string FormatRef(ITypeSymbol type);
}
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/RootMethodsCommenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void AddComments(CompositionCode composition, Root root)
}
else
{
code.AppendLine($"/// Provides a composition root of type {formatter.FormatRef(composition.Source.Source, root.Node.Type)}.");
code.AppendLine($"/// Provides a composition root of type {formatter.FormatRef(root.Node.Type)}.");
}
}
finally
Expand All @@ -43,7 +43,7 @@ public void AddComments(CompositionCode composition, Root root)
}

code.AppendLine("/// <example>");
code.AppendLine($"/// This example shows how to get an instance of type {formatter.FormatRef(composition.Source.Source, root.Node.Type)}:");
code.AppendLine($"/// This example shows how to get an instance of type {formatter.FormatRef(root.Node.Type)}:");
code.AppendLine("/// <code>");
code.AppendLine($"/// {(composition.TotalDisposablesCount == 0 ? "" : "using ")}var composition = new {composition.Source.Source.Name.ClassName}({string.Join(", ", composition.Args.Where(i => i.Node.Arg?.Source.Kind == ArgKind.Class).Select(arg => arg.VariableDeclarationName))});");
code.AppendLine($"/// var instance = composition.{formatter.Format(root)};");
Expand Down
1 change: 1 addition & 0 deletions src/Pure.DI.Core/Core/DependenciesToLinesWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ public override void VisitMethod(in Unit ctx, in DpMethod method)

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

// ReSharper disable once NotDisposedResourceIsReturned
public IEnumerator<string> GetEnumerator() => _lb.GetEnumerator();
}
3 changes: 3 additions & 0 deletions src/Pure.DI.Core/Core/Generation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Pure.DI.Core;

internal readonly record struct Generation;
13 changes: 9 additions & 4 deletions src/Pure.DI.Core/Core/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ internal sealed class Generator(
IBuilder<IEnumerable<SyntaxUpdate>, IEnumerable<MdSetup>> metadataBuilder,
Func<IBuilder<MdSetup, Unit>> codeBuilderFactory,
CancellationToken cancellationToken)
: IBuilder<IEnumerable<SyntaxUpdate>, Unit>
: IBuilder<ImmutableArray<SyntaxUpdate>, Generation>
{
public Unit Build(IEnumerable<SyntaxUpdate> updates)
public Generation Build(ImmutableArray<SyntaxUpdate> updates)
{
if (updates.IsDefaultOrEmpty)
{
return new Generation();
}

if (globalOptions.TryGetProfilePath(out var profilePath))
{
profiler.Profiling(profilePath);
Expand All @@ -29,10 +34,10 @@ public Unit Build(IEnumerable<SyntaxUpdate> updates)
logObserver.OnCompleted();
}

return Unit.Shared;
return new Generation();
}

private void ProcessUpdates(IEnumerable<SyntaxUpdate> updates)
private void ProcessUpdates(ImmutableArray<SyntaxUpdate> updates)
{
foreach (var setup in metadataBuilder.Build(updates))
{
Expand Down
2 changes: 0 additions & 2 deletions src/Pure.DI.Core/Core/IAsyncDisposableSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
internal interface IAsyncDisposableSettings
{
INamedTypeSymbol? TryGetAsyncDisposableType(Compilation compilation);

bool IsEnabled(Compilation compilation);
}
2 changes: 0 additions & 2 deletions src/Pure.DI.Core/Core/IAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Pure.DI.Core;

using Microsoft.CodeAnalysis.Operations;

internal interface IAttributes
{
T GetAttribute<TMdAttribute, T>(
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/ITypeConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ internal interface ITypeConstructor

ITypeSymbol Construct(MdSetup setup, Compilation compilation, ITypeSymbol type);

ITypeSymbol ConstructReversed(MdSetup setup, Compilation compilation, ITypeSymbol type);
ITypeSymbol ConstructReversed(ITypeSymbol type);
}
1 change: 1 addition & 0 deletions src/Pure.DI.Core/Core/MetadataToLinesWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public override void VisitFinish()

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

// ReSharper disable once NotDisposedResourceIsReturned
public IEnumerator<string> GetEnumerator() => _lb.GetEnumerator();
}
1 change: 1 addition & 0 deletions src/Pure.DI.Core/Core/Models/MdGenericTypeArgument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable HeapView.ObjectAllocation
// ReSharper disable NotAccessedPositionalProperty.Global
namespace Pure.DI.Core.Models;

internal readonly record struct MdGenericTypeArgument(
Expand Down
1 change: 1 addition & 0 deletions src/Pure.DI.Core/Core/Registry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable NotAccessedPositionalProperty.Local
namespace Pure.DI.Core;

internal class Registry<T>: IRegistryManager<T>, IRegistry<T>
Expand Down
Loading

0 comments on commit 401b2f5

Please sign in to comment.