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);
Imho, both A) and B) break the SRP
principle, although B) is less coupled.
I would approach this in another way:
Invoice
InvoiceProcessor
return an instance of Invoice
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);