Пример #1
0
        public void RecommendAFriend(int referrerId, NewAccount friendsAccountDetails)
        {
            Validate(friendsAccountDetails);

            // most technologies have similar transaction APIs
            using (var transaction = new System.Transactions.TransactionScope())
            {
                try
                {
                    // customerDirectory is a domain repository
                    var referrer = customerDirectory.Find(referrerId);
                    var friend   = customerDirectory.Add(friendsAccountDetails);
                    // referAFriend policy is a domain policy
                    referAFriendPolicy.Apply(referrer, friend);

                    transaction.Complete();

                    // log here to keep to avoid cluttering domain
                    logger.Debug("Successful friend recommendation");
                    StatsdClient.Metrics.Counter("friendReferrals");
                }
                catch (ReferralRejectedDueToLongTermOutstandingBalance ex)
                {
                    logger.Error(ex);
                    StatsdClient.Metrics.Counter("ReferralRejected");
                    throw new ApplicationError(
                              "Sorry, this referral cannot be completed. The referrer " +
                              "currently has an outstanding balance. Please contact " +
                              "customer support"
                              );
                    // transaction will roll back if Complete() not called
                }
            }
        }
Пример #2
0
        public void RecommendAFriend(int referrerId, NewAccount friendsAccountDetails)
        {
            Validate(friendsAccountDetails);

            using (var transaction = new System.Transactions.TransactionScope())
            {
                try
                {
                    var referrer = customerDirectory.Find(referrerId);
                    var friend   = customerDirectory.Add(friendsAccountDetails);
                    referAFriendPolicy.Apply(referrer, friend);

                    transaction.Complete();

                    var msg = new CustomerRegisteredViaReferralPolicy
                    {
                        ReferrerId = referrerId,
                        FriendId   = friend.Id
                    };
                    bus.Publish(msg);
                }
                catch (ReferralRejectedDueToLongTermOutstandingBalance)
                {
                    var msg = new ReferralSignupRejected
                    {
                        ReferrerId  = referrerId,
                        FriendEmail = friendsAccountDetails.Email,
                        Reason      = "Referrer has long term outstanding balance"
                    };
                    bus.Publish(msg);
                }
            }
        }