Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:04:56+00:00 2026-06-13T07:04:56+00:00

Lately I’ve been tinkering with TDD and coding based on the SOLID principles .

  • 0

Lately I’ve been tinkering with TDD and coding based on the SOLID principles . I have a scenario as follows:

  • One can have IRecurringProfile, which performs a series of payments on an interval, e.g monthly basis
  • When a payment tries to go through and it fails, a IRecurringProfileTransaction is created linked to the IRecurringProfilePayment to show that the payment did not go through.
  • The failure count for the payment is incremented.
  • The date/time for which to retry payment and send another failure notification (if payment is failed) is also updated.
  • If the failure count for a payment reaches a maximum threshold (e.g 3 failed attempts), the IRecurringProfile is suspended.
  • Also, with each failure, a notification is sent to inform the client that the payment did not go through, and will be retried again.

Below is some sample code I created, which mainly deals with the task of marking a recurring profile payment as failed. I tried to follow on the SOLID principles, as well as constructor injection. Would like to know if this is violating any of these principles or any programming best-practices, and subject the code to any form of scrutiny so that it can be improved. The code also uses NHibernate as the ORM.

public class RecurringPaymentMarkAsFailure
{
    private readonly IPaymentFailedNotificationSender paymentFailedNotificationSender;
    private readonly IRecurringProfileFailureNextNotificationDateUpdater failureNotificationDateUpdater;
    private readonly IRecurringProfileSuspender recurringProfileSuspender;

    public RecurringPaymentMarkAsFailure(IPaymentFailedNotificationSender paymentFailedNotificationSender, IRecurringProfileSuspender recurringProfileSuspender,
        IRecurringProfileFailureNextNotificationDateUpdater failureNotificationDateUpdater)
    {
        this.paymentFailedNotificationSender = paymentFailedNotificationSender;
        this.failureNotificationDateUpdater = failureNotificationDateUpdater;

        this.recurringProfileSuspender = recurringProfileSuspender;

    }

    private void checkProfileStatus(IRecurringProfile profile)
    {
        if (profile.Status != Enums.RecurringProfileStatus.Active)
        {
            throw new Exceptions.RecurringProfileException("This cannot be called when the profile is not marked as active");
        }
    }


    private void incrementFailureCount(IRecurringProfilePayment payment)
    {
        payment.FailureCount++;
    }

    public IRecurringProfileTransaction MarkPaymentAsFailed(IRecurringProfilePayment payment, string failureData)
    {
        using (var t = BeginTransaction())
        {
            checkProfileStatus(payment.RecurringProfile);


            IRecurringProfileTransaction transaction = payment.Transactions.CreateNewItem();
            transaction.OtherData = failureData;
            transaction.Status = Enums.RecurringProfileTransactionStatus.Failure;
            paymentFailedNotificationSender.CreateAndQueueNotification(transaction);
            failureNotificationDateUpdater.UpdateNextFailureNotificationDate(payment);
            incrementFailureCount(payment);

            if (payment.FailureCount >= payment.RecurringProfile.MaximumFailedAttempts)
            {
                recurringProfileSuspender.SuspendRecurringProfile(payment.RecurringProfile);
            }
            transaction.Save();
            t.Commit();
            return transaction;
        }

    }

}

—

As a side note, this question complements my most recent post about a similar topic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:04:57+00:00Added an answer on June 13, 2026 at 7:04 am

    As I see it, you are violating the Single Responsibility Principle, since your RecurringPaymentMarkAsFailure has two responsibilities. Besides the responsibility of makring the payment as a failure, it also adds the responsibility of managing the transaction (the using (BeginTransaction); that is a cross-cutting concern.

    You will probably have many classes like these in the system that handle business logic, and probably all (or many) have this exact same transaction code. Instead, consider adhering to the Open/Closed Principle by allowing to add this behavior as decorator. This is well possible, because the transaction is the first AND last operation in this code. A naive implementation of this decorator could look like this:

    public class TransactionRecurringPaymentMarkAsFailureDecorator
        : RecurringPaymentMarkAsFailure
    {
        private RecurringPaymentMarkAsFailure decoratedInstance;
    
        public RecurringPaymentMarkAsFailure(
            RecurringPaymentMarkAsFailure decoratedInstance)
        {
            this.decoratedInstance = decoratedInstance;
        }
    
        public override IRecurringProfileTransaction MarkPaymentAsFailed(
            IRecurringProfilePayment payment, string failureData)
        {
            using (var t = BeginTransaction())
            {
                var transaction = this.decoratedInstance
                    .MarkPaymentAsFailed(payment, failureData);
    
                t.Commit();
    
                return transaction;
            }
        }
    }
    

    This decorator allows you to wrap the class as follows:

    var marker =
        new TransactionRecurringPaymentMarkAsFailureDecorator(
            new RecurringPaymentMarkAsFailure(
                /* dependencies */    
            ));
    

    As I said this implementation is a bit naive, since you will probably have many classes that need to be wrapped, and this means that each class would get its own decorator. Although this is completely SOLID, it isn’t DRY.

    Instead, let all classes that execute use cases implement a single generic interface, something like ICommandHandler<TCommand>. This allows you to create a single generic TransactionCommandHandlerDecorator<TCommand> class to wrap all those instances. Take a look at this article to learn more about this model: Meanwhile… on the command side of my architecture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lately I have been designing and coding a website for one of my clients.
Lately I have been trying my hands on Eclipse IDE for java development. I
Lately I have been experiencing Eclipse Galileo (3.5) slowing down under Java 1.5b12 OR
Lately I have been reading about the Rack architecture in Passenger/Rails, and how it
Lately I've been wondering if there's a difference between initializing the variables that have
Lately I've been interested in representing uncompressed bitmaps in memory. However, one thing I'm
Lately I have been having more and more issues with the content assist in
Lately, I have been trying to compile php on my old PPC mac running
Lately I have been playing around with intents and bundles. I thought I had
Lately, I've been reading much about constructors from the well-received C++ FAQ . One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.