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); }
private object CreateInstance(Type type, Row row, EntityCache entityCache, string alias = null) { var instance = CreateInstance(type); IList <ColumnValue> columnValuesForType; if (String.IsNullOrWhiteSpace(alias)) { columnValuesForType = row.GetColumnValuesForType(type, _conventionReader); } else { columnValuesForType = row.GetColumnValuesForAlias(alias); } var columnValuesForCurrentType = columnValuesForType .Where(x => !_conventionReader.IsEntityReferenceProperty(x.ColumnName, type)) .ToList(); if (columnValuesForCurrentType.Any() && columnValuesForCurrentType.All(x => x.Value == null)) { return(null); } foreach (var columnValue in columnValuesForCurrentType) { var property = GetProperty(type, columnValue); property.SetValue(instance, columnValue.Value, null); } var includesPrimaryKeyColumn = QueryIncludesPrimaryKeyColumn(type, row, alias); // If the query does not include the primary key column we can't know if // the cache already contains the entity or not, since we do not have anything // unique to match the entities on. So, treat all rows as different to make sure // no rows are incorrectly merged into one, which would be the case if we assumed // that they represented the same entity. if (entityCache.Contains(instance) && includesPrimaryKeyColumn) { return(entityCache.GetExisting(instance)); } entityCache.Add(instance); return(instance); }
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); }