/// <summary>
        ///     A DataRow extension method that converts the @this to the entities.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="dr">The @this to act on.</param>
        /// <returns>@this as a T.</returns>
        public static T ToEntity <T>([NotNull] this DataRow dr)
        {
            var type       = typeof(T);
            var properties = CacheUtil.GetTypeProperties(type).Where(p => p.CanWrite).ToArray();

            var entity = NewFuncHelper <T> .Instance();

            if (type.IsValueType)
            {
                var obj = (object)entity;
                foreach (var property in properties)
                {
                    if (dr.Table.Columns.Contains(property.Name))
                    {
                        property.GetValueSetter()?.Invoke(obj, dr[property.Name].GetValueFromDbValue());
                    }
                }
                entity = (T)obj;
            }
            else
            {
                foreach (var property in properties)
                {
                    if (dr.Table.Columns.Contains(property.Name))
                    {
                        property.GetValueSetter()?.Invoke(entity, dr[property.Name].GetValueFromDbValue());
                    }
                }
            }

            return(entity);
        }
        /// <summary>
        ///     An IDataReader extension method that converts the @this to an entity.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="this">The @this to act on.</param>
        /// <param name="hadRead">whether the DataReader had read</param>
        /// <returns>@this as a T.</returns>
        public static T ToEntity <T>([NotNull] this IDataReader @this, bool hadRead = false)
        {
            if (!hadRead)
            {
                hadRead = @this.Read();
            }

            if (hadRead && @this.FieldCount > 0)
            {
                var type = typeof(T);
                if (type.IsBasicType())
                {
                    return(@this[0].ToOrDefault <T>());
                }

                var properties = CacheUtil.GetTypeProperties(type).Where(p => p.CanWrite).ToArray();

                var entity = NewFuncHelper <T> .Instance();

                var dic = Enumerable.Range(0, @this.FieldCount)
                          .ToDictionary(_ => @this.GetName(_).ToUpper(), _ => @this[_].GetValueFromDbValue());
                try
                {
                    if (type.IsValueType)
                    {
                        var obj = (object)entity;
                        foreach (var property in properties)
                        {
                            if (dic.ContainsKey(property.Name.ToUpper()))
                            {
                                property.GetValueSetter()?.Invoke(obj, dic[property.Name.ToUpper()]);
                            }
                        }
                        entity = (T)obj;
                    }
                    else
                    {
                        foreach (var property in properties)
                        {
                            if (dic.ContainsKey(property.Name.ToUpper()))
                            {
                                property.GetValueSetter()?.Invoke(entity, dic[property.Name.ToUpper()]);
                            }
                        }
                    }

                    return(entity);
                }
                catch (Exception e)
                {
                    Common.Helpers.LogHelper.GetLogger(typeof(DataExtension)).Error(e);
                    throw;
                }
            }

            return(default);
Example #3
0
 public static IInterceptionConfiguration With <TInterceptor>(this IInterceptionConfiguration interceptionConfiguration, params object?[] parameters) where TInterceptor : IInterceptor
 {
     if (Guard.NotNull(parameters, nameof(parameters)).Length == 0)
     {
         interceptionConfiguration.With(NewFuncHelper <TInterceptor> .Instance());
     }
     else
     {
         interceptionConfiguration.With(ActivatorHelper.CreateInstance <TInterceptor>(parameters));
     }
     return(interceptionConfiguration);
 }
Example #4
0
        /// <summary>
        /// convert csv file data to entity list
        /// </summary>
        /// <param name="csvBytes">csv bytes</param>
        public static List <TEntity> ToEntityList <TEntity>(byte[] csvBytes)
        {
            if (null == csvBytes)
            {
                throw new ArgumentNullException(nameof(csvBytes));
            }
            var entities = new List <TEntity>();

            if (typeof(TEntity).IsBasicType())
            {
                using (var ms = new MemoryStream(csvBytes))
                {
                    using (var sr = new StreamReader(ms, Encoding.UTF8))
                    {
                        string strLine;
                        var    isFirstLine = true;
                        while ((strLine = sr.ReadLine()).IsNotNullOrEmpty())
                        {
                            if (isFirstLine)
                            {
                                isFirstLine = false;
                                continue;
                            }
                            //
                            Debug.Assert(strLine != null, nameof(strLine) + " is null");
                            entities.Add(strLine.Trim().To <TEntity>());
                        }
                    }
                }
            }
            else
            {
                var configuration            = InternalHelper.GetExcelConfigurationMapping <TEntity>();
                var propertyColumnDictionary = InternalHelper.GetPropertyColumnDictionary <TEntity>();
                var propertyColumnDic        = propertyColumnDictionary.ToDictionary(_ => _.Key, _ => new PropertyConfiguration()
                {
                    ColumnIndex     = -1,
                    ColumnFormatter = _.Value.ColumnFormatter,
                    ColumnTitle     = _.Value.ColumnTitle,
                    ColumnWidth     = _.Value.ColumnWidth,
                    IsIgnored       = _.Value.IsIgnored
                });
                using (var ms = new MemoryStream(csvBytes))
                {
                    using (var sr = new StreamReader(ms, Encoding.UTF8))
                    {
                        string strLine;
                        var    isFirstLine = true;
                        while ((strLine = sr.ReadLine()).IsNotNullOrEmpty())
                        {
                            var entityType = typeof(TEntity);
                            var cols       = ParseLine(strLine);
                            if (isFirstLine)
                            {
                                for (var index = 0; index < cols.Count; index++)
                                {
                                    var setting = propertyColumnDic.GetPropertySetting(cols[index]);
                                    if (setting != null)
                                    {
                                        setting.ColumnIndex = index;
                                    }
                                }

                                if (propertyColumnDic.Values.All(_ => _.ColumnIndex < 0))
                                {
                                    propertyColumnDic = propertyColumnDictionary;
                                }

                                isFirstLine = false;
                            }
                            else
                            {
                                var entity = NewFuncHelper <TEntity> .Instance();

                                if (entityType.IsValueType)
                                {
                                    var obj = (object)entity;// boxing for value types

                                    foreach (var key in propertyColumnDic.Keys)
                                    {
                                        var colIndex = propertyColumnDic[key].ColumnIndex;
                                        if (colIndex >= 0 && colIndex < cols.Count && key.CanWrite)
                                        {
                                            var columnValue  = key.PropertyType.GetDefaultValue();
                                            var valueApplied = false;
                                            if (InternalCache.ColumnInputFormatterFuncCache.TryGetValue(key, out var formatterFunc) && formatterFunc?.Method != null)
                                            {
                                                var cellValue = cols[colIndex];
                                                try
                                                {
                                                    // apply custom formatterFunc
                                                    columnValue  = formatterFunc.DynamicInvoke(cellValue);
                                                    valueApplied = true;
                                                }
                                                catch (Exception e)
                                                {
                                                    Debug.WriteLine(e);
                                                    InvokeHelper.OnInvokeException?.Invoke(e);
                                                }
                                            }
                                            if (valueApplied == false)
                                            {
                                                columnValue = cols[colIndex].ToOrDefault(key.PropertyType);
                                            }

                                            key.GetValueSetter()?.Invoke(entity, columnValue);
                                        }
                                    }

                                    entity = (TEntity)obj;// unboxing
                                }
                                else
                                {
                                    foreach (var key in propertyColumnDic.Keys)
                                    {
                                        var colIndex = propertyColumnDic[key].ColumnIndex;
                                        if (colIndex >= 0 && colIndex < cols.Count && key.CanWrite)
                                        {
                                            var columnValue = key.PropertyType.GetDefaultValue();

                                            var valueApplied = false;
                                            if (InternalCache.ColumnInputFormatterFuncCache.TryGetValue(key, out var formatterFunc) && formatterFunc?.Method != null)
                                            {
                                                var cellValue = cols[colIndex];
                                                try
                                                {
                                                    // apply custom formatterFunc
                                                    columnValue  = formatterFunc.DynamicInvoke(cellValue);
                                                    valueApplied = true;
                                                }
                                                catch (Exception e)
                                                {
                                                    Debug.WriteLine(e);
                                                    InvokeHelper.OnInvokeException?.Invoke(e);
                                                }
                                            }
                                            if (valueApplied == false)
                                            {
                                                columnValue = cols[colIndex].ToOrDefault(key.PropertyType);
                                            }

                                            key.GetValueSetter()?.Invoke(entity, columnValue);
                                        }
                                    }
                                }

                                if (null != entity)
                                {
                                    foreach (var propertyInfo in propertyColumnDic.Keys)
                                    {
                                        if (propertyInfo.CanWrite)
                                        {
                                            var propertyValue = propertyInfo.GetValueGetter()?.Invoke(entity);
                                            if (InternalCache.InputFormatterFuncCache.TryGetValue(propertyInfo, out var formatterFunc) && formatterFunc?.Method != null)
                                            {
                                                try
                                                {
                                                    // apply custom formatterFunc
                                                    var formattedValue = formatterFunc.DynamicInvoke(entity, propertyValue);
                                                    propertyInfo.GetValueSetter()?.Invoke(entity, formattedValue);
                                                }
                                                catch (Exception e)
                                                {
                                                    Debug.WriteLine(e);
                                                    InvokeHelper.OnInvokeException?.Invoke(e);
                                                }
                                            }
                                        }
                                    }
                                }

                                if (configuration.DataValidationFunc != null && !configuration.DataValidationFunc(entity))
                                {
                                    // data invalid
                                    continue;
                                }
                                entities.Add(entity);
                            }
                        }
                    }
                }
            }

            return(entities);
        }