Database Paging and Sorting with CSLA Part 2 - The CSLA Class

Once your stored procedure is set up (see part 1) you can turn your attention to your CSLA class.   The class I am focusing on here is a read only list called "InfoList" that has a child class "InfoListItem".  This is a design taken directly from Rocky's book and sample application.  The class is declared as follows:

 public class InfoList : ReadOnlyListBase<InfoList, InfoListItem>, Csla.Core.IReportTotalRowCount

 In order to set up the grid view paging I am implementing the IReportTotalRowCount which is needed to get the total records satisfying your sp query.  The following is the implementation for the interface:

#region IReportTotalRowCount Members private int _totalRowCount;

/// <summary>

/// TotalRowCount - this property returns the total row count for the initial list that passed to the

/// constructor.

/// </summary>

int Csla.Core.IReportTotalRowCount.TotalRowCount

{

get { return _totalRowCount; }

}

//Property that can be used in UI to display total number of records
public int TotalRecordCount
{
get { return _totalRowCount; }
}

 #endregion

 Next is the code for the data access.  I have created a FilteredCriteria class that will handle the fields necessary to filter the resuts for paging:

[Serializable]

private class FilteredCriteria

{

internal const int MAXROWS = int.MaxValue;

internal const string DEFAULTSORT = "Subject";

private int _pageIndex;

private int _pageSize;

private string _sort;

private string _searchString;

public string searchString

{

get { return _searchString; }

}

public string Sort

{

get { return _sort; }

}

public int PageIndex

{

get { return _pageIndex; }

}

public int PageSize

{

get { return _pageSize; }

}

 

public FilteredCriteria(int index, int pageSize, string sort, string searchString)

{

//this._pageIndex = GetPageIndex(index,numberOfRecords);

this._pageIndex = index;

this._pageSize = pageSize;

if (String.IsNullOrEmpty(sort))

{

this._sort = DEFAULTSORT;

}

else

{

this._sort = sort;

}

this._searchString = searchString;

}

}

Finally, the Fetch method is coded to pass in the values from the criteria class. This is just a standard sp call, the only additional code is that to get the total row count from the stored procedure and set the _totalRowCount field:

/Get total number of records for GridView paging

dr.NextResult();

dr.Read();

_totalRowCount = dr.GetInt32("TotalRowCount");

That completes the code required for the business class.  The final part is setting up the UI to leverage the pagin logic.  I will demonstrate this in Part 3.

Silverlight Alpha C# Silverlight project issues VS 2008

Using the VS2008 Beta 2 VPC, I have encountoured an issue that results in your HTML files not loading your silverlight controls.  I am not sure what the root cause is, but my work around was to delete the html, js, and xaml files that are created when you create a new project and then add new ones.  Once I did this I was able to see the silverlight controls I had created in my xaml files.  Keep in mind that you will need to add all of the "wiring" code to your new files, which includes your reference to the js files and the div's to load your controls, etc.