|
| 1 | +using AutSoft.Common.Exceptions; |
| 2 | +using AutSoft.Common.Time; |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | + |
| 8 | +using Stateless; |
| 9 | + |
| 10 | +using System.Linq.Expressions; |
| 11 | + |
| 12 | +namespace AutSoft.Common.StateMachines; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Abstract base class of state machines, which add functionalities for Stateless StateMachine class |
| 16 | +/// </summary> |
| 17 | +/// <typeparam name="TState">Type of state machine's states</typeparam> |
| 18 | +/// <typeparam name="TTrigger">Type of state machine's triggers</typeparam> |
| 19 | +/// <typeparam name="TEntity">The type of entity whose state is described by the state machine</typeparam> |
| 20 | +public abstract class EntityStateMachineBase<TState, TTrigger, TEntity> : StateMachine<TState, TTrigger> |
| 21 | + where TState : struct, Enum |
| 22 | + where TTrigger : Enum |
| 23 | + where TEntity : class |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="EntityStateMachineBase{TState, TTrigger, TEntity}"/> class. |
| 27 | + /// </summary> |
| 28 | + /// <param name="entity">The entity whose state is described by the state machine</param> |
| 29 | + /// <param name="statePropertySelector">The state property of the entity described by the state machine</param> |
| 30 | + /// <param name="stateModifiedDatePropertySelector">The property of entity, which contains the time of the last change of state</param> |
| 31 | + /// <param name="timeProvider">An instance of <see cref="ITimeProvider"/></param> |
| 32 | + /// <param name="dbContext">An instance of <see cref="DbContext"/></param> |
| 33 | + /// <param name="exceptionMessage">Message of exception</param> |
| 34 | + /// <exception cref="ArgumentNullException">This exception is thrown if the added properties aren't available</exception> |
| 35 | + /// <exception cref="BusinessException">This exception is thrown if a state transition does not permitted</exception> |
| 36 | + protected EntityStateMachineBase( |
| 37 | + TEntity entity, |
| 38 | + Expression<Func<TEntity, TState>> statePropertySelector, |
| 39 | + Expression<Func<TEntity, DateTimeOffset>>? stateModifiedDatePropertySelector = null, |
| 40 | + ITimeProvider? timeProvider = null, |
| 41 | + DbContext? dbContext = null, |
| 42 | + string? exceptionMessage = null) |
| 43 | + : base( |
| 44 | + stateAccessor: () => (TState)statePropertySelector.GetPropertyAccess().GetValue(entity)!, |
| 45 | + stateMutator: state => statePropertySelector.GetPropertyAccess().SetValue(entity, state)) |
| 46 | + { |
| 47 | + if (stateModifiedDatePropertySelector != null && timeProvider == null) |
| 48 | + throw new ArgumentNullException(nameof(timeProvider), $"If {nameof(stateModifiedDatePropertySelector)} is specified, the {nameof(timeProvider)} parameter cannot be null"); |
| 49 | + |
| 50 | + if (stateModifiedDatePropertySelector != null && dbContext == null) |
| 51 | + throw new ArgumentNullException(nameof(timeProvider), $"If {nameof(stateModifiedDatePropertySelector)} is specified, the {nameof(dbContext)} parameter cannot be null"); |
| 52 | + |
| 53 | + OnTransitionedAsync(async _ => |
| 54 | + { |
| 55 | + stateModifiedDatePropertySelector?.GetPropertyAccess().SetValue(entity, timeProvider?.Now); |
| 56 | + |
| 57 | + if (dbContext != null) |
| 58 | + await dbContext.SaveChangesAsync(); |
| 59 | + }); |
| 60 | + |
| 61 | + OnUnhandledTrigger((s, t) => |
| 62 | + { |
| 63 | + var id = entity.GetType().GetProperty("Id")?.GetValue(entity); |
| 64 | + throw new BusinessException( |
| 65 | + exceptionMessage ?? $"The desired state transition is not allowed! (entity: {entity.GetType().Name} id: {id} state: {s} trigger: {t})", "Incorrect operation!"); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gives that an trigger can be fired with specified arguments or not |
| 71 | + /// </summary> |
| 72 | + /// <param name="trigger">The trigger object</param> |
| 73 | + /// <param name="arguments">The specified arguments</param> |
| 74 | + /// <returns>The trigger can be fired with specified arguments or not</returns> |
| 75 | + public bool CanFire(TTrigger trigger, params object[] arguments) |
| 76 | + { |
| 77 | + if (arguments == null || arguments.Length == 0) |
| 78 | + return base.CanFire(trigger); |
| 79 | + |
| 80 | + return GetPermittedTriggers(arguments).Any(x => Equals(x, trigger)); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Base abstract class of state machine factories |
| 85 | + /// </summary> |
| 86 | + /// <typeparam name="TStateMachine">Type of state machine to be created</typeparam> |
| 87 | + public abstract class FactoryBase<TStateMachine> |
| 88 | + where TStateMachine : EntityStateMachineBase<TState, TTrigger, TEntity> |
| 89 | + { |
| 90 | + private readonly IServiceProvider _serviceProvider; |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Initializes a new instance of the <see cref="FactoryBase{TStateMachine}"/> class |
| 94 | + /// </summary> |
| 95 | + /// <param name="serviceProvider">Instance of an IServiceProvider</param> |
| 96 | + protected FactoryBase(IServiceProvider serviceProvider) |
| 97 | + { |
| 98 | + _serviceProvider = serviceProvider; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Create a state machine with specified type |
| 103 | + /// </summary> |
| 104 | + /// <param name="entity">The entity whose state is described by the state machine</param> |
| 105 | + /// <returns>The created state machine</returns> |
| 106 | + public TStateMachine CreateStateMachine(TEntity entity) |
| 107 | + { |
| 108 | + return ActivatorUtilities.CreateInstance<TStateMachine>(_serviceProvider, entity); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments