0
Answer

Refactor code

Anonymous

Anonymous

13y
1.3k
1
I need some help with refactoring code.

IEnumerable<OrderChange> parentChanges = null; 
foreach (OrderChange orderChange in changes) 
 

 
  if (orderChange.Entity is IParentBL)  
 
  { 
 
   parentChanges =  
 
    changes.Where(orderChange.Entity is IParentBL) 
 
  }  
 
  switch (orderChange.Status) 
 
  { 
 
  case Status.A: 
 
  { 
 
   ISiblingBL siblibgBL = bl as ISiblingBL; 
 
 
 
   if (siblibgBL != null) 
 
   { 
 
   siblingBL.Ship(orderChange); 
 
   } 
 
   else if (bl is IChildBL) 
 
   { 
 
    (bl as IChildBL).Ship(orderChange)    
 
   } 
 
   else if (bl is IParentBL) 
 
   { 
 
   (bl as IParentBL).Ship(parentChanges); 
 
   } 
 
   break; 
 
  } 
 
  case Status.B: 
 
  { 
 
   if (bl is IParentBL) 
 
   { 
 
   (bl as IParentBL).ReturnShipment(parentChanges); 
 
   } 
 
   else 
 
   { 
 
   bl.ReturnShipment(orderCHange); 
 
   } 
 
 
 
   break; 
 
  } 
 
  case Status.C: 
 
  { 
 
   if (bl is IParentBL) 
 
   { 
 
   //do something 
 
   } 
 
   else 
 
   { 
 
   //do something 
 
   } 
 
 
 
 
 
  } 
 
  } 
 
 
 
  if (bl is IParentBL) 
 
  { 
 
  foreach (OrderChange change in parentChanges) 
 
  { 
 
        result.Add( 
 
   change.Id, change.Value); 
 
 
 
   } 
 
  } 
 
  else 
 
  { 
 
  result.Add( 
 
   orderChange.Id, orderChange.Value); 
 
 
 
  } 
 

 

IParentBL, ISiblingBL and IChildBL are all interfaces which could be implemented by any class that is an IOrderChangeBL. I want to switch on the status and check for the interface type so that i can call the appropriate method in the interface. OrderChange has an entity property which holds the instance of the class which implements one of these interfaces. OrderChange also has a status property.

Please refer to the snippet above.
I want to loop through changes collection and check the type of the entity object. if object type is IParentBL then i collect all orderchanges that are of the same type and store it in a temp variable.

Depending on the status of the OrderChange object and the type of the entity object i need to do something. If entity is IParentBL then i always use the collection which is stored in the temp variable.

if not then orderChange object is used