NHibernate to handle search and paging from stored procedure

Basically my task is to create a page that display data in grid, able to filter (search), and print the data I retrieve from a stored procedure using NHibernate.

cac

I will pass a filter with few parameter into the SP to retrieve the data to be display.

In xml file, I have added ‘sql-query’ query tag to query out the data. ‘query-param’ is the parameter to pass into the SP.

————————————————————————————————————

in .hbm.xml file:

<?xml version=”1.0″ encoding=”utf-8″?>
<hibernate-mapping namespace=”Domain.Entity” assembly=”Domain” xmlns=”urn:nhibernate-mapping-2.2″>
<class name=”Domain.Entity.ClientAccountReport, Domain” table=”ClientAccountReport_UI” schema=”dbo”>
<id name=”Id” column=”[Id]”>
<generator class=”native”/>
</id>
<property name=”SalesPerson”/>
<property name=”SalesPersonName”/>
<property name=”SalesPersonOpenDate”/>
<property name=”ClientAccountType”/>
<property name=”ClientCode” />
<property name=”ClientName”/>
<property name=”AppointCode”/>
<property name=”AppointName”/>
<property name=”AppointPersonCreatedDate”/>
<property name=”AppointPersonAmendedDate”/>
<property name=”AuditDate”/>
<property name=”AuditRecordID”/>
<property name=”FieldName”/>
<property name=”ActionType”/>
<property name=”OldValue”/>
<property name=”NewValue”/>
<property name=”Remarks”/>
<property name=”UserID”/>
<property name=”OpenDate”/>
<property name=”CloseDate”/>
<property name=”LastActiveDate”/>
<property name=”ClientDeleteDate”/>
</class>

<sql-query name=”ClientAccountReport_UI” callable=”true”>
<query-param name=”ClientCode” type=”String”/>
<query-param name=”ClientRefTypeCode” type=”String”/>
<query-param name=”ActionType” type=”String”/>
<query-param name=”AuditDate” type=”DateTime”/>
<return class=”ClientAccountReport”>
<return-property column=”ID” name=”Id” />
<return-property column=”SalesPerson” name=”SalesPerson” />
<return-property column=”SalesPersonName” name=”SalesPersonName” />
<return-property column=”SalesPersonOpenDate” name=”SalesPersonOpenDate” />
<return-property column=”ClientAccountType” name=”ClientAccountType” />
<return-property column=”ClientCode” name=”ClientCode” />
<return-property column=”ClientName” name=”ClientName” />
<return-property column=”AppointCode” name=”AppointCode” />
<return-property column=”AppointName” name=”AppointName” />
<return-property column=”AppointPersonCreatedDate” name=”AppointPersonCreatedDate” />
<return-property column=”AppointPersonAmendedDate” name=”AppointPersonAmendedDate” />
<return-property column=”AuditDate” name=”AuditDate” />
<return-property column=”AuditRecordID” name=”AuditRecordID” />
<return-property column=”FieldName” name=”FieldName” />
<return-property column=”ActionType” name=”ActionType” />
<return-property column=”OldValue” name=”OldValue” />
<return-property column=”NewValue” name=”NewValue” />
<return-property column=”Remarks” name=”Remarks” />
<return-property column=”UserID” name=”UserID” />
<return-property column=”OpenDate” name=”OpenDate” />
<return-property column=”CloseDate” name=”CloseDate” />
<return-property column=”LastActiveDate” name=”LastActiveDate” />
<return-property column=”ClientDeleteDate” name=”ClientDeleteDate” />
</return>
exec [ClientAccountReport_UI] @ClientCode=:ClientCode, @ClientRefTypeCode=:ClientRefTypeCode, @ActionType=:ActionType, @AuditDate=:AuditDate
</sql-query>
</hibernate-mapping>

————————————————————————————————————

In Dao.cs  class file

public void GetClientAccountReportByPage(IPage<ClientAccountReport> page, IList<IFilter> filters)
{
HibernateDaoHelp.SearchByPageForSP(page, “ClientAccountReport_UI”, filters);
}

————————————————————————————————————

In HibernateDaoHelp.cs class file

public void SearchByPageForSP(IPage<T> page, string hql, IList<FXBOSS.Utils.IFilter> Filters)
{
IQuery iQuery = this.GetSession().GetNamedQuery(hql);

if (Filters.Count > 0)
{
foreach (var obj in Filters)
{
if (obj.data.ToString().Trim().Length == 0)
{
iQuery.SetParameter(obj.field, String.Empty);
}
else
{
iQuery.SetParameter(obj.field, obj.data);
}
}
}

IList<T> ListOfObj = iQuery.List<T>();

if (page.pageSize != -1)
{
var list = ListOfObj.Skip((page.pageNo – 1) * page.pageSize).Take(page.pageSize).ToList();
page.totalCount = ListOfObj.Count();
page.result = list;
}
else
{
var list = ListOfObj.ToList();
page.totalCount = ListOfObj.Count();
page.result = list;
}
}

————————————————————————————————————

In controller.cs file

private Page<ClientAccountReport> filterPage(Page<ClientAccountReport> Page, string filters)
{
SystemSeg.GetClientAccountReportByPage(Page, FilterUtil.GetQuery<ClientAccountReport>(filters));
return Page;
}

————————————————————————————————————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s