Exemple #1
0
        /// <summary>
        /// Delete relations.
        /// </summary>
        /// <param name="dataProvider">Implementation of the data provider used to access data.</param>
        public virtual void DeleteRelations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider)
        {
            ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider));

            if (Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, Identifier, "Identifier"));
            }

            foreach (IHouseholdMemberProxy affectedHouseholdMember in MemberOfHouseholdProxy.DeleteMemberOfHouseholds(dataProvider, this))
            {
                HandleAffectedHouseholdMember(dataProvider, affectedHouseholdMember);
            }

            StorageProxy.DeleteStorages(dataProvider, Identifier.Value);

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }
Exemple #2
0
        /// <summary>
        /// Save relations.
        /// </summary>
        /// <param name="dataProvider">Implementation of the data provider used to access data.</param>
        /// <param name="isInserting">Indication of whether we are inserting or updating.</param>
        public virtual void SaveRelations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, bool isInserting)
        {
            ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider));

            if (Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, Identifier, "Identifier"));
            }

            IEnumerable <IHouseholdMember> householdMemberCollection = base.HouseholdMembers.ToArray(); // Using base.HouseholdMembers will not force the proxy to reload the household member collection.
            IHouseholdMember householdMemberWithoutIdentifier        = householdMemberCollection.FirstOrDefault(householdMember => householdMember.Identifier.HasValue == false);

            if (householdMemberWithoutIdentifier != null)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, householdMemberWithoutIdentifier.Identifier, "Identifier"));
            }

            IList <MemberOfHouseholdProxy> existingMemberOfHouseholdCollection = new List <MemberOfHouseholdProxy>(MemberOfHouseholdProxy.GetMemberOfHouseholds(dataProvider, this));

            foreach (IHouseholdMember householdMember in householdMemberCollection.Where(m => m.Identifier.HasValue))
            {
                if (existingMemberOfHouseholdCollection.Any(existingMemberOfHousehold => existingMemberOfHousehold.HouseholdMemberIdentifier == householdMember.Identifier))
                {
                    continue;
                }

                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    MemberOfHouseholdProxy memberOfHouseholdProxy = new MemberOfHouseholdProxy(householdMember, this)
                    {
                        Identifier = Guid.NewGuid()
                    };
                    existingMemberOfHouseholdCollection.Add(subDataProvider.Add(memberOfHouseholdProxy));
                }
            }

            while (_removedHouseholdMemberCollection.Count > 0)
            {
                IHouseholdMember householdMemberToRemove = _removedHouseholdMemberCollection.First();
                if (householdMemberToRemove.Identifier.HasValue == false)
                {
                    _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
                    continue;
                }

                MemberOfHouseholdProxy memberOfHouseholdProxyToRemove = existingMemberOfHouseholdCollection.SingleOrDefault(existingMemberOfHousehold => existingMemberOfHousehold.HouseholdMemberIdentifier == householdMemberToRemove.Identifier);
                if (memberOfHouseholdProxyToRemove == null)
                {
                    _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
                    continue;
                }

                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    subDataProvider.Delete(memberOfHouseholdProxyToRemove);
                }
                HandleAffectedHouseholdMember(dataProvider, memberOfHouseholdProxyToRemove.HouseholdMember as IHouseholdMemberProxy);
                _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
            }

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }