/// <summary> /// publish the specified message and log whether the publish was successful or not. /// </summary> /// <typeparam name="TEvent"></typeparam> /// <param name="checks">Specified properties will be checked for null value before calling the message constructor</param> /// <param name="messageConstructor">Sets values on an instance of the message</param> /// <param name="apprenticeship"></param> /// <param name="message">A log message that will be recorded with the success or failure message</param> /// <returns></returns> private async Task PublishWithLog <TEvent>(ApprenticePreChecks checks, Apprenticeship apprenticeship, Action <TEvent> messageConstructor, string message) where TEvent : class { var logMessage = $"Publish {typeof(TEvent).Name} message. {message}"; try { DoPreChecks <TEvent>(checks, apprenticeship); await _endpointInstance.Publish <TEvent>(messageConstructor); _logger.Info($"{logMessage} successful"); } catch (Exception e) { _logger.Error(e, $"{logMessage} failed"); throw; } }
private void DoPreChecks <TEvent>(ApprenticePreChecks checks, Apprenticeship apprenticeship) where TEvent : class { void DoCheckIf(ApprenticePreChecks qualifyingFlag, Action check) { if ((checks & qualifyingFlag) == qualifyingFlag) { check(); } } if (checks == ApprenticePreChecks.NotRequired) { return; } EnsureHasApprenticeship <TEvent>(apprenticeship); DoCheckIf(ApprenticePreChecks.HasStartDate, () => EnsureHasStartDate <TEvent>(apprenticeship)); DoCheckIf(ApprenticePreChecks.HasEndDate, () => EnsureHasEndDate <TEvent>(apprenticeship)); DoCheckIf(ApprenticePreChecks.HasStopDate, () => EnsureHasStopDate <TEvent>(apprenticeship)); DoCheckIf(ApprenticePreChecks.HasPauseDate, () => EnsureHasPauseDate <TEvent>(apprenticeship)); }