/// <summary>
        /// Set.
        /// </summary>
        /// <param name="context">The context to set.</param>
        public void Set(ExaminationValidationContext context)
        {
            if (_context != null)
            {
                throw new InvalidOperationException("Examination validation context has already been set.");
            }

            _context = context;
        }
        /// <inheritdoc/>
        protected override ValidationResult IsValid(
            object value,
            ExaminationValidationContext examinationValidationContext,
            ValidationContext validationContext)
        {
            if (examinationValidationContext.Examination == null)
            {
                return(new ValidationResult($"The examination was not present to confirm role validation."));
            }

            var userRetrievalByIdService =
                (IAsyncQueryHandler <UserRetrievalByIdQuery, MedicalExaminer.Models.MeUser>)
                validationContext.GetService(
                    typeof(IAsyncQueryHandler <UserRetrievalByIdQuery, MedicalExaminer.Models.MeUser>));

            if (userRetrievalByIdService == null)
            {
                throw new InvalidOperationException("The user retrieval by id service has not been registered.");
            }

            var userId = value as string;

            if (string.IsNullOrEmpty(userId))
            {
                return(ValidationResult.Success);
            }

            var user = userRetrievalByIdService.Handle(new UserRetrievalByIdQuery(userId)).Result;

            if (user == null)
            {
                return(new ValidationResult($"The user with id `{userId}` was not found."));
            }

            var hasRole = user.HasRoleForExamination(examinationValidationContext.Examination, _requiredRole);

            if (!hasRole)
            {
                return(new ValidationResult($"The user is not a `{_requiredRole.GetDescription()}`."));
            }

            return(ValidationResult.Success);
        }
 /// <summary>
 /// Is Valid.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <param name="examinationValidationContext">Examination Validation Context.</param>
 /// <param name="validationContext">Validation Context.</param>
 /// <returns><see cref="ValidationResult"/></returns>
 protected abstract ValidationResult IsValid(
     object value,
     ExaminationValidationContext examinationValidationContext,
     ValidationContext validationContext);