public void unchangedEntity_service_getEntityState_is_None() { EntityTrackingStates expected = EntityTrackingStates.None; ChangeTrackingService svc = new ChangeTrackingService(); PersonCollection p = new PersonCollection(svc); EntityTrackingStates actual = svc.GetEntityState(p); Assert.AreEqual <EntityTrackingStates>(expected, actual); }
/// <summary> /// Generates the advisory. /// </summary> /// <param name="svc">The service that holds the data to generate the advisory for.</param> /// <param name="changeSet">The subset of changes to generate the advisory for.</param> /// <returns>The generated advisory.</returns> public IAdvisory GenerateAdvisory(IChangeTrackingService svc, IChangeSet changeSet) { var result = new List <IAdvisedAction>(); var distinct = this.visitor.Visit(changeSet); foreach (var kvp in distinct) { ProposedActions proposedAction = kvp.Value.GetAdvisedAction(kvp.Key); EntityTrackingStates state = svc.GetEntityState(kvp.Key); bool isTransient = (state & EntityTrackingStates.IsTransient) == EntityTrackingStates.IsTransient; switch (proposedAction) { case ProposedActions.Create | ProposedActions.Update: proposedAction = isTransient ? ProposedActions.Create : ProposedActions.Update; break; case ProposedActions.Delete | ProposedActions.Dispose: proposedAction = isTransient ? ProposedActions.Dispose : ProposedActions.Delete; break; default: throw new NotSupportedException(); } var advisedAction = this.OnCreateAdvisedAction(kvp.Key, proposedAction); result.Add(advisedAction); } IEnumerable transientEntities = svc.GetEntities(EntityTrackingStates.IsTransient, true); foreach (Object te in transientEntities) { if (result.Any(a => a.Target == te)) { /* * An entity is created as Transient+Persistable, then is added to * a MementoEntityCollection that tracks a change as "item-added" thus * the advisory adds that entity (that is not AutoRemove) as something that * must be created. so if we arrive here and we already have the transient * entity in the advisory we skip it. */ continue; } var advisedAction = this.OnCreateAdvisedAction(te, ProposedActions.Create); result.Add(advisedAction); } return(new Advisory(result)); }
public void entityMemento_ctor_requesting_transient_registration_using_base_iMemento_successfully_register_entity_as_transient() { EntityTrackingStates expected = EntityTrackingStates.IsTransient | EntityTrackingStates.AutoRemove; using (ChangeTrackingService svc = new ChangeTrackingService()) { var target = new FakeMementoEntity(true); ((IMemento)target).Memento = svc; EntityTrackingStates actual = svc.GetEntityState(target); actual.Should().Be.EqualTo(expected); } }
public void after_mutual_exclusive_actions_entity_state_is_HasBackwardChanges() { EntityTrackingStates expected = EntityTrackingStates.HasBackwardChanges; IChangeTrackingService svc = new ChangeTrackingService(); PersonCollection p = new PersonCollection(svc); p.Add(new Person(svc, false)); p.RemoveAt(0); EntityTrackingStates actual = svc.GetEntityState(p); Assert.AreEqual <EntityTrackingStates>(expected, actual); }
public void after_add_GetEntityState_is_Changed() { EntityTrackingStates expected = EntityTrackingStates.HasBackwardChanges; ChangeTrackingService svc = new ChangeTrackingService(); PersonCollection p = new PersonCollection(svc); p.Add(new Person(svc, false)); p.Add(new Person(svc, false)); EntityTrackingStates actual = svc.GetEntityState(p); actual.Should().Be.EqualTo(expected); }
public void after_rejectChanges_entity_is_no_more_changed() { EntityTrackingStates expected = EntityTrackingStates.None; ChangeTrackingService svc = new ChangeTrackingService(); PersonCollection p = new PersonCollection(svc); p.Add(new Person(null, false)); svc.RejectChanges(); EntityTrackingStates actual = svc.GetEntityState(p); Assert.AreEqual <EntityTrackingStates>(expected, actual); }
public void entityMemento_ctor_requesting_transient_registration_using_base_iMemento_to_suspended_memento_do_not_register_entity_as_transient() { EntityTrackingStates expected = EntityTrackingStates.None; using (var svc = new ChangeTrackingService()) { svc.Suspend(); var target = new FakeMementoEntity(true); ((IMemento)target).Memento = svc; EntityTrackingStates actual = svc.GetEntityState(target); actual.Should().Be.EqualTo(expected); } }
/// <summary> /// Gets the entities. /// </summary> /// <param name="stateFilter">The state filter.</param> /// <param name="exactMatch">if set to <c>true</c> [exact match].</param> /// <returns></returns> public virtual IEnumerable<Object> GetEntities( EntityTrackingStates stateFilter, Boolean exactMatch ) { HashSet<Object> all = new HashSet<Object>( ObjectReferenceEqualityComparer.Instance ); transientEntities.Keys.ForEach( te => all.Add( te ) ); this.backwardChangesStack.ForEach( c => { /* * Recuperiamo le entity modificate incapsulate * in ogni IChange e le aggiugiamo all'HashSet che * le inserisce "disinct" in base alla reference * ergo se una reference è già stata inserita non verrà * inserita una seconda volta. */ c.GetChangedEntities().ForEach( entity => all.Add( entity ) ); } ); var query = from entity in all let state = this.GetEntityState( entity ) where exactMatch ? state == stateFilter : ( state & stateFilter ) == stateFilter select entity; return query.AsReadOnly(); }