Ejemplo n.º 1
0
        public static BoolResult <TEntity> RefToSingle <TEntity>(this DataBase db, string strSql, params IDataParameter[] parameters) where TEntity : class, new()
        {
            ValidationResults validationResults = new ValidationResults();
            TEntity           item       = default(TEntity);
            IDataReader       dataReader = null;

            try
            {
                dataReader = db.ExecuteReader(db.ConnectionString, CommandType.Text, strSql, parameters);
                DynamicBuilder <TEntity> dynamicBuilder = DynamicBuilder <TEntity> .CreateBuilder(dataReader);

                if (dataReader.Read())
                {
                    item = dynamicBuilder.Build(dataReader);
                }
            }
            catch (Exception ex)
            {
                validationResults.Add(ex.Message);
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
            return(new BoolResult <TEntity>(item, validationResults.IsValid, string.Empty, validationResults));
        }
Ejemplo n.º 2
0
        public static BoolResult <IList <TEntity> > RefSPToList <TEntity>(this DataBase db, string spName, params IDataParameter[] parameters) where TEntity : class, new()
        {
            ValidationResults validationResults = new ValidationResults();
            IList <TEntity>   list       = new List <TEntity>();
            IDataReader       dataReader = null;

            try
            {
                dataReader = db.ExecuteSPReader(db.ConnectionString, spName, parameters);
                DynamicBuilder <TEntity> dynamicBuilder = DynamicBuilder <TEntity> .CreateBuilder(dataReader);

                while (dataReader.Read())
                {
                    TEntity item = dynamicBuilder.Build(dataReader);
                    list.Add(item);
                }
            }
            catch (Exception ex)
            {
                validationResults.Add(ex.Message);
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
            return(new BoolResult <IList <TEntity> >(list, validationResults.IsValid, string.Empty, validationResults));
        }
Ejemplo n.º 3
0
        public static DynamicBuilder <T> CreateBuilder(IDataRecord dataRecord)
        {
            Type typeFromHandle = typeof(T);
            DynamicBuilder <T> dynamicBuilder = new DynamicBuilder <T>();
            DynamicMethod      dynamicMethod  = new DynamicMethod("DynamicCreate", typeFromHandle, new Type[]
            {
                typeof(IDataRecord)
            }, typeFromHandle, true);
            ILGenerator  iLGenerator = dynamicMethod.GetILGenerator();
            LocalBuilder local       = iLGenerator.DeclareLocal(typeFromHandle);

            iLGenerator.Emit(OpCodes.Newobj, typeFromHandle.GetConstructor(Type.EmptyTypes));
            iLGenerator.Emit(OpCodes.Stloc, local);
            for (int i = 0; i < dataRecord.FieldCount; i++)
            {
                PropertyInfo property = typeFromHandle.GetProperty(dataRecord.GetName(i));
                Label        label    = iLGenerator.DefineLabel();
                if (property != null && property.GetSetMethod() != null)
                {
                    iLGenerator.Emit(OpCodes.Ldarg_0);
                    iLGenerator.Emit(OpCodes.Ldc_I4, i);
                    iLGenerator.Emit(OpCodes.Callvirt, DynamicBuilder <T> .isDBNullMethod);
                    iLGenerator.Emit(OpCodes.Brtrue, label);
                    iLGenerator.Emit(OpCodes.Ldloc, local);
                    iLGenerator.Emit(OpCodes.Ldarg_0);
                    iLGenerator.Emit(OpCodes.Ldc_I4, i);
                    iLGenerator.Emit(OpCodes.Callvirt, DynamicBuilder <T> .getValueMethod);
                    bool flag = false;
                    if (property.PropertyType.Name.ToLower().Contains("nullable"))
                    {
                        flag = true;
                    }
                    Type fieldType = dataRecord.GetFieldType(i);
                    if (flag)
                    {
                        iLGenerator.Emit(OpCodes.Unbox_Any, DynamicBuilder <T> .types[fieldType]);
                    }
                    else
                    {
                        iLGenerator.Emit(OpCodes.Unbox_Any, fieldType);
                    }
                    iLGenerator.Emit(OpCodes.Callvirt, property.GetSetMethod());
                    iLGenerator.MarkLabel(label);
                }
            }
            iLGenerator.Emit(OpCodes.Ldloc, local);
            iLGenerator.Emit(OpCodes.Ret);
            dynamicBuilder.handler = (DynamicBuilder <T> .Load)dynamicMethod.CreateDelegate(typeof(DynamicBuilder <T> .Load));
            return(dynamicBuilder);
        }