/// <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)); }
/// <summary> /// Initializes a new instance of the <see cref="AdvisedAction"/> class. /// </summary> /// <param name="target">The target.</param> /// <param name="action">The action.</param> public AdvisedAction( Object target, ProposedActions action ) { Ensure.That( target ).Named( () => target ).IsNotNull(); Ensure.That( action ) .Named( () => action ) .IsDefined() .WithMessage( "None is a not supported ProposedActions value." ) .If( v => v == ProposedActions.None ) .ThenThrow( v => new NotSupportedException( v.GetFullErrorMessage() ) ); this.Target = target; this.Action = action; }
/// <summary> /// Initializes a new instance of the <see cref="AdvisedAction"/> class. /// </summary> /// <param name="target">The target.</param> /// <param name="action">The action.</param> public AdvisedAction(Object target, ProposedActions action) { Ensure.That(target).Named(() => target).IsNotNull(); Ensure.That(action) .Named(() => action) .IsDefined() .WithMessage("None is a not supported ProposedActions value.") .If(v => v == ProposedActions.None) .ThenThrow(v => new NotSupportedException(v.GetFullErrorMessage())); this.Target = target; this.Action = action; }
public void service_getAdvisory_generate_valid_advisory_with_more_changes_applied_to_the_same_entity() { ProposedActions expected = ProposedActions.Delete; ChangeTrackingService svc = new ChangeTrackingService(); PersonCollection list = new PersonCollection(svc); list.Add(new Person(null, false)); //First IChange list.RemoveAt(0); //Second IChange IAdvisory advisory = svc.GetAdvisory(); IAdvisedAction action = advisory.First(); ProposedActions actual = action.Action; Assert.AreEqual <ProposedActions>(expected, actual); }
/// <summary> /// Called in order to create the advised action for the give target. /// </summary> /// <param name="target">The target entity.</param> /// <param name="proposedAction">The proposed action.</param> /// <returns>The advised action.</returns> protected virtual IAdvisedAction OnCreateAdvisedAction(Object target, ProposedActions proposedAction) { var advisedAction = new AdvisedAction(target, proposedAction); return(advisedAction); }
/// <summary> /// Called in order to create the advised action for the give target. /// </summary> /// <param name="target">The target entity.</param> /// <param name="proposedAction">The proposed action.</param> /// <returns>The advised action.</returns> protected virtual IAdvisedAction OnCreateAdvisedAction( Object target, ProposedActions proposedAction ) { var advisedAction = new AdvisedAction( target, proposedAction ); return advisedAction; }
static IEnumerable <T> GetItemsWhereActionIs <T>(this IChangeTrackingService service, ProposedActions filter) { var items = service.GetAdvisory() .Where(a => { return(a.Target.GetType().Is <T>() && a.Action == filter); }) .Select(a => a.Target) .Cast <T>(); return(items); }