示例#1
0
        /// <summary>
        ///     Loads the saved file.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <returns>List{`0}.</returns>
        public List <T> LoadSavedFile(SamplerOptions options = null)
        {
            var fileName = $"{Activator.CreateInstance<T>().GetType()}.json";

            var location = new StringBuilder();

            if (string.IsNullOrEmpty(_saveLocation))
            {
                location.Append(
                    $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)}\\{fileName}");
                location.Replace(@"file:\", "");
            }
            else
            {
                location.Append($"{_saveLocation}\\{fileName}");
            }

            if (!File.Exists(location.ToString()))
            {
                return(null);
            }

            var list = JsonConvert.DeserializeObject <List <T> >(File.ReadAllText(location.ToString()));

            return(list);
        }
示例#2
0
        public Func <object> SetFunc(SamplerOptions samplerOptions, PropertyInfo propertyInfo)
        {
            Func <object> method;

            if (samplerOptions != null)
            {
                KeyValuePair <string, SamplerOptions.Options> value =
                    samplerOptions.PropertyOptions.FirstOrDefault(c => c.Key == propertyInfo.Name);
                if (value.Key != null)
                {
                    method = DecideTheAction(propertyInfo.PropertyType, value.Value);
                }
                else
                {
                    method = DecideTheAction(propertyInfo.PropertyType, SamplerOptions.Options.NoOptions);
                }
            }
            else
            {
                method = DecideTheAction(propertyInfo.PropertyType, SamplerOptions.Options.NoOptions);
            }

            return(method);
        }
示例#3
0
        /// <summary>
        /// Creates the sample data.
        /// </summary>
        /// <param name="size">The size exists.</param>
        /// <param name="samplerOptions">The options.</param>
        /// <returns>Sample List.</returns>
        public static List <T> CreateSampleData(int size, SamplerOptions samplerOptions = null)
        {
            var objectList = Activator.CreateInstance <List <T> >();
            var helper     = new RandomHelper();

            for (var i = 0; i < size; i++)
            {
                var instance = CreateInstance();

                IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();
                foreach (var propertyInfo in properties)
                {
                    var method       = helper.SetFunc(samplerOptions, propertyInfo);
                    var isUnique     = false;
                    var isSequential = false;
                    var isNull       = false;;
                    if (samplerOptions != null)
                    {
                        var value = samplerOptions.PropertyOptions.FirstOrDefault(c => c.Key == propertyInfo.Name);
                        if (value.Key != null)
                        {
                            method = helper.DecideTheAction(propertyInfo.PropertyType, value.Value);
                            if (value.Value == SamplerOptions.Options.IsUnique)
                            {
                                isUnique = true;
                            }
                            else if (value.Value == SamplerOptions.Options.Sequential)
                            {
                                isSequential = true;
                            }
                            else if (value.Value == SamplerOptions.Options.NullValue)
                            {
                                isNull = true;
                            }
                        }

                        if (samplerOptions.PropertyDefaults.Any())
                        {
                            var defaultValue = samplerOptions.PropertyDefaults.FirstOrDefault(c => c.Key.PropertyName == propertyInfo.Name);

                            if (defaultValue.Key != null)
                            {
                                var @default = defaultValue.Key;
                                method = helper.DecideTheAction(propertyInfo.PropertyType, defaultValue.Value, @default);
                            }
                        }
                    }

                    if (propertyInfo.CanWrite)
                    {
                        if (method != null)
                        {
                            var value     = method.Invoke();
                            var queryable = objectList.AsQueryable();
                            if (isSequential)
                            {
                                var lastItem = queryable.OrderBy(propertyInfo.Name + " descending").FirstOrDefault();
                                if (propertyInfo.PropertyType == typeof(int))
                                {
                                    if (lastItem != null)
                                    {
                                        var props =
                                            lastItem.GetType().GetProperty(propertyInfo.Name)
                                            ?.GetValue(lastItem,
                                                       null);
                                        if (props != null)
                                        {
                                            var currentCount = int.Parse(props.ToString());
                                            value = currentCount + 1;
                                        }
                                    }
                                }
                                else if (propertyInfo.PropertyType == typeof(DateTime))
                                {
                                    if (lastItem != null)
                                    {
                                        var props = lastItem.GetType().GetProperty(propertyInfo.Name)
                                                    ?.GetValue(lastItem, null);
                                        if (props != null)
                                        {
                                            var currentDt = DateTime.Parse(props.ToString());
                                            value = currentDt.AddDays(1);
                                        }
                                    }
                                }
                            }
                            else if (isUnique)
                            {
                                object[] array = { value };
                                bool     alreadyExists;
                                alreadyExists = propertyInfo.PropertyType == typeof(Guid) ? CheckForUniqueGuid(objectList, propertyInfo, value) : queryable.Where($"{propertyInfo.Name} = @0", array).Any();
                                while (alreadyExists)
                                {
                                    value         = method.Invoke();
                                    array         = new[] { value };
                                    alreadyExists = propertyInfo.PropertyType == typeof(Guid) ? CheckForUniqueGuid(objectList, propertyInfo, value) : queryable.Where($"{propertyInfo.Name} = @0", array).Any();
                                }
                            }
                            else if (isNull)
                            {
                                value = null;
                            }

                            propertyInfo.SetValue(instance, value, null);
                        }
                        else
                        {
                            if (propertyInfo.PropertyType.BaseType == typeof(object))
                            {
                                if (propertyInfo.PropertyType.IsClass) // Not really sure if the IsClass is necessary
                                {
                                    propertyInfo.SetValue(instance, CreateSingleItem(propertyInfo.PropertyType, helper),
                                                          null);
                                }
                            }
                        }
                    }
                }

                objectList.Add((T)instance);
            }

            return(objectList);
        }