Example #1
0
        /// <summary>
        /// Performs the action.
        /// </summary>
        /// <param name="ctx">The actioncontext.</param>
        /// <param name="action">Delegate to call to perform the action on the model.</param>
        /// <param name="actionName">Name of the action.</param>
        protected virtual BoolResult <T> PerformAction(IActionContext ctx, Action <IActionContext> action, EntityAction entityAction)
        {
            string entityName = ctx.Item == null ? " with id : " + ctx.Id : ctx.Item.GetType().FullName;

            try
            {
                // Step 1: Authenticate and check security.
                BoolResult <T> result = PerformAuthentication(ctx);
                if (!result.Success)
                {
                    return(new BoolResult <T>(default(T), false, result.Message, ctx.Errors, ctx.Messages));
                }


                // Step 2: Massage Data before validation.
                ApplyMassageOnAuditData(ctx, entityAction);
                PerformBeforeValidation(ctx, entityAction);

                // Step 3: Validate the entity.
                result = PerformValidation(ctx, entityAction);
                if (!result.Success)
                {
                    return(result);
                }

                // Step 4: Massage data after validation.
                PerformAfterValidation(ctx, entityAction);

                // Step 5: Now persist the entity.
                action(ctx);

                // Build the message.
                string message = "Successfully " + entityAction.ToString() + entityName;
                return(new BoolResult <T>(default(T), true, message, ctx.Errors, ctx.Messages));
            }
            catch (Exception ex)
            {
                string error = "Unable to : " + entityAction.ToString() + " entity : " + entityName;
                ErrorManager.Handle(error, ex, ctx.Errors);
                return(new BoolResult <T>(default(T), false, error, ctx.Errors, ctx.Messages));
            }
        }
Example #2
0
        /// <summary>
        /// Performs the action.
        /// </summary>
        /// <param name="ctx">The actioncontext.</param>
        /// <param name="action">Delegate to call to perform the action on the model.</param>
        /// <param name="entityAction">Name of the action.</param>
        protected virtual BoolResult <T> PerformAction(IActionContext ctx, Action <IActionContext> action, EntityAction entityAction)
        {
            string entityName = ctx.Item == null ? " with id : " + ctx.Id : ctx.Item.GetType().FullName;

            try
            {
                // Step 1: Authenticate and check security.
                BoolResult <T> result = PerformAuthentication(ctx, entityAction);
                if (!result.Success)
                {
                    return(new BoolResult <T>(default(T), false, result.Message, ctx.Errors));
                }

                // Step 2: Massage Data before validation.
                if (ctx.Item != null)
                {
                    IEntity entity = ctx.Item as IEntity;
                    Entities.Extensions.EntityExtensions.AuditAll(entity);
                }

                // Step 3: Now persist the entity.
                action(ctx);

                // Build the message.
                string message = "Successfully " + entityAction.ToString() + entityName;
                if (!ctx.Errors.IsValid)
                {
                    message = "Unable to " + entityAction.ToString() + typeof(T).Name;
                }

                return(new BoolResult <T>((T)ctx.Item, ctx.Errors.IsValid, message, ctx.Errors));
            }
            catch (Exception ex)
            {
                string error = "Unable to : " + entityAction.ToString() + " entity : " + entityName;
                ErrorManager.Handle(error, ex, ctx.Errors);
                return(new BoolResult <T>(default(T), false, error, ctx.Errors));
            }
        }