public void Derive(DynamicChangeSet changeSet) { System.Collections.Generic.Dictionary <DynamicObject, object> fullNames = changeSet.ChangedRoles <Person>("FullName"); if (fullNames?.Any() == true) { System.Collections.Generic.IEnumerable <DynamicObject> people = fullNames.Select(v => v.Key).Distinct(); foreach (dynamic person in people) { person.Greeting = $"Hello {person.FullName}!"; } } }
public void Derive() { DynamicChangeSet changeSet = this.Snapshot(); while (changeSet.HasChanges) { foreach (KeyValuePair <string, IDynamicDerivation> kvp in this.DerivationById) { IDynamicDerivation derivation = kvp.Value; derivation.Derive(changeSet); } changeSet = this.Snapshot(); } }
public void Derive(DynamicChangeSet changeSet) { this.derivation.Derive(changeSet); System.Collections.Generic.Dictionary <DynamicObject, object> firstNames = changeSet.ChangedRoles <Person>("FirstName"); System.Collections.Generic.Dictionary <DynamicObject, object> lastNames = changeSet.ChangedRoles <Person>("LastName"); if (firstNames?.Any() == true || lastNames?.Any() == true) { System.Collections.Generic.IEnumerable <DynamicObject> people = firstNames.Union(lastNames).Select(v => v.Key).Distinct(); foreach (dynamic person in people) { person.FullName = $"{person.FullName} Chained"; } } }
public void Derive(DynamicChangeSet changeSet) { System.Collections.Generic.Dictionary <DynamicObject, object> firstNames = changeSet.ChangedRoles <Person>("FirstName"); System.Collections.Generic.Dictionary <DynamicObject, object> lastNames = changeSet.ChangedRoles <Person>("LastName"); if (firstNames?.Any() == true || lastNames?.Any() == true) { System.Collections.Generic.IEnumerable <DynamicObject> people = firstNames.Union(lastNames).Select(v => v.Key).Distinct(); foreach (dynamic person in people) { // Dummy updates ... person.FirstName = person.FirstName; person.LastName = person.LastName; person.DerivedAt = DateTime.Now; person.FullName = $"{person.FirstName} {person.LastName}"; } } }
public void Snapshot() { var population = new Default.DynamicPopulation( new DynamicMeta(new Pluralizer()), v => { v.AddUnit <Person, string>("FirstName"); v.AddUnit <Person, string>("LastName"); }); dynamic john = population.New <Person>(); dynamic jane = population.New <Person>(); john.FirstName = "John"; john.LastName = "Doe"; DynamicChangeSet snapshot1 = population.Snapshot(); jane.FirstName = "Jane"; jane.LastName = "Doe"; System.Collections.Generic.Dictionary <DynamicObject, object> changedFirstNames = snapshot1.ChangedRoles <Person>("FirstName"); System.Collections.Generic.Dictionary <DynamicObject, object> changedLastNames = snapshot1.ChangedRoles <Person>("LastName"); Assert.Single(changedFirstNames.Keys); Assert.Single(changedLastNames.Keys); Assert.Contains(john, changedFirstNames.Keys); Assert.Contains(john, changedLastNames.Keys); DynamicChangeSet snapshot2 = population.Snapshot(); changedFirstNames = snapshot2.ChangedRoles <Person>("FirstName"); changedLastNames = snapshot2.ChangedRoles <Person>("LastName"); Assert.Single(changedFirstNames.Keys); Assert.Single(changedLastNames.Keys); Assert.Contains(jane, changedFirstNames.Keys); Assert.Contains(jane, changedLastNames.Keys); }
internal DynamicChangeSet Snapshot() { foreach (IDynamicRoleType roleType in this.changedRoleByAssociationByRoleType.Keys.ToArray()) { Dictionary <DynamicObject, object> changedRoleByAssociation = this.changedRoleByAssociationByRoleType[roleType]; Dictionary <DynamicObject, object> roleByAssociation = this.RoleByAssociation(roleType); foreach (DynamicObject association in changedRoleByAssociation.Keys.ToArray()) { object role = changedRoleByAssociation[association]; roleByAssociation.TryGetValue(association, out object originalRole); bool areEqual = ReferenceEquals(originalRole, role) || (roleType.IsOne && Equals(originalRole, role)) || (roleType.IsMany && ((IStructuralEquatable)originalRole)?.Equals((IStructuralEquatable)role) == true); if (areEqual) { changedRoleByAssociation.Remove(association); continue; } roleByAssociation[association] = role; } if (roleByAssociation.Count == 0) { this.changedRoleByAssociationByRoleType.Remove(roleType); } } foreach (IDynamicAssociationType associationType in this.changedAssociationByRoleByAssociationType.Keys.ToArray()) { Dictionary <DynamicObject, object> changedAssociationByRole = this.changedAssociationByRoleByAssociationType[associationType]; Dictionary <DynamicObject, object> associationByRole = this.AssociationByRole(associationType); foreach (DynamicObject role in changedAssociationByRole.Keys.ToArray()) { object changedAssociation = changedAssociationByRole[role]; associationByRole.TryGetValue(role, out object originalRole); bool areEqual = ReferenceEquals(originalRole, changedAssociation) || (associationType.IsOne && Equals(originalRole, changedAssociation)) || (associationType.IsMany && ((IStructuralEquatable)originalRole)?.Equals((IStructuralEquatable)changedAssociation) == true); if (areEqual) { changedAssociationByRole.Remove(role); continue; } associationByRole[role] = changedAssociation; } if (associationByRole.Count == 0) { this.changedAssociationByRoleByAssociationType.Remove(associationType); } } DynamicChangeSet snapshot = new DynamicChangeSet(this.meta, this.changedRoleByAssociationByRoleType, this.changedAssociationByRoleByAssociationType); this.changedRoleByAssociationByRoleType = new Dictionary <IDynamicRoleType, Dictionary <DynamicObject, object> >(); this.changedAssociationByRoleByAssociationType = new Dictionary <IDynamicAssociationType, Dictionary <DynamicObject, object> >(); return(snapshot); }