Esempio n. 1
0
 /// <summary>
 /// Retrieves all the instances of the model from the repository
 /// </summary>
 /// <returns></returns>
 public virtual BoolResult <IList <T> > GetAll(IActionContext ctx)
 {
     try
     {
         IList <T> items = _repository.GetAll();
         return(new BoolResult <IList <T> >(items, true, string.Empty, ctx.Errors, ctx.Messages));
     }
     catch (Exception ex)
     {
         string error = "Unable to retrieve all items of type " + typeof(T).Name;
         ErrorManager.Handle(error, ex, ctx.Errors);
         return(new BoolResult <IList <T> >(new List <T>(), false, error, ctx.Errors, ctx.Messages));
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Retrieves all the instances of the model from the repository
 /// </summary>
 /// <returns></returns>
 public virtual BoolResult <IList> GetAllItems(IActionContext ctx)
 {
     try
     {
         IList items = _repository.GetAllItems();
         return(new BoolResult <IList>(items, true, string.Empty, ValidationResults.Empty, StatusResults.Empty));
     }
     catch (Exception ex)
     {
         string error = "Unable to retrieve all items of type " + typeof(T).Name;
         ErrorManager.Handle(error, ex, ctx.Errors);
         return(new BoolResult <IList>(new ArrayList(), false, error, ValidationResults.Empty, StatusResults.Empty));
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Retrieves the model from the repository.
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public virtual BoolResult <T> Get(IActionContext ctx)
 {
     try
     {
         T item = _repository.Get(ctx.Id);
         return(new BoolResult <T>(item, true, string.Empty, ctx.Errors, ctx.Messages));
     }
     catch (Exception ex)
     {
         string error = "Unable to retrieve " + typeof(T).Name + " with : " + ctx.Id;
         ErrorManager.Handle(error, ex, ctx.Errors);
         return(new BoolResult <T>(default(T), false, error, ctx.Errors, ctx.Messages));
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Retrieves all the instances of the model from the repository
        /// </summary>
        /// <returns></returns>
        public virtual IList GetAllItems(IActionContext ctx)
        {
            IList items = null;

            try
            {
                items = _repository.GetAllItems();
            }
            catch (Exception ex)
            {
                string error = "Unable to retrieve all items of type " + typeof(T).Name;
                ErrorManager.Handle(error, ex, ctx.Errors);
            }
            return(items);
        }
Esempio n. 5
0
        /// <summary>
        /// Retrieves the model from the repository.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public virtual T Get(IActionContext ctx)
        {
            T item = default(T);

            try
            {
                item = _repository.Get(ctx.Id);
            }
            catch (Exception ex)
            {
                string error = "Unable to retrieve " + typeof(T).Name + " with : " + ctx.Id;
                ErrorManager.Handle(error, ex, ctx.Errors);
            }
            return(item);
        }
Esempio n. 6
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));
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Show the default exception handling.
        /// This can be configured to use an email notifier as
        /// the exception handling provider instance.
        /// </summary>
        public void DoExceptionHandling()
        {
            ErrorManager.Register("myErrorManager", false, new CustomExceptionHandler("myErrorManager"));
            Logger.Info("====================================================");
            Logger.Info("EXCEPTION HANDLING ");

            try
            {
                throw new ArgumentException("exception handling testing");
            }
            catch (Exception ex)
            {
                // Default error handler.
                ErrorManager.Handle("Default error handling uses the Logger static class", ex);

                // Custom named error handler "myErrorManager"
                ErrorManager.Handle("Example with custom NAMED error handler.", ex, "myErrorManager");
            }
            Logger.Info(Environment.NewLine);
        }
Esempio n. 8
0
        /// <summary>
        /// Run the application.
        /// </summary>
        public override BoolMessageItem Execute()
        {
            ErrorManager.Register("myErrorManager", false, new CustomExceptionHandler("myErrorManager"));
            Console.WriteLine("====================================================");
            Console.WriteLine("EXCEPTION HANDLING ");

            try
            {
                throw new ArgumentException("exception handling testing");
            }
            catch (Exception ex)
            {
                // Option 1. Use default error handler.
                ErrorManager.Handle("Default error handling.", ex);

                // Option 2. Use custom named error handler "myErrorManager"
                ErrorManager.Handle("Example with custom NAMED error handler.", ex, "myErrorManager");
            }
            Console.WriteLine(Environment.NewLine);
            return(BoolMessageItem.True);
        }
Esempio n. 9
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));
            }
        }
