-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from deveel/sample-console-app
Sample Console Applications
- Loading branch information
Showing
7 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Deveel.Pipelines; | ||
|
||
var pipeline = new MathBuilder() | ||
.Use<AddHandler>() | ||
.Use<MultiplyHandler>() | ||
.Use<DivideHandler>() | ||
.Use<SubtractHandler>() | ||
.Build(); | ||
|
||
int? number; | ||
Console.WriteLine("Please provide me a number: "); | ||
while (true) { | ||
if (int.TryParse(Console.ReadLine(), out var value)) { | ||
number = value; | ||
break; | ||
} else { | ||
Console.WriteLine("Invalid number, please try again: "); | ||
} | ||
} | ||
|
||
var context = new MathContext { | ||
Number = number.Value | ||
}; | ||
|
||
await pipeline.ExecuteAsync(context); | ||
|
||
Console.WriteLine($"The result is: {context.Number}"); | ||
|
||
|
||
class MathBuilder : PipelineBuilder<MathContext> { | ||
public MathBuilder Use<THandler>() where THandler : class { | ||
AddStep<THandler>(); | ||
return this; | ||
} | ||
|
||
public Pipeline<MathContext> Build() { | ||
return BuildPipeline(new PipelineBuildContext()); | ||
} | ||
} | ||
|
||
class MathContext : PipelineExecutionContext { | ||
public int Number { get; set; } | ||
} | ||
|
||
abstract class MathHandler { | ||
protected abstract int Execute(int number, int value); | ||
|
||
public Task HandleAsync(MathContext context) { | ||
Console.Out.WriteLine($"Executing {GetType().Name} with {context.Number}"); | ||
|
||
context.Number = Execute(context.Number, context.Number); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class AddHandler : MathHandler { | ||
protected override int Execute(int number, int value) { | ||
return number + value; | ||
} | ||
} | ||
|
||
class MultiplyHandler : MathHandler { | ||
protected override int Execute(int number, int value) { | ||
return number * value; | ||
} | ||
} | ||
|
||
class SubtractHandler : MathHandler { | ||
protected override int Execute(int number, int value) { | ||
return number - value; | ||
} | ||
} | ||
|
||
class DivideHandler : MathHandler { | ||
protected override int Execute(int number, int value) { | ||
return number / value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Deveel.Pipelines\Deveel.Pipelines.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Deveel.Pipelines; | ||
|
||
using Humanizer; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
var services = new ServiceCollection() | ||
.AddSingleton<INameNornalizer, DefaultNameNormalizer>() | ||
.AddMyPipeline(p => p | ||
.Use<AskName>() | ||
.Use<NormalizeName>() | ||
.Use<SayHello>()) | ||
.BuildServiceProvider(); | ||
|
||
var pipeline = services.GetRequiredService<Pipeline<MyContext>>(); | ||
|
||
await pipeline.ExecuteAsync(); | ||
|
||
|
||
class MyPipelineBuilder : PipelineBuilder<MyContext> { | ||
public MyPipelineBuilder Use<THandler>() where THandler : class { | ||
AddStep<THandler>(); | ||
return this; | ||
} | ||
|
||
public Pipeline<MyContext> Build(IServiceProvider services) | ||
=> BuildPipeline(new PipelineBuildContext(services)); | ||
} | ||
|
||
class MyContext : PipelineExecutionContext { | ||
public string? Name { get; set; } | ||
} | ||
|
||
class AskName { | ||
public Task HandleAsync(MyContext context, ExecutionDelegate<MyContext> next) { | ||
Console.WriteLine("What's your name?"); | ||
context.Name = Console.ReadLine(); | ||
// TODO: invoke next -- currently it stops the pipeline after next | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class SayHello { | ||
public void Handle(MyContext context) { | ||
Console.WriteLine($"Hello, {context.Name}"); | ||
} | ||
} | ||
|
||
class NormalizeName { | ||
private readonly INameNornalizer nameNornalizer; | ||
|
||
public NormalizeName(INameNornalizer nameNornalizer) { | ||
this.nameNornalizer = nameNornalizer; | ||
} | ||
|
||
public async Task HandleAsync(MyContext context, ExecutionDelegate<MyContext> next) { | ||
context.Name = await nameNornalizer.NormalizeNameAsync(context.Name); | ||
} | ||
} | ||
|
||
static class ServiceCollectionExtensions { | ||
public static IServiceCollection AddMyPipeline(this IServiceCollection services, Action<MyPipelineBuilder> configure) { | ||
services.AddSingleton<Pipeline<MyContext>>(provider => { | ||
var builder = new MyPipelineBuilder(); | ||
configure(builder); | ||
return builder.Build(provider); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
|
||
interface INameNornalizer { | ||
Task<string> NormalizeNameAsync(string? name); | ||
} | ||
|
||
class DefaultNameNormalizer : INameNornalizer { | ||
public Task<string> NormalizeNameAsync(string? name) | ||
=> Task.FromResult(name.Pascalize()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Humanizer" Version="2.14.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Deveel.Pipelines\Deveel.Pipelines.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters