/// <summary> /// Try a cascading series of attempts to get a matching entity /// </summary> /// <param name="address"></param> private async Task <Address> RefreshAddress(Address address) { // First try by id if (address.id.HasValue && address.id.Value != ObjectId.Empty) { var addrById = await _addressRepository.GetAsync(address.id.Value); if (!ReferenceEquals(addrById, null)) { return(addrById); } } _entityRefreshNeeded = true; // Then try a 'full' match var addrFull = await _addressRepository.FindAsync(address, false); if (!ReferenceEquals(addrFull, null)) { return(addrFull); } // Try a 'min' match var addrMin = await _addressRepository.FindAsync(address, true); if (!ReferenceEquals(addrMin, null)) { // Merge any details from the supplied entity into the found one return(addrMin.Merge(address)); } // There is still no match then save the supplied entity first address.id = await _addressRepository.AddOrUpdateAsync(address); return(address); }