Exemplo n.º 1
0
        public void ToObject <T>(IDataReader reader, T item) where T : DatabaseEntity, new()
        {
            if (reader == null)
            {
                return;
            }

            int len = reader.FieldCount;

            string[] propertyNames = new string[len];

            DatabaseEntityDef definition = _modelDefFactory.GetDef <T>();

            for (int i = 0; i < len; ++i)
            {
                propertyNames[i] = reader.GetName(i);
            }

            if (reader.Read())
            {
                for (int i = 0; i < len; ++i)
                {
                    DatabaseEntityPropertyDef property = definition.GetProperty(propertyNames[i]);

                    object value = property.TypeConverter == null?
                                   ValueConverter.DbValueToTypeValue(property.PropertyType, reader[i]) :
                                       property.TypeConverter.DbValueToTypeValue(reader[i]);

                    property.SetValue(item, value);
                }
            }
        }
Exemplo n.º 2
0
        public IList <T> ToList <T>(IDataReader reader)
            where T : DatabaseEntity, new()
        {
            IList <T> lst = new List <T>();

            if (reader == null)
            {
                return(lst);
            }

            int len = reader.FieldCount;

            string[] propertyNames = new string[len];


            DatabaseEntityDef definition = _modelDefFactory.GetDef <T>();

            for (int i = 0; i < len; ++i)
            {
                propertyNames[i] = reader.GetName(i);
            }

            while (reader.Read())
            {
                T item = new T();

                for (int i = 0; i < len; ++i)
                {
                    DatabaseEntityPropertyDef property = definition.GetProperty(propertyNames[i]);
                    object fieldValue = reader[i];

                    if (property.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        item = null;
                        break;
                    }

                    object value = property.TypeConverter == null?
                                   ValueConverter.DbValueToTypeValue(property.PropertyType, fieldValue) :
                                       property.TypeConverter.DbValueToTypeValue(fieldValue);

                    property.SetValue(item, value);
                }

                if (item != null && !item.Deleted)
                {
                    lst.Add(item);
                }
            }

            return(lst);
        }
Exemplo n.º 3
0
        public IList <Tuple <TSource, TTarget> > ToList <TSource, TTarget>(IDataReader reader)
            where TSource : DatabaseEntity, new()
            where TTarget : DatabaseEntity, new()
        {
            IList <Tuple <TSource, TTarget> > lst = new List <Tuple <TSource, TTarget> >();

            if (reader == null)
            {
                return(lst);
            }

            DatabaseEntityDef definition1 = _modelDefFactory.GetDef <TSource>();
            DatabaseEntityDef definition2 = _modelDefFactory.GetDef <TTarget>();

            string[] propertyNames1 = new string[definition1.FieldCount];
            string[] propertyNames2 = new string[definition2.FieldCount];

            int j = 0;

            for (int i = 0; i < definition1.FieldCount; ++j, ++i)
            {
                propertyNames1[i] = reader.GetName(j);
            }

            for (int i = 0; i < definition2.FieldCount; ++j, ++i)
            {
                propertyNames2[i] = reader.GetName(j);
            }

            while (reader.Read())
            {
                TSource t1 = new TSource();
                TTarget t2 = new TTarget();

                j = 0;

                for (int i = 0; i < definition1.FieldCount; ++i, ++j)
                {
                    DatabaseEntityPropertyDef pDef = definition1.GetProperty(propertyNames1[i]);
                    object fieldValue = reader[j];

                    if (pDef.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        t1 = null;
                        break;
                    }

                    if (pDef != null)
                    {
                        object value = pDef.TypeConverter == null?
                                       ValueConverter.DbValueToTypeValue(pDef.PropertyType, fieldValue) :
                                           pDef.TypeConverter.DbValueToTypeValue(fieldValue);

                        pDef.SetValue(t1, value);
                    }
                }

                for (int i = 0; i < definition2.FieldCount; ++i, ++j)
                {
                    DatabaseEntityPropertyDef pDef = definition2.GetProperty(propertyNames2[i]);
                    object fieldValue = reader[j];

                    if (pDef.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        t2 = null;
                        break;
                    }

                    if (pDef != null)
                    {
                        object value = pDef.TypeConverter == null?
                                       ValueConverter.DbValueToTypeValue(pDef.PropertyType, fieldValue) :
                                           pDef.TypeConverter.DbValueToTypeValue(fieldValue);

                        pDef.SetValue(t2, value);
                    }
                }

                if (t1 != null && t1.Deleted)
                {
                    t1 = null;
                }
                if (t2 != null && t2.Deleted)
                {
                    t2 = null;
                }

                //删除全为空
                if (t1 != null || t2 != null)
                {
                    lst.Add(new Tuple <TSource, TTarget>(t1, t2));
                }
            }

            return(lst);
        }