internal void RemoveRole(DynamicObject association, IDynamicRoleType roleType, DynamicObject role) { IDynamicAssociationType associationType = roleType.AssociationType; this.GetAssociation(role, associationType, out object previousAssociation); this.GetRole(association, roleType, out object previousRole); if (previousRole != null) { // Role var changedRoleByAssociation = this.ChangedRoleByAssociation(roleType); changedRoleByAssociation[association] = NullableArraySet.Remove(previousRole, role); // Association var changedAssociationByRole = this.ChangedAssociationByRole(associationType); if (associationType.IsOne) { // One to Many changedAssociationByRole[role] = null; } else { // Many to Many changedAssociationByRole[role] = NullableArraySet.Add(previousAssociation, association); } } }
public Dictionary <DynamicObject, object> ChangedRoles <TRole>(string name) { DynamicObjectType objectType = this.Meta.ObjectTypeByType[typeof(TRole)]; IDynamicRoleType roleType = objectType.RoleTypeByName[name]; return(this.ChangedRoles(roleType)); }
internal void AddRole(DynamicObject association, IDynamicRoleType roleType, DynamicObject role) { IDynamicAssociationType associationType = roleType.AssociationType; this.GetAssociation(role, associationType, out object previousAssociation); // Role var changedRoleByAssociation = this.ChangedRoleByAssociation(roleType); this.GetRole(association, roleType, out object previousRole); DynamicObject[] roleArray = (DynamicObject[])previousRole; roleArray = NullableArraySet.Add(roleArray, role); changedRoleByAssociation[association] = roleArray; // Association var changedAssociationByRole = this.ChangedAssociationByRole(associationType); if (associationType.IsOne) { // One to Many DynamicObject previousAssociationObject = (DynamicObject)previousAssociation; if (previousAssociationObject != null) { this.GetRole(previousAssociationObject, roleType, out object previousAssociationRole); changedRoleByAssociation[previousAssociationObject] = NullableArraySet.Remove(previousAssociationRole, role); } changedAssociationByRole[role] = association; } else { // Many to Many changedAssociationByRole[role] = NullableArraySet.Add(previousAssociation, association); } }
private void AddRoleType(IDynamicRoleType roleType) { this.CheckNames(roleType.SingularName, roleType.PluralName); this.assignedRoleTypeByName.Add(roleType.SingularName, roleType); this.assignedRoleTypeByName.Add(roleType.PluralName, roleType); }
private Dictionary <DynamicObject, object> ChangedRoleByAssociation(IDynamicRoleType roleType) { if (!this.changedRoleByAssociationByRoleType.TryGetValue(roleType, out Dictionary <DynamicObject, object> changedRoleByAssociation)) { changedRoleByAssociation = new Dictionary <DynamicObject, object>(); this.changedRoleByAssociationByRoleType[roleType] = changedRoleByAssociation; } return(changedRoleByAssociation); }
internal void GetRole(DynamicObject association, IDynamicRoleType roleType, out object role) { if (this.changedRoleByAssociationByRoleType.TryGetValue(roleType, out Dictionary <DynamicObject, object> changedRoleByAssociation) && changedRoleByAssociation.TryGetValue(association, out role)) { return; } this.RoleByAssociation(roleType).TryGetValue(association, out role); }
private bool TryGetRole(IDynamicRoleType roleType, out object result) { result = this.Population.GetRole(this, roleType); if (result == null && roleType.IsMany) { result = roleType.ObjectType.EmptyArray; } return(true); }
internal static object NormalizeToOne(this IDynamicRoleType @this, object value) { if (value != null) { var type = @this.ObjectType.Type; if (!type.IsAssignableFrom(value.GetType())) { throw new ArgumentException($"{@this.Name} should be a {type.Name} but was a {value.GetType()}"); } } return(value); }
internal static object NormalizeToMany(this IDynamicRoleType @this, object value) { if (value == null) { return(value); } if (value is ICollection collection) { return(@this.NormalizeToMany(collection).ToArray()); } throw new ArgumentException($"{value.GetType()} is not a collection Type"); }
private static IEnumerable <dynamic> NormalizeToMany(this IDynamicRoleType @this, ICollection role) { foreach (var @object in role) { if (@object != null) { Type type = @this.ObjectType.Type; if (!type.IsAssignableFrom(@object.GetType())) { throw new ArgumentException($"{@this.Name} should be a {type.Name} but was a {@object.GetType()}"); } yield return(@object); } } }
internal static string PluralNameForAssociation(this IDynamicRoleType @this, DynamicObjectType objectType) { return($"{@this.ObjectType.Meta.Pluralizer.Pluralize(objectType.Type.Name)}Where{@this.SingularName}"); }
public void AddRole(DynamicObject obj, IDynamicRoleType roleType, IDynamicObject role) { this.database.AddRole(obj, roleType, (DynamicObject)role); }
public void SetRole(DynamicObject obj, IDynamicRoleType roleType, object value) { this.database.SetRole(obj, roleType, value); }
public object GetRole(DynamicObject obj, IDynamicRoleType roleType) { this.database.GetRole(obj, roleType, out var result); return(result); }
public Dictionary <DynamicObject, object> ChangedRoles(IDynamicRoleType roleType) { this.roleByAssociationByRoleType.TryGetValue(roleType, out Dictionary <DynamicObject, object> changedRelations); return(changedRelations); }
public Dictionary <DynamicObject, object> ChangedRoles(DynamicObjectType objectType, string name) { IDynamicRoleType roleType = objectType.RoleTypeByName[name]; return(this.ChangedRoles(roleType)); }
internal static string SingularNameForAssociation(this IDynamicRoleType @this, DynamicObjectType objectType) { return($"{objectType.Type.Name}Where{@this.SingularName}"); }
internal void SetRole(dynamic association, IDynamicRoleType roleType, object role) { var normalizedRole = roleType.Normalize(role); if (roleType.IsUnit) { // Role this.ChangedRoleByAssociation(roleType)[association] = normalizedRole; } else { var associationType = roleType.AssociationType; this.GetRole(association, roleType, out object previousRole); if (roleType.IsOne) { var roleObject = (DynamicObject)normalizedRole; this.GetAssociation(roleObject, associationType, out object previousAssociation); // Role var changedRoleByAssociation = this.ChangedRoleByAssociation(roleType); changedRoleByAssociation[association] = roleObject; // Association var changedAssociationByRole = this.ChangedAssociationByRole(associationType); if (associationType.IsOne) { // One to One var previousAssociationObject = (DynamicObject)previousAssociation; if (previousAssociationObject != null) { changedRoleByAssociation[previousAssociationObject] = null; } if (previousRole != null) { var previousRoleObject = (DynamicObject)previousRole; changedAssociationByRole[previousRoleObject] = null; } changedAssociationByRole[roleObject] = association; } else { changedAssociationByRole[roleObject] = NullableArraySet.Remove(previousAssociation, roleObject); } } else { DynamicObject[] roles = ((IEnumerable <DynamicObject>)normalizedRole)?.ToArray() ?? Array.Empty <DynamicObject>(); DynamicObject[] previousRoles = (DynamicObject[])previousRole ?? Array.Empty <DynamicObject>(); // Use Diff (Add/Remove) IEnumerable <DynamicObject> addedRoles = roles.Except(previousRoles); IEnumerable <DynamicObject> removedRoles = previousRoles.Except(roles); foreach (DynamicObject addedRole in addedRoles) { this.AddRole(association, roleType, addedRole); } foreach (DynamicObject removeRole in removedRoles) { this.RemoveRole(association, roleType, removeRole); } } } }