Пример #1
0
        private static List <T> SelectInternal <T>(string connectstring, string query, SqlParameter[] parameters = null, bool isStoredProdedure = false)
        {
            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException("query", "query cannot be null or empty.");
            }
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Query Begin:{0}", query);
            }
            SqlConnection connection = new SqlConnection(connectstring);
            SqlCommand    cmd        = new SqlCommand(query, connection);

            cmd.CommandTimeout = commandTimeout;
            cmd.CommandType    = isStoredProdedure ? CommandType.StoredProcedure : CommandType.Text;
            if (parameters != null && parameters.Length > 0)
            {
                cmd.Parameters.AddRange(parameters);
                LogSqlParameters(parameters);
            }
            List <T>           list     = new List <T>();
            DynamicBuilder <T> _builder = null;

            connection.Open();
            using (SqlDataReader sqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                Type t = typeof(T);
                while (sqlDataReader.Read())
                {
                    if (t.IsValueType || t == typeof(string) || t == typeof(byte[]))
                    {
                        list.Add((T)Convert.ChangeType(sqlDataReader[0], typeof(T)));
                    }
                    else
                    {
                        if (_builder == null)
                        {
                            _builder = DynamicBuilderCache.ResolveDynamicBuilder <T>(sqlDataReader);
                        }

                        T entity = _builder.BuildEntity(sqlDataReader);

                        if (entity != null)
                        {
                            list.Add(entity);
                        }
                    }
                }
                cmd.Cancel();
            }
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Query End:{0}", query);
            }
            return(list);
        }
        public static DynamicBuilder <T> ResolveDynamicBuilder <T>(SqlDataReader sqlDataReader)
        {
            string cacheKey = string.Empty;

            cacheKey = ResolveDynamicBuilderCacheKeyInternal(sqlDataReader, typeof(T));
            DynamicBuilder <T> builder = null;

            if (dynamicBuilderCache.ContainsKey(cacheKey))
            {
                builder = dynamicBuilderCache[cacheKey] as DynamicBuilder <T>;
            }
            if (builder == null)
            {
                builder = DynamicBuilder <T> .CreateEntityBuilder(sqlDataReader);

                dynamicBuilderCache[cacheKey] = builder;
            }
            return(builder);
        }
Пример #3
0
        public static DynamicBuilder <T> CreateEntityBuilder(IDataRecord dataRecord)
        {
            Type t = typeof(T);
            DynamicBuilder <T> dynamicBuilder = new DynamicBuilder <T>();

            try
            {
                DynamicMethod method = new DynamicMethod("DynamicModelCreate", typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true);

                ILGenerator generator = method.GetILGenerator();

                LocalBuilder result = generator.DeclareLocal(typeof(T));

                generator.Emit(OpCodes.Newobj, t.GetConstructor(Type.EmptyTypes));

                generator.Emit(OpCodes.Stloc, result);
                var properties = t.GetProperties();
                for (int i = 0; i < dataRecord.FieldCount; i++)
                {
                    var          temp         = properties.Where(n => n.Name.ToLower() == dataRecord.GetName(i).ToLower()).ToList();
                    PropertyInfo propertyInfo = null;

                    if (temp.Count() >= 1)
                    {
                        propertyInfo = temp[0];
                    }

                    LocalBuilder il_P = generator.DeclareLocal(typeof(PropertyInfo));

                    Label endIfLabel = generator.DefineLabel();
                    if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
                    {
                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                        generator.Emit(OpCodes.Brtrue, endIfLabel);

                        generator.Emit(OpCodes.Ldloc, result);
                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        generator.Emit(OpCodes.Callvirt, getValueMethod);

                        switch (propertyInfo.PropertyType.Name)
                        {
                        case "Int16":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToInt16", new Type[] { typeof(object) }));
                            break;

                        case "Int32":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToInt32", new Type[] { typeof(object) }));
                            break;

                        case "Int64":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToInt64", new Type[] { typeof(object) }));
                            break;

                        case "Boolean":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToBoolean", new Type[] { typeof(object) }));
                            break;

                        case "String":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToString", new Type[] { typeof(object) }));
                            break;

                        case "DateTime":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToDateTime", new Type[] { typeof(object) }));
                            break;

                        case "Decimal":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToDecimal", new Type[] { typeof(object) }));
                            break;

                        case "Byte":
                            generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToByte", new Type[] { typeof(object) }));
                            break;

                        case "Nullable`1":
                        {
                            if (propertyInfo.PropertyType == typeof(DateTime?))
                            {
                                generator.Emit(OpCodes.Call, CreateConverterMethodInfo("ToDateTimeNull"));
                            }
                            else if (propertyInfo.PropertyType == typeof(Int32?))
                            {
                                generator.Emit(OpCodes.Call, CreateConverterMethodInfo("ToInt32Null"));
                            }
                            else if (propertyInfo.PropertyType == typeof(Boolean?))
                            {
                                generator.Emit(OpCodes.Call, CreateConverterMethodInfo("ToBooleanNull"));
                            }
                            break;
                        }

                        default:
                            generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i));
                            break;
                        }

                        generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());

                        generator.MarkLabel(endIfLabel);
                    }
                }

                generator.Emit(OpCodes.Ldloc, result);
                generator.Emit(OpCodes.Ret);


                dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(dynamicBuilder);
        }