예제 #1
0
        public EntityBase LoadEntityById(object id)
        {
            if (!_compiled)
            {
                Compile();
            }

            ObjectDescription od = ClassFactory.GetObjectDescription(ReflectedType, _ps);

            if (od.IsAggregated)
            {
                return(_ps.GetEntityById(ReflectedType, id));
            }

            DataRow row = GetUnderlyingRow(ReflectedType, id);

            if (row == null)
            {
                return(null);
            }

            TableAttribute tableAttr = od.DbTableAttribute;

            if (tableAttr.Conditional)
            {
                if (!tableAttr.CheckConditions(row))
                {
                    return(null);
                }
            }

            EntityBase entity = od.CreateObject() as EntityBase;

            od.IdField.SetValue(entity, row[Mapper[od.IdField.Name]]);

            EntityCache cache = _ps.Caches[entity.GetType()];

            if (!cache.Contains(entity.ID))
            {
                cache.Add(entity);
            }
            else
            {
                cache[entity.ID] = entity;
            }

            ReloadEntity(entity);            // this is a reference type.... (I hate them!!!!)
            return(entity);
        }
예제 #2
0
        public List <T> LoadAll <T>() where T : EntityBase
        {
            if (!_compiled)
            {
                Compile();
            }

            ObjectDescription od = ClassFactory.GetObjectDescription(ReflectedType, _ps);


            if (od.IsAggregated)
            {
                throw new Exception("Cannot load aggregated types");
            }
            //return _ps.GetEntities(ReflectedType);

            List <T> list = new List <T>(UnderlyngDataSet.Tables[Mapper.TableName].Rows.Count);

            foreach (DataRow row in UnderlyngDataSet.Tables[Mapper.TableName].Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    continue;
                }
                TableAttribute tableAttr = od.DbTableAttribute;
                if (tableAttr != null && tableAttr.Conditional)
                {
                    if (!tableAttr.CheckConditions(row))
                    {
                        continue;
                    }
                }

                T entity = od.CreateObject() as T;
                entity.BeginLoad();
                entity.CreatorPs        = this._ps;
                entity[od.IdField.Name] = row[Mapper[od.IdField.Name]];
                entity.SourceRow        = row;

                EntityCache cache = _ps.Caches[entity.GetType()];
                if (!cache.Contains(entity.ID))
                {
                    cache.Add(entity);
                }
                else
                {
                    //throw new Exception();
                    entity = (T)cache[entity.ID];
                }

                list.Add(entity);
            }
            foreach (T entity in list)
            {
                ObjectDescription ods = ClassFactory.GetObjectDescription(entity.GetType(), _ps);
                if (!ods.IsWrapped)
                {
                    ReloadEntity(entity);                    // this is a reference type.... (I hate them!!!!)
                }
                else
                {
                    EntityBase e = null;                    //(EntityBase)ClassFactory.CreateObject(od.WrappedClass, _ps);//( entity as IWrapObject ).WrappedObject;
                    //e.BeginLoad();
                    //e.Ps = _ps;
                    //e.SourceRow = entity.SourceRow;
                    e = _ps.GetEntityById(od.WrappedClass, entity.ID);
                    (entity as IWrapObject).WrappedObject = e;
                    e.EndLoad();
                    //ReloadEntity(e);
                    //( entity as IWrapObject ).WrappedObject = e;
                }
                entity.EndLoad();
            }
            return(list);
        }