Search code examples
c#oopdesign-principles

Design suggestion for an invoice application


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);

Solution

  • 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);