Add GetAllTransitionDefinitions and GetAllowedTransitions methods to IWorkflow.cs

This commit is contained in:
Shkar T. Noori 2024-04-28 11:17:50 +03:00
parent bb52ee16d5
commit f99bd9b15c
No known key found for this signature in database
GPG Key ID: C5E1A00F3BB78732
3 changed files with 62 additions and 0 deletions

View File

@ -10,6 +10,15 @@ public interface IWorkflow<TState, TCommand, TContext>
string Reference => $"{Id}.v{Version}"; string Reference => $"{Id}.v{Version}";
/// <summary>
/// Retrieves all transition definitions for the workflow.
/// </summary>
/// <typeparam name="TState">The type of the workflow state.</typeparam>
/// <typeparam name="TCommand">The type of the workflow command.</typeparam>
/// <typeparam name="TContext">The type of the workflow context.</typeparam>
/// <returns>A list of transition definitions.</returns>
public List<TransitionDefinition<TState, TCommand, TContext>> GetAllTransitionDefinitions();
/// <summary> /// <summary>
/// Gets a list of allowed transitions without any condition checks. /// Gets a list of allowed transitions without any condition checks.
/// </summary> /// </summary>
@ -24,4 +33,13 @@ public interface IWorkflow<TState, TCommand, TContext>
/// <param name="from">The incoming state</param> /// <param name="from">The incoming state</param>
/// <returns>A list of available transitions for the current context</returns> /// <returns>A list of available transitions for the current context</returns>
public List<Transition<TState, TCommand>> GetAllowedTransitions(TContext context, TState from); public List<Transition<TState, TCommand>> GetAllowedTransitions(TContext context, TState from);
/// <summary>
/// Retrieves a list of allowed transitions based on the provided request.
/// </summary>
/// <typeparam name="TState">The type of the workflow state.</typeparam>
/// <typeparam name="TCommand">The type of the workflow command.</typeparam>
/// <param name="request">The request object containing the necessary information.</param>
/// <returns>An enumerable of allowed transitions.</returns>
public IEnumerable<Transition<TState, TCommand>> GetAllowedTransitions(ListTransitionsRequest<TState, TCommand, TContext> request);
} }

View File

@ -0,0 +1,13 @@
namespace DIT.Workflower;
public sealed class ListTransitionsRequest<TState, TCommand, TContext>
{
public TState? From { get; set; }
public TState? To { get; set; }
public TCommand? Command { get; set; }
public TContext? Context { get; set; }
}

View File

@ -12,6 +12,8 @@ public record WorkflowDefinition<TState, TCommand, TContext>
_transitions = transitions; _transitions = transitions;
} }
public List<TransitionDefinition<TState, TCommand, TContext>> GetAllTransitionDefinitions() => _transitions;
/// <summary> /// <summary>
/// Lists all allowed transitions from current state for the given context. /// Lists all allowed transitions from current state for the given context.
/// </summary> /// </summary>
@ -33,4 +35,33 @@ public record WorkflowDefinition<TState, TCommand, TContext>
return query.Select(x => x.ToTransition()).ToList(); return query.Select(x => x.ToTransition()).ToList();
} }
public IEnumerable<Transition<TState, TCommand>> GetAllowedTransitions(ListTransitionsRequest<TState, TCommand, TContext> request)
{
var query = _transitions.AsQueryable();
if (request.From is TState from)
{
query = query.Where(doc => doc.From.Equals(from));
}
if (request.To is TState to)
{
query = query.Where(doc => doc.To.Equals(to));
}
if (request.Command is TCommand command)
{
query = query.Where(doc => doc.Command.Equals(command));
}
if (request.Context is not null)
{
query = query.Where(doc => doc.Conditions == null || !doc.Conditions.Any(cond => !cond(request.Context)));
}
return query.Select(x => x.ToTransition());
}
} }