Skip to main content
3 of 4
added 52 characters in body
SiD
  • 597
  • 2
  • 8

Readability

  • Use namespace CompanyX instead of using in every method's return type.
  • Create single instance of InvoiceTableAdapter and SqlConnection and reuse it.
  • In UpdateInvoice method, instead of passing multiple properties, pass data model itself and use properties which are required inside 'Update1' method.
  • You will need to put the cleanup of the connection in a Dispose() after making the data access class IDisposable.

Not sure why you're using 2 SQL connections. I would suggest you to use only one.

After refactoring code should look like this:

    using CompanyX;
    using CompanyX.DataAccess.CompanyXTableAdapters;

    namespace CompanyX.DataAccess
    {
        public class CompanyXDataAccess : IDisposable
        {
            InvoiceTableAdapter ta = new InvoiceTableAdapter();
            SqlConnection readerConnection = new SqlConnection(DB.GetReaderConnectionString());
            SqlConnection writerConnection = new SqlConnection(DB.GetWriterConnectionString());
        
            public InvoiceDataTable GetInvoiceByID(int ID)
            {
                ta.Connection = readerConnection;
                return ta.GetByID(ID);
            }

            public InvoiceDataTable GetInvoice(string userSID)
            {
                ta.Connection = readerConnection;
                return ta.Get(userSID);
            }

            public void DeleteInvoice(int ID)
            {
                ta.Connection = writerConnection;
                ta.Delete1(ID);
            }

            public void UpdateInvoice(Invoice m)
            {
                ta.Connection = writerConnection                                    
                ta.Update1(m);
            }

            public void Dispose()
            {
                // Do clean up
            }
        }
    }
SiD
  • 597
  • 2
  • 8