Nagornev.Graph
1.0.2
dotnet add package Nagornev.Graph --version 1.0.2
NuGet\Install-Package Nagornev.Graph -Version 1.0.2
<PackageReference Include="Nagornev.Graph" Version="1.0.2" />
<PackageVersion Include="Nagornev.Graph" Version="1.0.2" />
<PackageReference Include="Nagornev.Graph" />
paket add Nagornev.Graph --version 1.0.2
#r "nuget: Nagornev.Graph, 1.0.2"
#:package Nagornev.Graph@1.0.2
#addin nuget:?package=Nagornev.Graph&version=1.0.2
#tool nuget:?package=Nagornev.Graph&version=1.0.2
Nagornev.Graph
Information
This library creates a node graph to manage a complex business process. The principle of graph creation is based on the "Chain of responsibility" pattern. If you know this pattern, then you can easily work with this library.
Installation
Install the current version with dotnet:
dotnet add package Nagornev.Graph
Usage
Quick start
Use this code to write console lines:
using Nagornev.Graph.Commands;
using Nagornev.Graph.Invokers;
using Nagornev.Graph.Nodes;
AutomaticInvoker invoker = new AutomaticInvoker();
//Init
ConsistentNode startNode = new ConsistentNode(() =>
FunctionalConsistentCommand.Create(async () =>
{
Console.WriteLine("Handling start node.");
}));
ConditionNode middleNode = new ConditionNode(() =>
FunctionalConditionCommand.Create(async () =>
{
Random random = new Random();
int number = random.Next(10);
bool response = number % 2 == 0;
Console.WriteLine($"Handling middle node. Response {response}.");
return response;
}));
ConsistentNode firstEndNode = new ConsistentNode(() =>
FunctionalConsistentCommand.Create(async () =>
{
Console.WriteLine("Handling first end node.");
}));
ConsistentNode secondEndNode = new ConsistentNode(() =>
FunctionalConsistentCommand.Create(async () =>
{
Console.WriteLine("Handling second end node.");
}));
//Connecting
startNode.SetSuccessor(middleNode);
middleNode.SetSuccessor(true, firstEndNode);
middleNode.SetSuccessor(false, secondEndNode);
//Invoke
invoker.SetStart(startNode);
await invoker.Start();
How to use it?
This library has 3 main components:
*Node- used for connecting between nodes;*Command- used to set up the next handling node;*Invoker- used to invoke the node handling;
Nodes:
This library has 4 main nodes:
ConsistentNode- has one successor. Command type isConsistentCommand;ConditionNode- has two successors (true/false). Command type isConditionCommand;ConvertibleNode<T>- has an unknown number of successors (the number of successors depends on the typeT). Command type isConvertibleCommand<T>;ExceptionNode- has one successor. Also this node can repeat parent node (the parent node is the node where catched unhandled exception). Command type isExceptionCommand;
Each of the 3 types of main nodes (ConsistentNode, ConditionNode,
ConvertibleNode<T>) has additional nodes:
Oneway*Node- the command is executed once. After executing the command, the node will return the successor it returned the first time;Interval*Node- the command is executed after skipping the set call interval;Delayed*Node- the command is executed after a period of time;
Each node has methods:
SetSuccessor(Node successor) / SetSuccessor(TConvertibleType key, Node successor)- sets the node successor;SetExceptionSuccessor(ExceptionNode successor)- sets the exception node successor;SetDelay(int delay)- sets the delay after execution;SetSingle(bool single)- sets the parameter that controls the command creation;SetTag(string tag)- sets the tag that used the node handling;
Exception node has specific methods:
-SetRepeat(int count) - sets the repetition count of the parent node;
Commands:
This library has 4 main commands:
ConsistentCommand- the execution method has the signature:Func<Task>;ConditionCommand- the execution method has the signature:Func<Task<bool>>;ConvertibleCommand<T>- the execution method has the signature:Func<Task<T>>,where T: struct, IConvertible;ExceptionCommand- the execution method has the signature:Func<Task>;
Each main commands has additional commands:
Functional*Command- the constructor accept the * delegate;Callback*Command<T>- the command has a special methodT Call(), which invoke the acepted callback;
Invokers
This library has 2 main invokers:
AutomaticInvokerHandInvoker
To start handling nodes, you need call method SetStart(Node node) and call method Task Start() (AutomaticInvoker) or call method Task Execute() (HandInvoker).
Sample
Let's calculate and get the factorial of the number:
public class FactorialCommand : CallbackConsistentCommand<int>
{
private readonly int _number;
private int _factorial;
public FactorialCommand(int number,
params CommandCallbackDelegate<int>[] callbacks)
: base(callbacks)
{
_number = number;
}
protected override async Task Execute()
{
_factorial = Factorial(_number);
}
private int Factorial(int number)
{
if (number == 1) return 1;
return number * Factorial(number - 1);
}
protected override int Call()
{
return _factorial;
}
}
Let's create a graph and call it for handling.
using Nagornev.Graph.Commands;
using Nagornev.Graph.Invokers;
using Nagornev.Graph.Nodes;
int number = int.Parse(Console.ReadLine());
int result = 0;
AutomaticInvoker invoker = new AutomaticInvoker();
//Init
ConsistentNode calculateFactorialNode = new ConsistentNode(() =>
new FactorialCommand(number,
(factorial) => result = factorial));
ConsistentNode printFactorialNode = new ConsistentNode(() =>
FunctionalConsistentCommand.Create(async () =>
{
Console.WriteLine($"Factorial {number} is {result}.");
}));
//Connecting
calculateFactorialNode.SetSuccessor(printFactorialNode);
//Invoke
invoker.SetStart(calculateFactorialNode);
await invoker.Start();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|