Exemplo n.º 1
0
        /// <summary>
        /// Try a cascading series of attempts to get a matching entity
        /// </summary>
        /// <param name="party"></param>
        private async Task <Party> RefreshParty(Party party)
        {
            // First try by id
            if (party.id.HasValue && party.id.Value != ObjectId.Empty)
            {
                var partyById = await _partyRepository.GetAsync(party.id.Value);

                if (!ReferenceEquals(partyById, null))
                {
                    return(partyById);
                }
            }

            _entityRefreshNeeded = true;

            // Then try a 'full' match
            var partyFull = await _partyRepository.FindAsync(party, false);

            if (!ReferenceEquals(partyFull, null))
            {
                return(partyFull);
            }

            // Try a 'min' match
            var partyMin = await _partyRepository.FindAsync(party, true);

            if (!ReferenceEquals(partyMin, null))
            {
                // Merge any details from the supplied entity into the found one
                // This does NOT merge IsInfrastructureOwner or the child entities of Party
                return(partyMin.Merge(party));
            }

            // There is still no match then save the supplied entity first
            party.id = await _partyRepository.AddOrUpdateAsync(party);

            return(party);
        }