Skip to content

Commit 330cb45

Browse files
committed
Add initial version with documentation comments.
1 parent f09c7b1 commit 330cb45

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

SimpleChain.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34902.65
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleChain", "SimpleChain\SimpleChain.csproj", "{0F65EDD3-359B-41D0-B412-5BC4B130AA2A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0F65EDD3-359B-41D0-B412-5BC4B130AA2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0F65EDD3-359B-41D0-B412-5BC4B130AA2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0F65EDD3-359B-41D0-B412-5BC4B130AA2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0F65EDD3-359B-41D0-B412-5BC4B130AA2A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {7BFEA764-EEFF-431E-AC16-18A782EDEC9E}
24+
EndGlobalSection
25+
EndGlobal

SimpleChain/Controller.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace SimpleChain;
2+
3+
/// <inheritdoc cref="IController{TInput, TOutput}" />
4+
/// <typeparam name="TProcessor">Interface for processors to be used. Recommended to be derived from <see cref="IProcessor{T}"/>.</typeparam>
5+
public class Controller<TProcessor, TInput, TOutput> : IController<TInput, TOutput> where TProcessor : IProcessor<TInput, TOutput>
6+
{
7+
private readonly IEnumerable<TProcessor> processors;
8+
9+
public Controller(IEnumerable<TProcessor> processors) {
10+
this.processors = processors;
11+
}
12+
13+
public TOutput Run(TInput input) {
14+
return processors
15+
.First(p => p.Condition(input))
16+
.Process(input);
17+
}
18+
}

SimpleChain/IController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SimpleChain;
2+
3+
/// <summary>
4+
/// Orchestrator of the chain of responsibility.
5+
/// </summary>
6+
/// <typeparam name="TInput">Object containing inputs to processors.</typeparam>
7+
public interface IController<TInput, TOutput>
8+
{
9+
/// <summary>
10+
/// Execute the chain of processors with given input. The input is passed to each processor's
11+
/// <see cref="IProcessor{TInput, TOutput}.Condition(TInput)">Condition</see> and
12+
/// <see cref="IProcessor{TInput, TOutput}.Process(TInput)">Process</see> methods.
13+
/// </summary>
14+
/// <param name="input">Input passed to processors.</param>
15+
/// <returns>Output of processor able to handle input.</returns>
16+
TOutput Run(TInput input);
17+
}

SimpleChain/IProcessor.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace SimpleChain;
2+
3+
/// <summary>
4+
/// Base processor interface used by the library. All processors must implement this interface.
5+
/// It is recommended to create your own interfaces derived from this interface instead of using
6+
/// this interface directly.
7+
/// </summary>
8+
/// <typeparam name="TInput">Input passed to both <see cref="Condition(TInput)">Condition</see> and <see cref="Process(TInput)">Process</see>.</typeparam>
9+
/// <typeparam name="TOutput">Processor output.</typeparam>
10+
public interface IProcessor<TInput, TOutput>
11+
{
12+
/// <summary>
13+
/// Condition that determines whether processor should execute.
14+
/// </summary>
15+
/// <param name="input">Input used by the condition to determine if processor should execute.</param>
16+
/// <returns>Boolean indicating if condition should execute.</returns>
17+
public bool Condition(TInput input);
18+
19+
/// <summary>
20+
/// Executes the processor.
21+
/// </summary>
22+
/// <param name="input">Input used by the processor to create its output.</param>
23+
/// <returns>Processor output.</returns>
24+
public TOutput Process(TInput input);
25+
}

SimpleChain/SimpleChain.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)