Tuesday, 21 March 2017

c# Dictionary Part3 (Refactoring if else condtion)

12:16 Posted by Nikesh No comments

In this post we will cover refactoring of nested if else in large sensitive codes. Large sensitive here means when we have a code where only conditioned data present of static key ids in large if else file.

To fix this we can again use dictionary as a good part of key value pair of organizing data.

For example suppose we have to work with multiple static id value whose condition has different quantity as whole sellers:

Suppose we have view written like this......
  if(id== 1)
  {
  //calculation for id one
<input type=number" min="10" />
  }
 else if(id == 2)
  {
   //calculation for id two
<input type=number" min="12" />
  }
  else if(id==3)
   {
  //calculation for id three
<input type=number" min="15" />
  }

and so on we have multi nested conditions. how we can refactor those so we can make our code readability better.

Let's see how we can do that.

To get better readability we can create a dictionary function which can return dictionary of lists values

public static Dictionary<string, List<string>> GetMyQuantityInfo()
{

 Create your type of list available based on your conditions.

List<string> pTypeForMin10Quantity = new List<string>() {" id of all products whose quantity need 10" };
List<string> pTypeForMin12Quantity = new List<string>() { "id of all products whose quantity need 12" };
List<string> pTypeForMin50Quantity = new List<string>() {"id of all products whose quantity need 15 "};

// Bind lists to dictionary with some keys as i am using chkfor10,12 and 15 so on

Dictionary<string, List<string>> QuanityInfo = new Dictionary<string, List<string>>();
QuanityInfo.Add("chkfor10", pTypeForMin10Quantity);
QuanityInfo.Add("chkfor12", pTypeForMin12Quantity);
QuanityInfo.Add("chkfor15", pTypeForMin15Quantity);


return QuanityInfo;

}


Now Create another dictionary which says which type of check we have to do



public static Dictionary<string, int> allquantityiformation()

{

Dictionary<string, int> alltypproduct = new Dictionary<string, int>();
alltypproduct.Add("chkfor10", 10);
alltypproduct.Add("chkfor12", 12);
alltypproduct.Add("chkfor15", 15);


return alltypproduct;

}



Now calling, how we can get those conditons:


List<string> ProductIdAndTypesforMinQuantity = new List<string>() {};


var QuantityInfo = Model.GetMyQuantityInfo();

var aqi = Model.allquantityiformation();

//Now pass your type of productid in QuantityInfo.ContainsKey() so we can check what type of quantity information available for my products.

// here i am doing foreach because i have to check based on type of quantity check
//This foreach will go to my type allquantityiformation dictionary and check product in this type of dictionary

//here our ptype will be chkfor10,chkfor12,chkfor15


foreach (string ptype in allquantityifo)

{
if (QuantityInfo.ContainsKey(ptype))
{
//Now get its all list of id in an empty list

//Based on check in each loop get its respective list from dictonary QuantityInfo

ProductIdAndTypesforMinQuantity = QuantityInfo[ptype];

string id = Id; -- Remember this will be our id of our product which we current checking in list for availability, we can get id from product datatable rows. ex: string id = dr["ProductId"].ToString();


if (ProductIdAndTypesforMinQuantity.Contains(id))
{
             //Getting minimum value for chktype currently going

Int64 MinValue = aqi[ptype]; --- this will give us 10 or 12 or 15
   // Binding minimum value current available to input control and set an boolean value  minSetupdon       true
<input value='@dr["Quantity"]' type="number" min="@MinValue" maxlength="2" >
minSetupdone = true;
}
else
{
availabeMin = false;
}

}
else
{
ProductIdAndTypesforMinQuantity = new List<String>() { };

}
}

//Now when avaibility not found set min value 0

if (availabeMin == false && minSetupdone == false)
{
<input value='@dr["Quantity"]' type="number" min="0">
}

In that way we can refactor our large size nested if else conditions easily and makes them more readable.