Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 internal void AddObject(DynamicObject newObject)
 {
     this.Objects = NullableArraySet.Add(this.Objects, newObject);
 }