using System; using System.Collections; using System.Data; using Zhaizj.Framework.ORM.Caching; namespace Zhaizj.Framework.ORM { internal class FillUtil { private static readonly ILog logger = LogManager.GetLogger( typeof( FillUtil ) ); public static Object Populate( IDataRecord rd, ObjectInfo state ) { IEntity obj = Entity.New( state.EntityInfo.Type.FullName ); // state //obj.state.Order = state.Order; for (int i = 0; i < rd.FieldCount; i++) { Object fdvalue = rd[i]; if (fdvalue == null || fdvalue == DBNull.Value) continue; EntityPropertyInfo ep = state.EntityInfo.GetPropertyByColumn( rd.GetName( i ) ); if (ep == null) continue; try { if (ep.IsEntity || ep.IsAbstractEntity) { setEntityPropertyValueById( obj, state, ep, rd.GetInt32( i ) ); } else { ep.SetValue( obj, getReaderValue( fdvalue, ep.Type ) ); } } catch (Exception ex) { logger.Error( ex.Message + "=" + ep.Name + "_" + ep.Type ); logger.Error( ex.StackTrace ); throw ex; } } return obj; } private static void setEntityPropertyValueById( IEntity obj, ObjectInfo state, EntityPropertyInfo property, int pid ) { if (!property.IsAbstractEntity) { IEntity objValue = Entity.New( property.Type.FullName ); objValue.Id = pid; // state //objValue.state = new ObjectInfo( property.Type ).Copy( state ); IEntity objCache = ObjectPool.FindOne( property.Type, objValue.Id ); if (objCache != null) { objValue = objCache; } obj.set( property.Name, objValue ); } } private static Object getReaderValue( Object rdValue, Type ptype ) { if (ptype == typeof( decimal )) return Convert.ToDecimal( rdValue ); if (ptype == typeof( int )) return Convert.ToInt32( rdValue ); return rdValue; } } }