Skip to content

Commit

Permalink
Improved templates
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 11, 2024
1 parent 8c083f9 commit a8d5f92
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;

namespace _PureDIProjectName_;

public class ConsoleAdapter: IInput, IOutput
{
public string? ReadLine() => Console.ReadLine();
public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default) =>
Console.In.ReadLineAsync(cancellationToken);

public void WriteLine(string? line) => Console.WriteLine(line);
public Task WriteLineAsync(string line = "", CancellationToken cancellationToken = default) =>
Console.Out.WriteLineAsync(line.AsMemory(), cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using System;
global using System.Diagnostics;
global using System.Threading;
global using System.Threading.Tasks;
global using Pure.DI;
global using static Pure.DI.Lifetime;
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IInput
{
string? ReadLine();
ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace _PureDIProjectName_;

public interface IOutput
{
void WriteLine(string? line);
Task WriteLineAsync(string line = "", CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System.Diagnostics;
using Pure.DI;
using static Pure.DI.Lifetime;
namespace _PureDIProjectName_;

namespace _PureDIProjectName_;

/// <summary>
/// Pure.DI Composition Setup. Please see <see href="https://github.com/DevTeam/Pure.DI.Solution">this</see> example.
/// </summary>
internal class $(CompositionName)
{
[Conditional("DI")]
void Setup() =>
private void Setup() =>
DI.Setup(nameof($(CompositionName)), CompositionKind.Global)
.Bind().As(Singleton).To<ConsoleAdapter>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace _PureDIProjectName_;

internal class ConsoleAdapter: IInput, IOutput
{
public string? ReadLine() => Console.ReadLine();
public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default) =>
Console.In.ReadLineAsync(cancellationToken);

public void WriteLine(string? line) => Console.WriteLine(line);
public Task WriteLineAsync(string line = "", CancellationToken cancellationToken = default) =>
Console.Out.WriteLineAsync(line.AsMemory(), cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
global using _PureDIProjectName_;
global using System;
global using System.Diagnostics;
global using System.Diagnostics;
global using System.Threading;
global using System.Threading.Tasks;
global using Pure.DI;
global using static Pure.DI.Lifetime;
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

internal interface IInput
{
string? ReadLine();
ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace _PureDIProjectName_;

internal interface IOutput
{
void WriteLine(string? line);
Task WriteLineAsync(string line = "", CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
var composition = new $(CompositionName)();
return composition.Root.Run(args);
var composition = new $(CompositionName)(args);
return await composition.Root.RunAsync();

internal partial class Program
internal partial class Program(
string[] args,
IInput input,
IOutput output)
{
private readonly IInput _input;
private readonly IOutput _output;

internal Program(IInput input, IOutput output)
{
_input = input ?? throw new ArgumentNullException(nameof(input));
_output = output ?? throw new ArgumentNullException(nameof(output));
}

private int Run(string[] args)
private async Task<int> RunAsync(CancellationToken cancellationToken = default)
{
_output.WriteLine("Hello!");
await output.WriteLineAsync("Hello!", cancellationToken);
await output.WriteLineAsync(string.Join(", ", args), cancellationToken);

_output.WriteLine("Press the Enter key to exit.");
_input.ReadLine();
await output.WriteLineAsync("Press the Enter key to exit.", cancellationToken);
await input.ReadLineAsync(cancellationToken);

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Pure.DI;
using static Pure.DI.Lifetime;
namespace _PureDIProjectName_;

namespace _PureDIProjectName_;

/// <summary>
/// Pure.DI Composition Setup. Please see <see href="https://github.com/DevTeam/Pure.DI.Solution">this</see> example.
/// </summary>
internal partial class $(CompositionName)
{
[Conditional("DI")]
void Setup() =>
private void Setup() =>
DI.Setup(nameof($(CompositionName)))
.Arg<string[]>("args")
.Bind().As(Singleton).To<ConsoleAdapter>()
.Root<Program>("Root");
}

0 comments on commit a8d5f92

Please sign in to comment.