/// <summary> /// Registers an entity to be inserted /// </summary> /// <param name="entity"></param> public void RegisterToInsert(object entity) { if (entity == null) { throw new ArgumentNullException("entity"); } var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = new EntityTrack(entity, EntityState.ToInsert); entities.Add(entityTrack); } else { switch (entityTrack.EntityState) { // if already registered for insert/update, then this is an error case EntityState.ToInsert: case EntityState.ToWatch: throw new InvalidOperationException(); // whenever the object is registered for deletion, the fact of // registering it for insertion sets it back to watch case EntityState.ToDelete: entityTrack.EntityState = EntityState.ToWatch; entitiesByKey[entityTrack.IdentityKey] = entityTrack; break; default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Registers entity to be deleted /// </summary> /// <param name="entity"></param> public void RegisterToDelete(object entity) { if (entity == null) { throw new ArgumentNullException("entity"); } var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = new EntityTrack(entity, EntityState.ToDelete); entities.Add(entityTrack); } else { // changes the state of the current entity switch (entityTrack.EntityState) { // if entity was to be inserted, we just remove it from the list // as if it never came here case EntityState.ToInsert: entities.Remove(entityTrack); break; // watched entities are registered to be removed case EntityState.ToWatch: entityTrack.EntityState = EntityState.ToDelete; entitiesByKey.Remove(entityTrack.IdentityKey); break; case EntityState.ToDelete: throw new InvalidOperationException(); default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Registers an entity to be watched /// </summary> /// <param name="entity"></param> /// <param name="identityKey"></param> public void RegisterToWatch(object entity, IdentityKey identityKey) { var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = FindByIdentity(identityKey); if (entityTrack != null) { throw new System.Data.Linq.DuplicateKeyException(entity); } entityTrack = new EntityTrack(entity, EntityState.ToWatch) { IdentityKey = identityKey }; entities.Add(entityTrack); entitiesByKey[identityKey] = entityTrack; } else { // changes the state of the current entity switch (entityTrack.EntityState) { case EntityState.ToInsert: entityTrack.EntityState = EntityState.ToWatch; entityTrack.IdentityKey = identityKey; entitiesByKey[identityKey] = entityTrack; break; // watched entities should not be registered again case EntityState.ToWatch: case EntityState.ToDelete: throw new InvalidOperationException(); default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Registers an entity to be inserted /// </summary> /// <param name="entity"></param> public void RegisterToInsert(object entity) { if (entity == null) throw new ArgumentNullException("entity"); var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = new EntityTrack(entity, EntityState.ToInsert); entities.Add(entityTrack); } else { switch (entityTrack.EntityState) { // if already registered for insert/update, then this is an error case EntityState.ToInsert: case EntityState.ToWatch: throw new InvalidOperationException(); // whenever the object is registered for deletion, the fact of // registering it for insertion sets it back to watch case EntityState.ToDelete: entityTrack.EntityState = EntityState.ToWatch; entitiesByKey[entityTrack.IdentityKey] = entityTrack; break; default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Registers entity to be deleted /// </summary> /// <param name="entity"></param> public void RegisterToDelete(object entity) { if (entity == null) throw new ArgumentNullException("entity"); var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = new EntityTrack(entity, EntityState.ToDelete); entities.Add(entityTrack); } else { // changes the state of the current entity switch (entityTrack.EntityState) { // if entity was to be inserted, we just remove it from the list // as if it never came here case EntityState.ToInsert: entities.Remove(entityTrack); break; // watched entities are registered to be removed case EntityState.ToWatch: entityTrack.EntityState = EntityState.ToDelete; entitiesByKey.Remove(entityTrack.IdentityKey); break; case EntityState.ToDelete: throw new InvalidOperationException(); default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Registers an entity to be watched /// </summary> /// <param name="entity"></param> /// <param name="identityKey"></param> public void RegisterToWatch(object entity, IdentityKey identityKey) { var entityTrack = FindByReference(entity); if (entityTrack == null) { entityTrack = FindByIdentity(identityKey); if (entityTrack != null) throw new System.Data.Linq.DuplicateKeyException(entity); entityTrack = new EntityTrack(entity, EntityState.ToWatch) { IdentityKey = identityKey }; entities.Add(entityTrack); entitiesByKey[identityKey] = entityTrack; } else { // changes the state of the current entity switch (entityTrack.EntityState) { case EntityState.ToInsert: entityTrack.EntityState = EntityState.ToWatch; entityTrack.IdentityKey = identityKey; entitiesByKey[identityKey] = entityTrack; break; // watched entities should not be registered again case EntityState.ToWatch: case EntityState.ToDelete: throw new InvalidOperationException(); default: throw new ArgumentOutOfRangeException(); } } }