0

I have lately been reading on the Single Responsibility Principle concept, and in theory I agree a lot with it. I am having difficulty coming to terms on which code can be exactly classified as violating the principle. I would like to implement this principle, so as to gear towards Test-Driven Development.

Take for example the code below:

public void MarkAsSuccessful(PaymentMethodSpecific paymentMethod, bool requiresManualIntervention)
    {
            this.Paid = true;
            this.PaidOn = CS.General_v3.Util.Date.Now;
            this.PaidByPaymentMethod = paymentMethod;
            this.RequiresManualIntervention = requiresManualIntervention;

            this.Update();

        this.CreateAndSendNotificationRegardingImmediatePayment();

        this.SendPaymentSuccessfulEmails();

    }

This is placed in a class called PaymentRequest, which basically is a class which handles logic related to payment in an e-commerce application. The method above marks the request as 'successful'. This must mark the columns paid, as well as other information, as well as send a notification that this was successful, and also send emails.

For example, when it comes to unit testing - It is very difficult to unit-test this method as I have no way to know that the notification was actually created and sent, as well as the payment emails have been sent. Would like to know how more experienced people on the SRP concept would approach such an example.

4

1 回答 1

1

我的猜测是你有一个类与你的PaymentProcessor类合并/嵌入PaymentRequest

PaymentProcessor基本上是 aPaymentRequest穿过的有限状态机。

在构建它时PaymentProcessor,您将注入处理“即时付款通知”和一般电子邮件的对象。这样,您可以通过注入断言预期条件发生的模拟对象来独立测试所有状态和处理器。

单一责任本身是一个很棒的概念,但是当您开始整体投资 SOLID 时,力量真的来了。

于 2012-08-23T16:42:16.413 回答