Beispiel #1
0
        /// <summary>
        /// Performs 6 common actions for all function entry points:
        /// 1) Initialises and creates an instance of
        ///    <see cref="IPersonManager" /> via the container, providing an
        ///    entry point to the logic library.
        /// 2) Deserialises the request body into a
        ///    <typeparamref name="TRequestBody" /> instance.
        /// 3) Validates the root of the deserialised
        ///    <typeparamref name="TRequestBody" /> instance.
        /// 4) Validates the rest of the <typeparamref name="TRequestBody" />
        ///    instance via <paramref name="validateModelLogic" />.
        /// 5) Wraps <paramref name="serviceInvocationLogic" /> in a try catch,
        ///    providing global error handling across all functions. In every
        ///    instance, if an unhandled/unexpected exception is thrown, then
        ///    a 500 will be returned, and the exception logged via the
        ///    provided <paramref name="loggerProvider" />.
        /// 6) Provides the <see cref="IActionResult" />.
        /// </summary>
        /// <typeparam name="TRequestBody">
        /// A type deriving from <see cref="Models.ModelsBase" />.
        /// </typeparam>
        /// <param name="httpRequest">
        /// An instance of <see cref="HttpRequest" />.
        /// </param>
        /// <param name="loggerProvider">
        /// An instance of type <see cref="ILoggerProvider" />.
        /// </param>
        /// <param name="validateModelLogic">
        /// Additional validation logic beyond the immediate route of the
        /// provided instance of <typeparamref name="TRequestBody" />.
        /// </param>
        /// <param name="serviceInvocationLogic">
        /// The service invocation logic, accepting an instance of type
        /// <see cref="IPersonManager" /> and the deserialised
        /// <typeparamref name="TRequestBody" /> instance.
        /// Returns a <see cref="HttpStatusCode" /> depending on expected
        /// outcomes.
        /// </param>
        /// <returns>
        /// An instance of <see cref="IActionResult" />.
        /// </returns>
        public static IActionResult Execute <TRequestBody>(
            HttpRequest httpRequest,
            ILoggerProvider loggerProvider,
            Func <ILoggerProvider, TRequestBody, bool> validateModelLogic,
            Func <ILoggerProvider, IPersonManager, TRequestBody, HttpStatusCode> serviceInvocationLogic)
            where TRequestBody : Models.ModelsBase
        {
            IActionResult toReturn = null;

            HttpStatusCode httpStatusCode;

            try
            {
                httpStatusCode = InvokePersonManager(
                    httpRequest,
                    loggerProvider,
                    validateModelLogic,
                    serviceInvocationLogic);
            }
            catch (Exception exception)
            {
                loggerProvider.Error(
                    "An unhandled exception was thrown.",
                    exception);

                httpStatusCode = HttpStatusCode.InternalServerError;
            }

            toReturn = new StatusCodeResult((int)httpStatusCode);

            loggerProvider.Info($"Returning {httpStatusCode}.");

            return(toReturn);
        }