Sunday, September 4, 2011

data integration with columns appended to a DataTable

I now have data integration I eluded too here working. Noteworthy:

  1. Below: Program.GoGetBaz() in an example of an API on a partial extending a domain object that reaches out to a different domain object for information. Baz is not a parameter on Program. It is a parameter based on a different parameter on what we will call Qux, a domain object that has a many-to-one relationship with Program (i.e. a child of Program). Is this a good way to approach this or should this method be in the same View Model as FixupDataTable()? I'm still trying to decide. What do you think? The floor is open!

    public virtual Int32 GoGetBaz()

    {

       var quxes = this.Qux.Where(qux => qux.Whatever == true);

       List<QuxChild> quxKids = new List<QuxChild>();

       foreach (Qux qux in quxes)

       {

          quxKids.AddRange(qux.FishYetOneLevelDeeperForBaz());

       }

       return quxKids.Distinct().Count();

    }

  2. We are this sprint ironing out a good way to get data for views. It has been decided that we will both need an IEnumerable<Program> and a DataTable representing a flattened Program snapshot. The later is applicable for handing to a view listing records while former, with all of its nested concerns, is applicable for handing to an edit screen for CRUD concerns against one particular Program in the IEnumerable<Program>. ProgramsDTO, kept on our view model, keeps both concerns at ProgramsDTO.Entities and ProgramsDTO.DataTable.
  3. The content below in white has changed. The content in black is unchanged from here.

 
 

public DataTable FixupDataTable()

{

   //strip away redundant columns

   DataTable dataTable = this.ProgramsDTO.DataTable;

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

   foreach(DataColumn dataColumn in dataTable.Columns)

      if (dataColumn.Caption != "Foo") columnNames.Add(dataColumn.Caption);

   foreach(string columnName in columnNames)

      dataTable.Columns.Remove(columnName);

   

   //rename the Name column

   dataTable.Columns[0].Caption = "Bar";

   

   //add Open Offerings column and populate it

   dataTable.Columns.Add("Baz", typeof(Int32));

   
Int32 counterOfPrograms = 0;

   foreach (Program program in ProgramsDTO.Entities)

   {

      dataTable.Rows[counterOfPrograms][1] = program.GoGetBaz();

      counterOfPrograms++;

   }


   

   //hand back the fixed up DataTable

   this.ProgramsDTO.DataTable = dataTable;

   return this.ProgramsDTO.DataTable;

}

 
 

The above example passes these tests.

Assert.AreEqual(dataTable.Rows.Count,13);

Assert.AreEqual(dataTable.Columns.Count, 2);

Assert.AreEqual(dataTable.Columns[0].Caption, "Bar");

Assert.AreEqual(dataTable.Columns[1].Caption, "Baz");

Assert.AreEqual(dataTable.Rows[0][1], 3);

Assert.AreEqual(dataTable.Rows[1][1], 1);

Assert.AreEqual(dataTable.Rows[2][1], 1);

Assert.AreEqual(dataTable.Rows[3][1], 0);

 
 

Again, forgive my use of foreach. I'll see if I can refactor these away too.

No comments:

Post a Comment