Tuesday, November 22, 2011

refactoring last week's work to accommodate the serialization of collections that make their way from C# to jQuery

Want to see what serializing a collection in C# to hand it to jQuery looks like? Well, I refactored the last two methods in ProgramPlanShuffleListFiltrationHelper to look like so:

   private void FlattenProgramPlansForPrograms(IEnumerable<Program> programs)

   {

      Dictionary<string, Guid> dictionary = new Dictionary<string, Guid>();

      foreach (Program program in programs)

      {

         List<ProgramPlan> dummyList = _programPlansForPrograms[program];

         foreach (ProgramPlan programPlan in dummyList)

         {

            string name = program.Name + "." + programPlan.NameStaticAttribute

                  .GetSelectedValueWithDefault(programPlan.Id).Value;

            dictionary.Add(name,programPlan.Id);

         }

      }

      var structure = dictionary.Select(d => new { Plan = d.Key, Id = d.Value });

      FlattenedListOfListsOfProgramPlansForPrograms =

            Newtonsoft.Json.JsonConvert.SerializeObject(structure);

   }

   

   private void FlattenProgramPlansForSampleTypes(IEnumerable<SampleType>

      sampleTypes)

   {

      List<List<string>> collection = new List<List<string>>();

      foreach (SampleType sampleType in sampleTypes)

      {

         List<ProgramPlan> dummyList = _programPlansForSampleTypes[sampleType];

         foreach (ProgramPlan programPlan in dummyList)

         {

            string name = programPlan.Program.Name + "." +

                  programPlan.NameStaticAttribute

                  .GetSelectedValueWithDefault(programPlan.Id).Value;

            string type = sampleType.Category.Name + "." + sampleType.Name;

            collection.Add(new List<string> { name, type });

         }

      }

      var structure = collection.Select(c => new { Plan = c[0], Type = c[1] });

      FlattenedListOfListsOfProgramPlansForSampleTypes =

            Newtonsoft.Json.JsonConvert.SerializeObject(structure);

   }

 
 

Want to see what the JSON going over the seam looks like? I had to change the two assert statements at the end of ProgramPlanShuffleListFiltrationHelperTest to be like this in order to make my test pass. (I did not otherwise need to modify the test.)

  1. Assert.AreEqual(programPlanShuffleListFiltrationHelper.FlattenedListOfListsOfProgramPlansForPrograms, "[{\"Plan\":\"MoneyMaker.Alpha\",\"Id\":\"" + alpha.Id + "\"},{\"Plan\":\"MoneyMaker.Beta\",\"Id\":\"" + beta.Id + "\"},{\"Plan\":\"MoneyMaker.Gamma\",\"Id\":\"" + gamma.Id + "\"},{\"Plan\":\"MoneyMaker.Delta\",\"Id\":\"" + delta.Id + "\"},{\"Plan\":\"CrowdPleaser.Epsilon\",\"Id\":\"" + epsilon.Id + "\"},{\"Plan\":\"CrowdPleaser.Zeta\",\"Id\":\"" + zeta.Id + "\"},{\"Plan\":\"CrowdPleaser.Eta\",\"Id\":\"" + eta.Id + "\"}]");
  2. Assert.AreEqual(programPlanShuffleListFiltrationHelper.FlattenedListOfListsOfProgramPlansForSampleTypes, "[{\"Plan\":\"MoneyMaker.Alpha\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"MoneyMaker.Beta\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"MoneyMaker.Gamma\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"CrowdPleaser.Epsilon\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"CrowdPleaser.Zeta\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"CrowdPleaser.Eta\",\"Type\":\"Silicon.CPU\"},{\"Plan\":\"MoneyMaker.Alpha\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"MoneyMaker.Gamma\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"MoneyMaker.Delta\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"CrowdPleaser.Epsilon\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"CrowdPleaser.Zeta\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"CrowdPleaser.Eta\",\"Type\":\"Silicon.GPU\"},{\"Plan\":\"MoneyMaker.Alpha\",\"Type\":\"Silicon.Chipset\"},{\"Plan\":\"CrowdPleaser.Eta\",\"Type\":\"Silicon.Chipset\"}]");

 
 

What do I need to do to wrangle the JSON on the jQuery side? I'm glad you asked! Below is a refactoring of the beginning of the "logic" method:

function logic(programsList, sampleTypesList, currentSampleType, currentPrograms) {

   var truncatedList = new Array();

   var $counterForSampleTypes = 0;

   var $counterForSampleTypeMatches = 0;

   $(sampleTypesList).each(function () {

      var presentType = this.Type;

      if (presentType == currentSampleType) {

         $counterForSampleTypeMatches++;

      }

      $counterForSampleTypes++;

   });

   var dummyStringForCreatingEmptyStringArray = 'replace me';

   if ($counterForSampleTypeMatches > 1) {

      var $counterForCreatingEmptyStringArray = 1;

      while ($counterForCreatingEmptyStringArray < $counterForSampleTypeMatches) {

         dummyStringForCreatingEmptyStringArray += ',replace me';

         $counterForCreatingEmptyStringArray++;

      }

   }

   truncatedList = dummyStringForCreatingEmptyStringArray.split(',');

   if ($counterForSampleTypeMatches > 0) {

      $counterForSampleTypes = 0;

      var $counterForPopulatingStringArray = 0;

      $(sampleTypesList).each(function () {

         var presentType = this.Type;

         var presentPlan = this.Plan;

         if (presentType == currentSampleType) {

            truncatedList[$counterForPopulatingStringArray] = presentPlan;

            $counterForPopulatingStringArray++;

         }

         $counterForSampleTypes++;

      });

   }

No comments:

Post a Comment