protected IBaseServiceResponse LogError(IBaseServiceResponse response, string message)
 {
     response.IsSuccessful = false;
     response.Message      = message;
     LogError(response.Message);
     return(response);
 }
 protected IBaseServiceResponse LogInformation(IBaseServiceResponse response, string message)
 {
     response.IsSuccessful = true;
     response.Message      = message;
     LogInformation(response.Message);
     return(response);
 }
Exemple #3
0
 /// <summary>
 /// HandleError
 /// </summary>
 /// <param name="ex"></param>
 /// <param name="response"></param>
 protected void HandleError(Exception ex,IBaseServiceResponse response)
 {
     string errorCode=string.Empty;
     if (ex.GetType() == typeof(BusinessException))
     {
         errorCode = (ex as BusinessException).Code;
         response.Status = ResponseStatus.BusinessException;
     }
     else
     {
         response.Status = ResponseStatus.Exception;
         // log error
         log.Error(ex.Message,ex);
     }
     response.ResponseMessage.MessageCode= errorCode;
     response.ResponseMessage.Message=ex.Message;
 }
Exemple #4
0
        /// <summary>
        /// HandleError
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="response"></param>
        protected void HandleError(Exception ex, IBaseServiceResponse response)
        {
            string errorCode = string.Empty;

            if (ex.GetType() == typeof(BusinessException))
            {
                errorCode       = (ex as BusinessException).Code;
                response.Status = ResponseStatus.BusinessException;
            }
            else
            {
                response.Status = ResponseStatus.Exception;
                // log error
                log.Error(ex.Message, ex);
            }
            response.ResponseMessage.MessageCode = errorCode;
            response.ResponseMessage.Message     = ex.Message;
        }
Exemple #5
0
        public static string ToJson(this IBaseServiceResponse response, JsonSerializationOptions options = null)
        {
            var _options = new JsonSerializationOptions(options);
            var result   = new StringBuilder();

            if (response != null)
            {
                foreach (var prop in response.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    if (prop.CanRead)
                    {
                        var propType = prop.PropertyType;
                        var value    = prop.GetValue(response);

                        if (value != null)
                        {
                            var cmdParam = value as CommandParameter;
                            if (cmdParam != null)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name, cmdParam.ToJson());
                                continue;
                            }
                            if (propType.IsEnum)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value.ToString());
                                continue;
                            }
                            if (propType == TypeHelper.TypeOfBool)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name,
                                                                       SafeClrConvert.ToBoolean(value) ? "true" : "false");
                                continue;
                            }
                            if (propType == TypeHelper.TypeOfString)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value);
                                continue;
                            }
                            if (propType == TypeHelper.TypeOfChar)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": '{1}'", prop.Name, value);
                                continue;
                            }
                            if (propType.IsBasicType())
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name, value);
                                continue;
                            }
                            if (value is Guid)
                            {
                                if ((Guid)value != Guid.Empty)
                                {
                                    result.AppendFormatWithCommaIfNotEmpty("\"{0}\":'{1}'", prop.Name, value);
                                    continue;
                                }
                                else
                                {
                                    result.AppendFormatWithCommaIfNotEmpty("\"{0}\":null", prop.Name);
                                    continue;
                                }
                            }
                            var jm = value as IJsonSerializable;

                            if (jm != null)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", prop.Name, jm.ToJson(_options));
                                continue;
                            }

                            var e = value as IEnumerable;
                            if (e != null)
                            {
                                result.AppendFormatWithCommaIfNotEmpty("\"{0}\": ", prop.Name);
                                result.Append(e.ToJson(_options));
                                continue;
                            }
                            result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value.ToString());
                        }
                        else
                        {
                            result.AppendFormatWithCommaIfNotEmpty("\"{0}\": null", prop.Name);
                        }
                    }
                }
            }

            return("{" + result + "}");
        }