I'm trying to create an application sample that can call a function / method and after a certain time it checks whether the process has already been executed, if not, call the same function / method again and check the previous progress if it is running or if it is finished and return the process.
I've tried a lot, but I have not found an example that can be followed. The links below describe what Design Patterns - Idempotency Messages actually does.
I'm trying this way;
using System;
namespace Idempotent_Messages
{
public class Program
{
static IdePotencyHelper idePotencyHelper = new IdePotencyHelper();
public static int passo = 1;
static void Main(string[] args)
{
var func = new Funcionario();
var command = new CqrsBase<Int32, Funcionario>();
command.Execute(func);
command.IdPotency((f) =>
{
var funcBanco = new Funcionario();
funcBanco.Id = 1;
return f.Id == funcBanco.Id;
},
(x)=> { return x.Id; }
, func);
}
}
public class Funcionario
{
public int Id { get; set; }
}
public class CqrsBase<T, TArgs>
{
public T Execute(TArgs args)
{
return default(T);
}
}
public static class CqrsBase
{
public static TReturn IdPotency<TReturn, TArgs>(this CqrsBase<TReturn, TArgs> cqrs, Func<TArgs,bool> verify, Func<TArgs, TReturn> existsFunc, TArgs args)
{
var existe = verify(args);
if (existe)
{
return existsFunc(args);
}
else
{
return cqrs.Execute(args);
}
}
}
}
Design Patterns - Idempotency
An operation is idempotent if it does not have additional effects if it is called more than once with the same parameters. You can not control how your public API is called by your clients, so you should make sure that it does not lead to any unwanted effects if they repeat your calls over and over again.
A common solution is to provide a correlation ID for any operation potentially changed by the state. The service checks in some repository if the request has been processed. If yes, the previously stored response must be provided.
Here is a flowchart that shows communication with a correlation ID:
Image copied from site
The problem is that I do not know how to create an identifier for each process to store its status and after the determiner time check the process by the identifier.