Skip to content

Commit

Permalink
Add hint DisposeAsyncMethodModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 27, 2024
1 parent fd082c0 commit c733b59
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 23 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ Both approaches can be used in combination with each other.
| [ObjectResolveByTagMethodModifiers](#objectresolvebytagmethodmodifiers-hint) | Method modifier | | _public_ |
| [ObjectResolveByTagMethodName](#objectresolvebytagmethodname-hint) | Method name | | _Resolve_ |
| [DisposeMethodModifiers](#disposemethodmodifiers-hint) | Method modifier | | _public_ |
| [DisposeAsyncMethodModifiers](#disposeasyncmethodmodifiers-hint) | Method modifier | | _public_ |
| [FormatCode](#formatcode-hint) | _On_ or _Off_ | | _Off_ |
| [SeverityOfNotImplementedContract](#severityofnotimplementedcontract-hint) | _Error_ or _Warning_ or _Info_ or _Hidden_ | | _Error_ |
| [Comments](#comments-hint) | _On_ or _Off_ | | _On_ |
Expand Down Expand Up @@ -776,6 +777,10 @@ Overrides the method name for `public object Resolve(Type type, object? tag)`.

Overrides the modifiers of the `public void Dispose()` method.

### DisposeAsyncMethodModifiers Hint

Overrides the modifiers of the `public ValueTask DisposeAsync()` method.

### FormatCode Hint

Specifies whether the generated code should be formatted. This option consumes a lot of CPU resources. This hint may be useful when studying the generated code or, for example, when making presentations.
Expand Down
5 changes: 5 additions & 0 deletions readme/FooterTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Both approaches can be used in combination with each other.
| [ObjectResolveByTagMethodModifiers](#objectresolvebytagmethodmodifiers-hint) | Method modifier | | _public_ |
| [ObjectResolveByTagMethodName](#objectresolvebytagmethodname-hint) | Method name | | _Resolve_ |
| [DisposeMethodModifiers](#disposemethodmodifiers-hint) | Method modifier | | _public_ |
| [DisposeAsyncMethodModifiers](#disposeasyncmethodmodifiers-hint) | Method modifier | | _public_ |
| [FormatCode](#formatcode-hint) | _On_ or _Off_ | | _Off_ |
| [SeverityOfNotImplementedContract](#severityofnotimplementedcontract-hint) | _Error_ or _Warning_ or _Info_ or _Hidden_ | | _Error_ |
| [Comments](#comments-hint) | _On_ or _Off_ | | _On_ |
Expand Down Expand Up @@ -494,6 +495,10 @@ Overrides the method name for `public object Resolve(Type type, object? tag)`.

Overrides the modifiers of the `public void Dispose()` method.

### DisposeAsyncMethodModifiers Hint

Overrides the modifiers of the `public ValueTask DisposeAsync()` method.

### FormatCode Hint

Specifies whether the generated code should be formatted. This option consumes a lot of CPU resources. This hint may be useful when studying the generated code or, for example, when making presentations.
Expand Down
6 changes: 3 additions & 3 deletions readme/async-disposable-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ partial class Composition: global::System.IDisposable, global::System.IAsyncDisp
}
catch (Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
}
Expand Down Expand Up @@ -315,14 +315,14 @@ partial class Composition: global::System.IDisposable, global::System.IAsyncDisp
}
catch (Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
}
}
}

partial void OnAsyncDisposeException<T>(T asyncDisposableInstance, Exception exception) where T : global::System.IAsyncDisposable;
partial void OnDisposeAsyncException<T>(T asyncDisposableInstance, Exception exception) where T : global::System.IAsyncDisposable;

public override string ToString()
{
Expand Down
6 changes: 3 additions & 3 deletions readme/async-disposable-singleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ partial class Composition: global::System.IDisposable, global::System.IAsyncDisp
}
catch (Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
}
Expand Down Expand Up @@ -250,14 +250,14 @@ partial class Composition: global::System.IDisposable, global::System.IAsyncDisp
}
catch (Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
}
}
}

partial void OnAsyncDisposeException<T>(T asyncDisposableInstance, Exception exception) where T : global::System.IAsyncDisposable;
partial void OnDisposeAsyncException<T>(T asyncDisposableInstance, Exception exception) where T : global::System.IAsyncDisposable;

public override string ToString()
{
Expand Down
27 changes: 24 additions & 3 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,27 @@ internal enum Hint
/// <seealso cref="IConfiguration.Hint"/>
DisposeMethodModifiers,

/// <summary>
/// Overrides modifiers of the method <c>public <see cref="ValueTask"/> DisposeAsyncMethodModifiers()</c>. "public" by default.
/// </summary>
/// <example>
/// <code>
/// // DisposeAsyncMethodModifiers = internal
/// DI.Setup("Composition")
/// .Bind&lt;IDependency&gt;().To&lt;Dependency&gt;();
/// </code>
/// <br/>
/// or using the API call <see cref="IConfiguration.Hint"/>:
/// <code>
/// DI.Setup("Composition")
/// .Hint(Hint.DisposeAsyncMethodModifiers, "internal")
/// .Bind&lt;IDependency&gt;().To&lt;Dependency&gt;();
/// </code>
/// </example>
/// </summary>
/// <seealso cref="IConfiguration.Hint"/>
DisposeAsyncMethodModifiers,

/// <summary>
/// <c>On</c> or <c>Off</c>. Specifies whether the generated code should be formatted. This option consumes a lot of CPU resources. <c>Off</c> by default.
/// </summary>
Expand Down Expand Up @@ -1152,7 +1173,7 @@ public void Dispose()
}
catch (global::System.Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
#endif
Expand All @@ -1176,7 +1197,7 @@ public void Dispose()
}
catch (global::System.Exception exception)
{
OnAsyncDisposeException(asyncDisposableInstance, exception);
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;

Expand Down Expand Up @@ -1212,7 +1233,7 @@ partial void OnDisposeException<T>(T disposableInstance, Exception exception)
/// <param name="asynDisposableInstance">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 OnAsyncDisposeException<T>(T asynDisposableInstance, Exception exception)
partial void OnDisposeAsyncException<T>(T asynDisposableInstance, Exception exception)
where T : global::System.IAsyncDisposable;
#endif

Expand Down
12 changes: 6 additions & 6 deletions src/Pure.DI.Core/Core/Code/DisposeMethodBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine();
}

AddAsyncDisposePart(composition, code, false);
AddDisposeAsyncPart(composition, code, false);
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine("/// </summary>");
}

code.AppendLine($"{composition.Source.Source.Hints.DisposeMethodModifiers} async {Names.ValueTaskName} DisposeAsync()");
code.AppendLine($"{composition.Source.Source.Hints.DisposeAsyncMethodModifiers} async {Names.ValueTaskName} DisposeAsync()");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -100,7 +100,7 @@ public CompositionCode Build(CompositionCode composition)
{
if (hasAsyncDisposable)
{
AddAsyncDisposePart(composition, code, true);
AddDisposeAsyncPart(composition, code, true);
}

if (hasDisposable)
Expand Down Expand Up @@ -129,14 +129,14 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine("/// <param name=\"asyncDisposableInstance\">The disposable instance.</param>");
code.AppendLine("/// <param name=\"exception\">Exception occurring during disposal.</param>");
code.AppendLine("/// <typeparam name=\"T\">The actual type of instance being disposed of.</typeparam>");
code.AppendLine($"partial void {Names.OnAsyncDisposeExceptionMethodName}<T>(T asyncDisposableInstance, Exception exception) where T : {Names.IAsyncDisposableInterfaceName};");
code.AppendLine($"partial void {Names.OnDisposeAsyncExceptionMethodName}<T>(T asyncDisposableInstance, Exception exception) where T : {Names.IAsyncDisposableInterfaceName};");
membersCounter++;
}

return composition with { MembersCount = membersCounter };
}

private static void AddAsyncDisposePart(CompositionCode composition, LinesBuilder code, bool makeAsyncCall)
private static void AddDisposeAsyncPart(CompositionCode composition, LinesBuilder code, bool makeAsyncCall)
{
code.AppendLine($"case {Names.IAsyncDisposableInterfaceName} asyncDisposableInstance:");
using (code.Indent())
Expand Down Expand Up @@ -167,7 +167,7 @@ private static void AddAsyncDisposePart(CompositionCode composition, LinesBuilde
code.AppendLine("{");
using (code.Indent())
{
code.AppendLine($"{Names.OnAsyncDisposeExceptionMethodName}(asyncDisposableInstance, exception);");
code.AppendLine($"{Names.OnDisposeAsyncExceptionMethodName}(asyncDisposableInstance, exception);");
}

code.AppendLine("}");
Expand Down
3 changes: 3 additions & 0 deletions src/Pure.DI.Core/Core/Hints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal sealed class Hints : Dictionary<Hint, string>, IHints

public string DisposeMethodModifiers =>
GetValueOrDefault(Hint.DisposeMethodModifiers, Names.DefaultApiMethodModifiers);

public string DisposeAsyncMethodModifiers =>
GetValueOrDefault(Hint.DisposeAsyncMethodModifiers, Names.DefaultApiMethodModifiers);

public DiagnosticSeverity SeverityOfNotImplementedContract =>
GetHint(Hint.SeverityOfNotImplementedContract, DiagnosticSeverity.Error);
Expand Down
2 changes: 2 additions & 0 deletions src/Pure.DI.Core/Core/IHints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ internal interface IHints : IReadOnlyDictionary<Hint, string>

string DisposeMethodModifiers { get; }

string DisposeAsyncMethodModifiers { get; }

DiagnosticSeverity SeverityOfNotImplementedContract { get; }
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Names.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static class Names
// Partial methods
public const string OnNewInstanceMethodName = "OnNewInstance";
public const string OnDisposeExceptionMethodName = "OnDisposeException";
public const string OnAsyncDisposeExceptionMethodName = "OnAsyncDisposeException";
public const string OnDisposeAsyncExceptionMethodName = "OnDisposeAsyncException";
public const string OnDependencyInjectionMethodName = "OnDependencyInjection";
public const string OnCannotResolve = "OnCannotResolve";
public const string OnNewRootMethodName = "OnNewRoot";
Expand Down
14 changes: 7 additions & 7 deletions tests/Pure.DI.IntegrationTests/LifetimesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void Dispose()
public ValueTask DisposeAsync()
{
Console.WriteLine("AsyncDispose3");
Console.WriteLine("DisposeAsync3");
return ValueTask.CompletedTask;
}
}
Expand Down Expand Up @@ -611,7 +611,7 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "AsyncDispose3", "Dispose2", "Dispose3", "Dispose2"], result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "DisposeAsync3", "Dispose2", "Dispose3", "Dispose2"], result);
}

[Fact]
Expand Down Expand Up @@ -674,7 +674,7 @@ public Dependency3(IDependency1 dep1, IDependency2 dep2)
public ValueTask DisposeAsync()
{
Console.WriteLine("AsyncDispose3");
Console.WriteLine("DisposeAsync3");
return ValueTask.CompletedTask;
}
}
Expand Down Expand Up @@ -740,7 +740,7 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "AsyncDispose3", "Dispose2", "AsyncDispose3", "Dispose2"], result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "DisposeAsync3", "Dispose2", "DisposeAsync3", "Dispose2"], result);
}

[Fact]
Expand Down Expand Up @@ -777,7 +777,7 @@ public Dependency2(IDependency1 dep1)
public ValueTask DisposeAsync()
{
Console.WriteLine("AsyncDispose2");
Console.WriteLine("DisposeAsync2");
return ValueTask.CompletedTask;
}
}
Expand All @@ -804,7 +804,7 @@ public Dependency3(IDependency1 dep1, IDependency2 dep2)
public ValueTask DisposeAsync()
{
Console.WriteLine("AsyncDispose3");
Console.WriteLine("DisposeAsync3");
return ValueTask.CompletedTask;
}
}
Expand Down Expand Up @@ -870,7 +870,7 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "AsyncDispose3", "AsyncDispose2", "AsyncDispose3", "AsyncDispose2"], result);
result.StdOut.ShouldBe(["Dependency2", "Dependency3", "Dependency2", "Dependency3", "DisposeAsync3", "DisposeAsync2", "DisposeAsync3", "DisposeAsync2"], result);
}

[Fact]
Expand Down

0 comments on commit c733b59

Please sign in to comment.