private IEntity ProcessProperties(ref EntityBase entity, DataRow row) { ObjectDescription od = ClassFactory.GetObjectDescription(ReflectedType, _ps); List <PropertyDescription> l = new List <PropertyDescription>(); foreach (PropertyDescription prop in od.Properties) { if (prop.GetAttribute <NonPersistentAttribute>() != null) { continue; } if (prop.IsId) { continue; } RelationAttribute relAttr = prop.GetAttribute <RelationAttribute>(); if (relAttr != null) { if (relAttr.RelationType == RelationType.ManyToMany) { l.Add(prop); continue; } else if (relAttr.RelationType == RelationType.OneToOne) { SetOtoRelationValue(ref entity, row, relAttr, prop); continue; } else if (relAttr.RelationType == RelationType.OneToMany) { l.Add(prop); continue; } } object val = prop.GetValue(entity); if (val != null) { row[Mapper[prop.Name]] = val; } else { row[Mapper[prop.Name]] = DBNull.Value; } } entity.AcceptChanges(); foreach (PropertyDescription prop in l) { RelationAttribute relAttr = prop.GetAttribute <RelationAttribute>(); if (relAttr.RelationType == RelationType.ManyToMany) { SetMtmRelationValues(ref entity, row, relAttr, prop); } else if (relAttr.RelationType == RelationType.OneToMany) { SetOtmRelationValues(ref entity, row, relAttr, prop); } } return(entity); }
public void Save(EntityBase entity) { if (entity.State == EntityState.Unchanged || entity.State == EntityState.NewDeleted) { return; } ObjectDescription od = ClassFactory.GetObjectDescription(entity.GetType(), _ps); EntityBase ent; if (od.IsWrapped) { ent = ((IWrapObject)entity).WrappedObject; _ps.SaveInner(ent); return; } ent = entity; bool isNew = ent.State == EntityState.New; bool isDeleted = ent.State == EntityState.Deleted; //ent.State = EntityState.Unchanged; if (isNew && ent.ID == null) { throw new Exception("Id is null: entity type " + ent); } if (isNew && !_ps.Caches[ent.GetType()].Contains(ent.ID)) { _ps.Caches[ent.GetType()].Add(ent); } foreach (PropertyDescription pd in od.ManyToManyProperties) { _ps.SourceStorageAccessMediator.AddCommand(new StorageCommand(ent, pd)); } StorageCommand command = new StorageCommand(ent); ent.AcceptChanges(); switch (command.CommandType) { case StorageCommandType.Insert: case StorageCommandType.Update: foreach (PropertyDescription pd in od.OneToOneProperties) { EntityBase propvalue = pd.GetValue(ent) as EntityBase; if (propvalue != null) { _ps.SaveInner(propvalue); } } _ps.SourceStorageAccessMediator.AddCommand(command); foreach (PropertyDescription pd in od.OneToManyProperties) { IList propvalue = pd.GetValue(ent) as IList; if (propvalue != null && propvalue.Count > 0) { foreach (EntityBase e in propvalue) { _ps.SaveInner(e); } } } break; case StorageCommandType.Delete: foreach (PropertyDescription pd in od.OneToManyProperties) { IList propvalue = pd.GetValue(ent) as IList; if (propvalue != null && propvalue.Count > 0) { foreach (EntityBase e in propvalue) { _ps.SaveInner(e); } } } _ps.SourceStorageAccessMediator.AddCommand(command); foreach (PropertyDescription pd in od.OneToOneProperties) { EntityBase propvalue = pd.GetValue(ent) as EntityBase; if (propvalue != null) { _ps.SaveInner(propvalue); } } break; } if (isDeleted) { if (_ps.Caches[ent.GetType()].Contains(ent.ID)) { _ps.Caches[ent.GetType()].Delete(ent); } } ent.AcceptChanges(); }