public override TDataObject GetObject <TDataObject>(TDataObject objectToGet)
        {
            int?objectToGetInternalId;

            if (objectToGet.IsNew)
            {
                objectToGetInternalId = objectToGet.InternalObjectId;
            }
            else
            {
                if ((objectToGet as GOGroupRoleDataObject) == null)
                {
                    _logEngine.LogError("Unable to get value which value is null", "The object you are trying to get doesn't have a value", "GOGroupRoleObjectsDataSet", null);
                    throw new PulpException("Unable to get an element which value is null.");
                }
                objectToGetInternalId = GOGroupRoleObjectInternalIds.ContainsKey((objectToGet as GOGroupRoleDataObject).PrimaryKeysCollection) ? (int?)GOGroupRoleObjectInternalIds[(objectToGet as GOGroupRoleDataObject).PrimaryKeysCollection] : null;
            }
            if (objectToGetInternalId != null)
            {
                return(GOGroupRoleObjects.ContainsKey((int)objectToGetInternalId) ? GOGroupRoleObjects[(int)objectToGetInternalId] as TDataObject : null);
            }

            return(null);
        }
        public override void RemoveObject(IDataObject objectToRemove)
        {
            if (GOGroupRoleObjects == null)
            {
                return;
            }
            bool completed;
            int? objectToRemoveInternalId;

            if ((objectToRemove as GOGroupRoleDataObject) == null)
            {
                _logEngine.LogError("Unable to remove null object", "The object you are trying to remove is null", "GOGroupRoleObjectsDataSet.RemoveObject", null);
                throw new PulpException("Unable to remove Null Object.");
            }

            if (objectToRemove.IsNew)
            {
                objectToRemoveInternalId = objectToRemove.InternalObjectId;
            }
            else
            {
                objectToRemoveInternalId = GOGroupRoleObjectInternalIds.ContainsKey((objectToRemove as GOGroupRoleDataObject).PrimaryKeysCollection) ? (int?)GOGroupRoleObjectInternalIds[(objectToRemove as GOGroupRoleDataObject).PrimaryKeysCollection] : null;
            }

            if (objectToRemoveInternalId != null)
            {
                GOGroupRoleDataObject value;
                completed = false;
                var count = 0;
                while (!completed && count++ < 15)
                {
                    completed = GOGroupRoleObjects.TryRemove((int)objectToRemoveInternalId, out value);
                }

                // Reinit InternalObjectId only if the object to remove is part of the current dataset
                if (ReferenceEquals(objectToRemove.ObjectsDataSet, this._rootObjectDataSet))
                {
                    objectToRemove.InternalObjectId = null;
                }

                if (!objectToRemove.IsNew)
                {
                    int idvalue;
                    completed = false;
                    count     = 0;
                    while (!completed && count++ < 15)
                    {
                        completed = GOGroupRoleObjectInternalIds.TryRemove((objectToRemove as GOGroupRoleDataObject).PrimaryKeysCollection, out idvalue);
                    }
                }

                // Delete the Group FK Index
                if ((objectToRemove as GOGroupRoleDataObject).GOGroupName != null)
                {
                    if (Group_FKIndex.ContainsKey((objectToRemove as GOGroupRoleDataObject).GOGroupName) && Group_FKIndex[(objectToRemove as GOGroupRoleDataObject).GOGroupName].Contains((int)objectToRemoveInternalId))
                    {
                        Group_FKIndex[(objectToRemove as GOGroupRoleDataObject).GOGroupName].Remove((int)objectToRemoveInternalId);

                        if (!Group_FKIndex[(objectToRemove as GOGroupRoleDataObject).GOGroupName].Any())
                        {
                            List <int> outvalue;
                            var        iscompleted = false;
                            var        count2      = 0;
                            while (!iscompleted && count2++ < 15)
                            {
                                iscompleted = Group_FKIndex.TryRemove((objectToRemove as GOGroupRoleDataObject).GOGroupName, out outvalue);
                            }
                        }
                    }

                    GOGroupDataObject relatedGroup;
                    if ((objectToRemove as GOGroupRoleDataObject)._group_NewObjectId != null)
                    {
                        relatedGroup = _rootObjectDataSet.GetObject(new GOGroupDataObject()
                        {
                            IsNew = true, InternalObjectId = (objectToRemove as GOGroupRoleDataObject)._group_NewObjectId
                        });
                    }
                    else
                    {
                        relatedGroup = _rootObjectDataSet.GetObject(new GOGroupDataObject((objectToRemove as GOGroupRoleDataObject).GOGroupName)
                        {
                            IsNew = false
                        });
                    }

                    if (relatedGroup != null && this.RootObjectDataSet.NotifyChanges)
                    {
                        relatedGroup.NotifyPropertyChanged("GroupRoleItems", new SeenObjectCollection());
                    }
                }

                // Delete the Role FK Index
                if ((objectToRemove as GOGroupRoleDataObject).GORoleName != null)
                {
                    if (Role_FKIndex.ContainsKey((objectToRemove as GOGroupRoleDataObject).GORoleName) && Role_FKIndex[(objectToRemove as GOGroupRoleDataObject).GORoleName].Contains((int)objectToRemoveInternalId))
                    {
                        Role_FKIndex[(objectToRemove as GOGroupRoleDataObject).GORoleName].Remove((int)objectToRemoveInternalId);

                        if (!Role_FKIndex[(objectToRemove as GOGroupRoleDataObject).GORoleName].Any())
                        {
                            List <int> outvalue;
                            var        iscompleted = false;
                            var        count2      = 0;
                            while (!iscompleted && count2++ < 15)
                            {
                                iscompleted = Role_FKIndex.TryRemove((objectToRemove as GOGroupRoleDataObject).GORoleName, out outvalue);
                            }
                        }
                    }

                    GORoleDataObject relatedRole;
                    if ((objectToRemove as GOGroupRoleDataObject)._role_NewObjectId != null)
                    {
                        relatedRole = _rootObjectDataSet.GetObject(new GORoleDataObject()
                        {
                            IsNew = true, InternalObjectId = (objectToRemove as GOGroupRoleDataObject)._role_NewObjectId
                        });
                    }
                    else
                    {
                        relatedRole = _rootObjectDataSet.GetObject(new GORoleDataObject((objectToRemove as GOGroupRoleDataObject).GORoleName)
                        {
                            IsNew = false
                        });
                    }

                    if (relatedRole != null && this.RootObjectDataSet.NotifyChanges)
                    {
                        relatedRole.NotifyPropertyChanged("GroupRoleItems", new SeenObjectCollection());
                    }
                }
            }
        }
        public override void AddObject(IDataObject objectToAdd, bool replaceIfExists)
        {
            var existingObject = GetObject(objectToAdd);

            if (!replaceIfExists && existingObject != null)
            {
                throw new PulpException("Object already exists");
            }

            int newInternalId;

            if (existingObject != null)
            {
                //RemoveObject(existingObject);
                if (existingObject.InternalObjectId == null)
                {
                    _logEngine.LogError("Error while trying to Add Object to the GOGroupRoleObjectsDataSet", "The object you are trying to add doesn't have an InternalObjectId", "GOGroupRoleObjectsDataSet", null);
                    throw new PulpException("Error while trying to add an object to the dataset without InternalObjectId");
                }
                newInternalId = (int)existingObject.InternalObjectId;
                objectToAdd.InternalObjectId = newInternalId;
                existingObject.CopyValuesFrom(objectToAdd, false);
            }
            else
            {
                newInternalId = GetNextNewInternalObjectId();
                objectToAdd.InternalObjectId = newInternalId;

                var completed = false;
                var count     = 0;
                while (!completed && count++ < 15)
                {
                    completed = GOGroupRoleObjects.TryAdd(newInternalId, (GOGroupRoleDataObject)objectToAdd);
                }
            }

            if (!objectToAdd.IsNew && existingObject == null)
            {
                //The following if should not be necessary...
                var completed = false;
                if (GOGroupRoleObjectInternalIds.ContainsKey(((GOGroupRoleDataObject)objectToAdd).PrimaryKeysCollection))
                {
                    int value;
                    var count2 = 0;
                    while (!completed && count2++ < 15)
                    {
                        completed = GOGroupRoleObjectInternalIds.TryRemove(((GOGroupRoleDataObject)objectToAdd).PrimaryKeysCollection, out value);
                    }
                }

                completed = false;
                var count = 0;
                while (!completed && count++ < 15)
                {
                    completed = GOGroupRoleObjectInternalIds.TryAdd(((GOGroupRoleDataObject)objectToAdd).PrimaryKeysCollection, newInternalId);
                }
            }
            // Update relations including platform as "many" side or "one" side , pk side for one to one relations
            if ((objectToAdd as GOGroupRoleDataObject) == null)
            {
                _logEngine.LogError("Unable to Add an object which is null", "Unable to add an object which is null", "GOGroupRoleDataObject", null);
                throw new PulpException("Unexpected Error: Unable to Add an object which is Null.");
            }

            // Update the Group FK Index
            if ((objectToAdd as GOGroupRoleDataObject).GOGroupName != null)
            {
                if (!Group_FKIndex.ContainsKey((objectToAdd as GOGroupRoleDataObject).GOGroupName))
                {
                    var iscompleted = false;
                    var count2      = 0;
                    while (!iscompleted && count2++ < 15)
                    {
                        iscompleted = Group_FKIndex.TryAdd((objectToAdd as GOGroupRoleDataObject).GOGroupName, new List <int>());
                    }
                }

                if (!Group_FKIndex[(objectToAdd as GOGroupRoleDataObject).GOGroupName].Contains(newInternalId))
                {
                    Group_FKIndex[(objectToAdd as GOGroupRoleDataObject).GOGroupName].Add(newInternalId);
                }

                GOGroupDataObject relatedGroup;
                if ((objectToAdd as GOGroupRoleDataObject)._group_NewObjectId != null)
                {
                    relatedGroup = _rootObjectDataSet.GetObject(new GOGroupDataObject()
                    {
                        IsNew = true, InternalObjectId = (objectToAdd as GOGroupRoleDataObject)._group_NewObjectId
                    });
                }
                else
                {
                    relatedGroup = _rootObjectDataSet.GetObject(new GOGroupDataObject((objectToAdd as GOGroupRoleDataObject).GOGroupName)
                    {
                        IsNew = false
                    });
                }

                if (relatedGroup != null && this.RootObjectDataSet.NotifyChanges)
                {
                    relatedGroup.NotifyPropertyChanged("GroupRoleItems", new SeenObjectCollection());
                }
            }

            // Update the Role FK Index
            if ((objectToAdd as GOGroupRoleDataObject).GORoleName != null)
            {
                if (!Role_FKIndex.ContainsKey((objectToAdd as GOGroupRoleDataObject).GORoleName))
                {
                    var iscompleted = false;
                    var count2      = 0;
                    while (!iscompleted && count2++ < 15)
                    {
                        iscompleted = Role_FKIndex.TryAdd((objectToAdd as GOGroupRoleDataObject).GORoleName, new List <int>());
                    }
                }

                if (!Role_FKIndex[(objectToAdd as GOGroupRoleDataObject).GORoleName].Contains(newInternalId))
                {
                    Role_FKIndex[(objectToAdd as GOGroupRoleDataObject).GORoleName].Add(newInternalId);
                }

                GORoleDataObject relatedRole;
                if ((objectToAdd as GOGroupRoleDataObject)._role_NewObjectId != null)
                {
                    relatedRole = _rootObjectDataSet.GetObject(new GORoleDataObject()
                    {
                        IsNew = true, InternalObjectId = (objectToAdd as GOGroupRoleDataObject)._role_NewObjectId
                    });
                }
                else
                {
                    relatedRole = _rootObjectDataSet.GetObject(new GORoleDataObject((objectToAdd as GOGroupRoleDataObject).GORoleName)
                    {
                        IsNew = false
                    });
                }

                if (relatedRole != null && this.RootObjectDataSet.NotifyChanges)
                {
                    relatedRole.NotifyPropertyChanged("GroupRoleItems", new SeenObjectCollection());
                }
            }
        }