Esempio n. 10
0
 private void WhenHandleIsCalledWithException(Exception ex)
 {
     ThenResponse = _someErrorManager.Handle(ex);
 }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strName">参数名</param>
        /// <param name="type">传递的话就要验证 不传递就不验证</param>
        /// <returns></returns>
        public static T GetFromString <T>(string strName, DataType type)
        {
            string obj = "";

            if ("".Equals(GetStringByQueryString(strName)))
            {
                obj = GetFormString(strName);
            }
            else
            {
                obj = GetStringByQueryString(strName);
            }
            if (obj != "" && type != null)
            {
                #region "判断类型"
                switch (type)
                {
                case DataType.Int:
                    int IntTempValue = 0;
                    if (!int.TryParse(obj, out IntTempValue))
                    {
                        ErrorManager.Handle("输入数据格式验证失败" + string.Format("{0}字段值:{1}数据类型必需为Int型!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(IntTempValue, typeof(T)));

                case DataType.Dat:
                    DateTime DateTempValue = DateTime.MinValue;
                    if (!DateTime.TryParse(obj, out DateTempValue))
                    {
                        ErrorManager.Handle("输入数据格式验证失败" + string.Format("{0}字段值:{1}数据类型必需为时间型!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(DateTempValue, typeof(T)));

                case DataType.Long:
                    long LongTempValue = long.MinValue;
                    if (!long.TryParse(obj, out LongTempValue))
                    {
                        ErrorManager.Handle("输入数据格式验证失败:" + string.Format("{0}字段值:{1}数据类型必需为Long型!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(LongTempValue, typeof(T)));

                case DataType.Double:
                    double DoubleTempValue = double.MinValue;
                    if (!double.TryParse(obj, out DoubleTempValue))
                    {
                        ErrorManager.Handle("输入数据格式验证失败:" + string.Format("{0}字段值:{1}数据类型必需为Double型!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(DoubleTempValue, typeof(T)));

                case DataType.CharAndNum:
                    if (!CheckRegEx(obj, "^[A-Za-z0-9]+$"))
                    {
                        ErrorManager.Handle("输入数据格式验证失败:" + string.Format("{0}字段值:{1}数据类型必需为英文或数字!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(strName, typeof(T)));

                case DataType.CharAndNumAndChinese:
                    if (!CheckRegEx(obj, "^[A-Za-z0-9\u00A1-\u2999\u3001-\uFFFD]+$"))
                    {
                        ErrorManager.Handle("输入数据格式验证失败:" + string.Format("{0}字段值:{1}数据类型必需为英文或数字或中文!", strName, obj), null);
                    }
                    return((T)Convert.ChangeType(strName, typeof(T)));

                case DataType.Email:
                    if (!CheckRegEx(obj, "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
                    {
                        ComLib.Exceptions.ErrorManager.Handle("输入数据格式验证失败", new Exception("googog"));
                    }
                    ErrorManager.Handle("输入数据格式验证失败:" + string.Format("{0}字段值:{1}数据类型必需为邮件地址!", strName, obj), null);
                    return((T)Convert.ChangeType(obj, typeof(T)));

                default:
                    return((T)Convert.ChangeType(obj, typeof(T)));;
                }
                #endregion
            }

            if (obj == "")
            {
                return(default(T));
            }
            return(obj.ConvertTo <T>());
        }