Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The following snippet of code contains two nested try blocks that try to find information in table "a", and if that fails, tries to find the same info in table "b". The two 'try's are identical except for the table that is queried and the value assigned to 'whereFound'. I know I need to encapsulate what varies, but I can't quite see how to do it on my own.

C#
try
{
   #region Try to find the Order info in planned orders.
   dcReplication dc = new dcReplication(String.Format(
      "Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                                        replicationServer,
                                        replicationDatabase,
                                        replicationUsername,
                                        replicationPassword));

   // Find the Bill To and Ship To Customer Numbers in the
   //Sales Order Master table.
   var q1 = (from a in dc.SalesOrderMasterTable
            where a.SalesOrderNumber == orderNumber
            select new { a.BillToCustomerNumber,
                         a.ShipToCustomerNumber }).ToList();

   foreach (var r in q1)
   {  shipToCustomerNumber = r.ShipToCustomerNumber;
      billToCustomerNumber = r.BillToCustomerNumber;
   }

   if (q1.Count > 0)
   {  whereFound = "Planned";
      return orderNumber;
   }
   #endregion
   else
   {
      try
      #region Try to find Order in packed orders.
      {  var q2 = (from a in dc.InvoiceHistoryTable
                   where a.OrderNumber == orderNumber
                   select new { a.BillToCustomerNumber ,
                                a.ShipToCustomerNumber }).Distinct()
                                .ToList();

         foreach (var r in q2)
         {  shipToCustomerNumber = r.ShipToCustomerNumber;
            billToCustomerNumber = r.BillToCustomerNumber;
         }

         if (q2.Count > 0)
         {  whereFound = "Packed";
            return orderNumber;
         }
         else
         {  throw new RowNotInTableException(String.Format
            ("Are you sure you entered the Order Number correctly?  Order '{0}' could not be found in Sales Order Master or Invoice History.",
                                             orderNumber));
         }
      }
      #endregion
Posted
Comments
Jameel VM 12-Sep-13 14:49pm    
did you mean optimizing the code? or which data you need to be encapsulate?
J Melkerson 13-Sep-13 7:08am    
After I posted the question, I realized I didn't ask it very well. I want to refactor (or apply a pattern or something) the two try blocks such that I can pass "SalesOrderMasterTable" (or something that resolves to it) into the first block and then pass "InvoiceHistoryTable" into the second block thereby eliminating the duplicate code.

I want to eliminate the duplicate code but the names of the tables being queried are in the linq query statements and I don't know how to pass table names as variables to linq queries.

Hope that helps clear things up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900