ToString() public static method

Returns a string that represents the instance. If the instance is null, this method will return "null". This method is great when the value of a property must be logged.
public static ToString ( object instance ) : string
instance object The instance, can be null.
return string
Beispiel #1
0
 public static void IsNotNull(string paramName, object paramValue)
 {
     if (paramValue == null)
     {
         Log.Error("Argument '{0}' cannot be null", ObjectToStringHelper.ToString(paramName));
         throw new ArgumentNullException(paramName);
     }
 }
Beispiel #2
0
 public static void IsNotEmpty(string paramName, Guid paramValue)
 {
     if (paramValue == Guid.Empty)
     {
         var error = string.Format("Argument '{0}' cannot be Guid.Empty", ObjectToStringHelper.ToString(paramName));
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #3
0
 public static void IsNotNullOrEmpty(string paramName, string paramValue)
 {
     if (string.IsNullOrEmpty(paramValue))
     {
         var error = string.Format("Argument '{0}' cannot be null or empty", ObjectToStringHelper.ToString(paramName));
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #4
0
 public static void IsMaximum <T>(string paramName, T paramValue, T maximumValue, Func <T, T, bool> validation)
 {
     if (!validation(paramValue, maximumValue))
     {
         var error = string.Format("Argument '{0}' should be at maximum {1}", ObjectToStringHelper.ToString(paramName), maximumValue);
         Log.Error(error);
         throw new ArgumentOutOfRangeException(paramName, error);
     }
 }
Beispiel #5
0
 public static void IsNotNullOrEmpty(string paramName, Guid?paramValue)
 {
     if (!paramValue.HasValue || paramValue.Value == Guid.Empty)
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' cannot be null or Guid.Empty";
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #6
0
 public static void IsValid <T>(string paramName, T paramValue, bool validation)
 {
     if (!validation)
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' is not valid";
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #7
0
 public static void IsNotNull(string paramName, object paramValue)
 {
     if (paramValue == null)
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' cannot be null";
         Log.Error(error);
         throw new ArgumentNullException(paramName, error);
     }
 }
Beispiel #8
0
 public static void IsNotNullOrEmptyArray(string paramName, Array paramValue)
 {
     if ((paramValue == null) || (paramValue.Length == 0))
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' cannot be null or an empty array";
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #9
0
 public static void IsNotNullOrWhitespace(string paramName, string paramValue)
 {
     if (string.IsNullOrEmpty(paramValue) || (string.CompareOrdinal(paramValue.Trim(), string.Empty) == 0))
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' cannot be null or whitespace";
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #10
0
 public static void IsNotNullOrWhitespace(string paramName, string paramValue)
 {
     if (string.IsNullOrWhiteSpace(paramValue))
     {
         var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' cannot be null or whitespace";
         Log.Error(error);
         throw new ArgumentException(error, paramName);
     }
 }
Beispiel #11
0
        public static void IsValid <T>(string paramName, T paramValue, bool validation)
        {
            Argument.IsNotNull("paramValue", paramValue);

            if (!validation)
            {
                var error = string.Format("Argument '{0}' is not valid", ObjectToStringHelper.ToString(paramName));
                Log.Error(error);
                throw new ArgumentException(error, paramName);
            }
        }
Beispiel #12
0
        public static void IsMinimal <T>(string paramName, T paramValue, T minimumValue, Func <T, T, bool> validation)
        {
            IsNotNull("validation", validation);

            if (!validation(paramValue, minimumValue))
            {
                var error = string.Format("Argument '{0}' should be minimal {1}", ObjectToStringHelper.ToString(paramName), minimumValue);
                Log.Error(error);
                throw new ArgumentOutOfRangeException(paramName, error);
            }
        }
Beispiel #13
0
        public static void IsNotOutOfRange <T>(string paramName, T paramValue, T minimumValue, T maximumValue, Func <T, T, T, bool> validation)
        {
            IsNotNull("validation", validation);

            if (!validation(paramValue, minimumValue, maximumValue))
            {
                var error = $"Argument '{ObjectToStringHelper.ToString(paramName)}' should be between {minimumValue} and {maximumValue}";
                Log.Error(error);
                throw new ArgumentOutOfRangeException(paramName, error);
            }
        }
Beispiel #14
0
        /// <summary>
        /// Runs async tasks in parallel in pre-defined batches.
        /// </summary>
        /// <param name="tasks">The task list.</param>
        /// <param name="taskName">The task name.</param>
        /// <param name="batchSize">The batch size.</param>
        public static async Task ExecuteInParallelAsync(List <Func <Task> > tasks, int batchSize = 1000, string taskName = null)
        {
            Argument.IsNotNull(nameof(tasks), tasks);

            taskName = ObjectToStringHelper.ToString(taskName);

            Log.Debug("[{0}] Executing '{1}' async tasks in parallel in batches of size '{2}'", taskName, tasks.Count, batchSize);

            for (int i = 0; i < tasks.Count; i = i + batchSize)
            {
                await TaskHelper.RunAndWaitAsync(tasks.Skip(i).Take(Math.Min(batchSize, tasks.Count - i)).ToArray());
            }
        }
Beispiel #15
0
        /// <summary>
        /// Executes all the items in the collection in parallel batches.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="items">The items.</param>
        /// <param name="actionToInvoke">The action to invoke per item.</param>
        /// <param name="itemsPerBatch">The items per batch.</param>
        /// <param name="taskName">Name of the task, can be <c>null</c>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="items" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="actionToInvoke" /> is <c>null</c>.</exception>
        public static void ExecuteInParallel <T>(List <T> items, Action <T> actionToInvoke, int itemsPerBatch = 1000, string taskName = null)
        {
            Argument.IsNotNull("items", items);
            Argument.IsNotNull("actionToInvoke", actionToInvoke);

            taskName = ObjectToStringHelper.ToString(taskName);

            Log.Debug("[{0}] Executing '{1}' actions in parallel in batches of '{2}' items per batch", taskName, items.Count, itemsPerBatch);

            var batches = new List <List <T> >();

            if (itemsPerBatch > 0)
            {
                var typeCount = items.Count;
                for (int i = 0; i < typeCount; i = i + itemsPerBatch)
                {
                    int itemsToSkip = i;
                    int itemsToTake = itemsPerBatch;
                    if (itemsToTake >= typeCount)
                    {
                        itemsToTake = typeCount - i;
                    }

                    batches.Add(items.Skip(itemsToSkip).Take(itemsToTake).ToList());
                }
            }
            else
            {
                batches.Add(new List <T>(items));
            }

            if (batches.Count == 1)
            {
                ExecuteBatch(taskName, "single", batches[0], actionToInvoke);
            }
            else
            {
                var actions = new List <Action>();
                for (var i = 0; i < batches.Count; i++)
                {
                    var innerI = i;
                    var batch  = batches[i];

                    actions.Add(() => ExecuteBatch(taskName, innerI.ToString(), batch, actionToInvoke));
                }

                TaskHelper.RunAndWait(actions.ToArray());
            }

            Log.Debug("[{0}] Executed '{1}' actions in parallel in '{2}' batches of '{3}' items per batch", taskName, items.Count, batches.Count, itemsPerBatch);
        }