public void Register(IPostalUser postalUser) { if (postalUser == null) { throw new ArgumentNullException(nameof(postalUser)); } if (!this._usersByPostalCode.ContainsKey(postalUser.PostalCode)) { this._usersByPostalCode.Add(postalUser.PostalCode, postalUser); } }
public void Send(IPostalUser fromPostalUser, string toPostalCode, Parcel parcel) { if (fromPostalUser == null) { throw new ArgumentNullException(nameof(fromPostalUser)); } if (this._usersByPostalCode.TryGetValue(toPostalCode, out IPostalUser toPostalUser)) { toPostalUser.ReceiveParcel(fromPostalUser.PostalCode, parcel); } // Make sure the sender is not rejecting parcels, otherwise we may end up with an infinite callback. else if (!(fromPostalUser is RejectingParcelUser)) { // Send it back, we couldn't find the recipient fromPostalUser.ReceiveParcel(toPostalCode, parcel); } }