2

I am trying to make an invoice management system for our company. I am a little bit confused because of design principles. SOLID

Lets say that a class takes care of invoices: InvoiceProcessor

InvoiceProcessor ip=new InvoiceProcessor(DraftHTML);
ip.Customer=theCustomer;
ip.Order=theOrder;
ip.Prepare();

After this, which approach is better? And why?

A)

ip.SaveToFile(fileName);
ip.SendToCustomer();

ip.DBConnection=myActiveConnection;
ip.LoadFromDB(invoiceID);
ip.SaveToDB();

B)

SaveToFile(fileName,ip.GetHTML());
SendEmail(ip.Customer,ip.GetHTML());

ip.InvoiceInfo=LoadFromDB(invoiceID);
SaveToDB(ip.InvoiceInfo);
Cœur
  • 37,241
  • 25
  • 195
  • 267
mustafa öztürk
  • 539
  • 3
  • 13
  • To me -- it makes more sense for the domain, InvoiceProcessor, to deal with things related to processing an invoice. Therefore, option B makes the most sense. – Ian P May 28 '14 at 13:26
  • 1
    I see a lot of responsibilities (preparing/creating-an-invoice, loading, saving, sending-email) and only a single class - This breaks SOLID already. – Maarten May 28 '14 at 13:34

2 Answers2

4

Imho, both A) and B) break the SRP principle, although B) is less coupled.

I would approach this in another way:

  1. Create a new type which represents an Invoice
  2. Make InvoiceProcessor return an instance of Invoice
  3. For each post processing step create a different type of action.

The code should look like this:

var ip = new InvoiceProcessor
{
    Customer = theCustomer,
    Order = theOrder
};
var invoice = ip.CreateInvoice();
// Post processing
HtmlPrinter.PrintInvoice(invoice, htmlFileName);
DataAccess.SaveInvoice(invoice);
MailService.SendInvoiceToCustomer(invoice, theCustomer.Email);
RePierre
  • 9,358
  • 2
  • 20
  • 37
0

B option is better because you can use your methods in various ways. If you choose your first suggestion, in the future if you want to write a code let's say for your dispatches, then you have to write same code again for dispatch part . That will cause a bad pattern in your code. You should avoid repetitive code as possible as you can.

Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17