예제 #1
0
        /// <summary>
        /// Gets the entity.
        /// </summary>
        /// <typeparam name="TCriteria">The type of the criteria.</typeparam>
        /// <typeparam name="TOutput">The type of the output.</typeparam>
        /// <param name="query">The query.</param>
        /// <param name="key">The key.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="resultType">Type of the result.</param>
        /// <param name="postValidation">The post validation.</param>
        /// <param name="partialViewWrapper">The partial view wrapper.</param>
        /// <param name="criteriaBoundaryEnsurance">The criteria boundary ensurance.</param>
        /// <returns></returns>
        /// <exception cref="ResourceNotFoundException">TOutput</exception>
        protected virtual ActionResult GetEntityView <TCriteria, TOutput>(Func <TCriteria, List <TOutput> > query, Guid?key, string viewName, ActionResultType resultType = ActionResultType.View, Func <TOutput, bool> postValidation = null, PartialViewWrapper partialViewWrapper = null, Action <TCriteria> criteriaBoundaryEnsurance = null)
            where TCriteria : class, IIdentifier, new()
        {
            try
            {
                query.CheckNullObject(nameof(query));
                viewName.CheckEmptyString(nameof(viewName));

                var criteria = key.HasValue ? new TCriteria {
                    Key = key
                } : null;
                if (criteria != null)
                {
                    criteriaBoundaryEnsurance?.Invoke(criteria);
                }
                var result = criteria != null?query(criteria).SafeFirstOrDefault() : default(TOutput);

                if (result == null || (postValidation != null && !postValidation(result)))
                {
                    throw new ResourceNotFoundException(nameof(TOutput), key.ToString());
                }

                if (resultType == ActionResultType.PartialView)
                {
                    return(PartialView(viewName, result));
                }
                else
                {
                    if (partialViewWrapper != null)
                    {
                        partialViewWrapper.Model       = result;
                        partialViewWrapper.PartialView = viewName;
                        return(View(partialViewWrapperView, partialViewWrapper));
                    }
                    else
                    {
                        return(View(viewName, result));
                    }
                }
            }
            catch (Exception ex)
            {
                return(HandleException(ex, resultType));
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the entity view.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="get">The get.</param>
        /// <param name="key">The key.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="inputValidator">The input validator.</param>
        /// <param name="resultType">Type of the result.</param>
        /// <param name="postValidation">The post validation.</param>
        /// <param name="considerEmptyKeyAsNew">if set to <c>true</c> [consider empty key as new].</param>
        /// <param name="partialViewWrapper">The partial view wrapper.</param>
        /// <returns></returns>
        /// <exception cref="ResourceNotFoundException">TEntity</exception>
        private ActionResult GetEntityView <TInput, TEntity>(Func <TInput, TEntity> get, TInput key, string viewName, Func <TInput, bool> inputValidator, ActionResultType resultType, Func <TEntity, bool> postValidation, bool considerEmptyKeyAsNew = false, PartialViewWrapper partialViewWrapper = null)
        {
            try
            {
                get.CheckNullObject(nameof(get));
                viewName.CheckEmptyString(nameof(viewName));

                var     hasKey = inputValidator(key);
                TEntity result = default(TEntity);

                if (hasKey)
                {
                    result = get.Invoke(key);

                    if (result == null)
                    {
                        // force to set considerEmptyKeyAsNew as false
                        // When KEY is specified and no related object found, return 404
                        considerEmptyKeyAsNew = false;
                    }
                }

                if (result != null || considerEmptyKeyAsNew)
                {
                    if (resultType == ActionResultType.PartialView)
                    {
                        return(PartialView(viewName, result));
                    }
                    else
                    {
                        if (partialViewWrapper != null)
                        {
                            partialViewWrapper.Model       = result;
                            partialViewWrapper.PartialView = viewName;
                            return(View(partialViewWrapperView, partialViewWrapper));
                        }
                        else
                        {
                            return(View(viewName, result));
                        }
                    }
                }
                else
                {
                    throw new ResourceNotFoundException(nameof(TEntity), key.ToString());
                }
            }
            catch (Exception ex)
            {
                return(HandleException(ex, resultType));
            }
        }