确定。所以之前已经提出这个问题,但我无法从这些问题中得到一些提示(可能是因为我对这个通用概念不熟悉)。
所以我的代码是这样的: -
if (dbundle.BoardFK != null && dbundle.ClassesFK != null && dbundle.ClassSubjectFK != null && !string.IsNullOrEmpty(Request.Form["MaterialTypeFK"].ToString()))
{
List<ClassMaterial> ClassMaterialList = null;
ClassMaterialList = PagingList(dbundle);
}
else if (dbundle.DegreeFK != null && dbundle.DegreeStudyFieldFK != null && dbundle.DegreeCourseFK != null && dbundle.DegreeUnivFK != null && dbundle.YearFK != null && dbundle.SemesterFK != null && dbundle.DegreeSubjectFK != null && !string.IsNullOrEmpty(Request.Form["MaterialTypeFK"].ToString()))
{
dbundle.MaterialIndicator = Convert.ToInt32(Request.Form["MaterialTypeFK"]);
List<DegreeMaterial> DegreeMaterialList = null;
DegreeMaterialList = PagingList(dbundle);
} // This continues like this
我的PagingList方法返回一个分页列表: -
public AllTblListClass PagingList(DataBundle aListBundle)
{
AllTblListClass TblList = new AllTblListClass();
using (SqlConnection conn = new SqlConnection(Constants.Connection))
{
if (!string.IsNullOrEmpty(aListBundle.ListType))
{
conn.Open();
SqlCommand cmd = new SqlCommand("FetchPagedList", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ListType", SqlDbType.VarChar, 50).Value = aListBundle.ListType;
if (aListBundle.ListType.Equals(Constants.ClassSubMatRelation))
{
cmd.Parameters.Add("@BoardFK", SqlDbType.Int).Value = aListBundle.BoardFK;
cmd.Parameters.Add("@ClassFK", SqlDbType.Int).Value = aListBundle.ClassesFK;
cmd.Parameters.Add("@ClassSubjectFK", SqlDbType.Int).Value = aListBundle.ClassSubjectFK;
}
else if (aListBundle.ListType.Equals(Constants.DegreeSubMatRel))
{
cmd.Parameters.Add("@DegreeFK", SqlDbType.Int).Value = aListBundle.DegreeFK;
cmd.Parameters.Add("@DegreeStudyFieldFK", SqlDbType.Int).Value = aListBundle.DegreeStudyFieldFK;
cmd.Parameters.Add("@DegreeCourseFK", SqlDbType.Int).Value = aListBundle.DegreeCourseFK;
cmd.Parameters.Add("@DegreeUnivFK", SqlDbType.Int).Value = aListBundle.DegreeUnivFK;
cmd.Parameters.Add("@YearFK", SqlDbType.Int).Value = aListBundle.YearFK;
cmd.Parameters.Add("@SemesterFK", SqlDbType.Int).Value = aListBundle.SemesterFK;
cmd.Parameters.Add("@DegreeSubjectFK", SqlDbType.Int).Value = aListBundle.DegreeSubjectFK;
}
else if (aListBundle.ListType.Equals(Constants.DiplomaSubMatRel))
{
cmd.Parameters.Add("@DiplomaFK", SqlDbType.Int).Value = aListBundle.DiplomaFK;
cmd.Parameters.Add("@DiplomaStudyFieldFK", SqlDbType.Int).Value = aListBundle.DiplomaStudyFieldFK;
cmd.Parameters.Add("@DiplomaCourseFK", SqlDbType.Int).Value = aListBundle.DiplomaCourseFK;
cmd.Parameters.Add("@DiplomaUnivFK", SqlDbType.Int).Value = aListBundle.DiplomaUnivFK;
cmd.Parameters.Add("@YearFK", SqlDbType.Int).Value = aListBundle.YearFK;
cmd.Parameters.Add("@SemesterFK", SqlDbType.Int).Value = aListBundle.SemesterFK;
cmd.Parameters.Add("@DiplomaSubjectFK", SqlDbType.Int).Value = aListBundle.DiplomaSubjectFK;
}
else if (aListBundle.ListType.Equals(Constants.CertificateRel))
{
cmd.Parameters.Add("@CertificateFK", SqlDbType.Int).Value = aListBundle.CertificateFK;
cmd.Parameters.Add("@CertificateTypeFK", SqlDbType.Int).Value = aListBundle.CertificateTypeFK;
cmd.Parameters.Add("@CertSubjectFK", SqlDbType.Int).Value = aListBundle.CertificateSubjectFK;
}
cmd.Parameters.Add("@MaterialTypeFK", SqlDbType.Int).Value = aListBundle.MaterialIndicator;
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
if (reader.HasRows)
while (reader.Read())
{
//Want to implement reader here
}
reader.Close();
cmd.Dispose();
conn.Close();
}
}
if (aListBundle.ListType.Equals(Constants.ClassSubMatRelation))
{
List<ClassMaterial> cm = new List<Models.ClassMaterial>();
TblList.MatrlObj = cm;
return TblList.MatrlObj; // want to return list from here
}
else
return null;
}
我的AllTblListClass类已经添加了所有列表。
public class AllTblListClass
{
public List<ClassMaterial> MatrlObj { get; set; }
public List<CourseTbl> Courses { get; set; }
public List<CourseDesc> CourseDesc { get; set; }
public List<CourseSubDesc> CourseSubDesc { get; set; }
public List<Contact> Contacts { get; set; }
}
我的SP FetchPagedList如下: -
Alter Proc FetchPagedList
@PageStart int,
@PageEnd int,
@TblName varchar(200),
@BoardFK int=null,
@ClassFK int=null,
@ClassSubjectFK int=null,
@MaterialTypeFK int=null,
@ClassMaterialFK int=null,
@DegreeFK int=null,
@DegreeStudyFieldFK int=null,
@DegreeCourseFK int=null,
@DegreeUnivFK int=null,
@DegreeSubjectFK int=null,
@DiplomaFK int=null,
@DiplomaStudyFieldFK int=null,
@DiplomaCourseFK int=null,
@DiplomaUnivFK int=null,
@DiplomaSubjectFK int=null,
@YearFK int=null,
@SemesterFK int=null,
@CertificateFK int=null,
@CertificateTypeFK int=null,
@CertSubjectFK int=null
--@YearFK int=null,
作为 开始
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
if(@TblName='ClassSubMatRelation')
BEGIN
WITH PagingItem AS
(
SELECT cm.ClassMaterialID, cm.Name, cm.Description, cm.EbookLink,
ROW_NUMBER() OVER (ORDER BY ClassSubMatRelationID) AS 'RowNumber'
FROM ClassSubMatRelation csmr join ClassMaterial cm on cm.ClassMaterialID=csmr.ClassMaterialFK
where BoardFK = @BoardFK and ClassFK = @ClassFK and ClassSubjectFK=@ClassSubjectFK and MaterialTypeFK=@MaterialTypeFK
)
SELECT * FROM PagingItem WHERE RowNumber BETWEEN @PageStart AND @PageEnd;
END
else if(@TblName='DegreeSubMatRel')
BEGIN
WITH PagingItem AS
(
SELECT dm.DegreeMaterialID,
ROW_NUMBER() OVER (ORDER BY DegreeSubMatRelID) AS 'RowNumber'
FROM DegreeSubMatRel dsmr join DegreeMaterial dm on dsmr.DegreeMaterialFK = dm.DegreeMaterialID
where DegreeFK = @DegreeFK and DegreeStudyFieldFK = @DegreeStudyFieldFK and DegreeCourseFK=@DegreeCourseFK and DegreeUnivFK=@DegreeUnivFK and DegreeSubjectFK=@DegreeSubjectFK and YearFK=@YearFK and SemesterFK=@SemesterFK and MaterialTypeFK=@MaterialTypeFK
)
SELECT * FROM PagingItem WHERE RowNumber BETWEEN @PageStart AND @PageEnd;
END
else if(@TblName='DiplomaSubMatRel')
BEGIN
WITH PagingItem AS
(
SELECT dm.DiplomaMaterialID, dm.Name, dm.Description,
ROW_NUMBER() OVER (ORDER BY DiplomaSubMatRelID) AS 'RowNumber'
FROM DiplomaSubMatRel dsmr join DiplomaMaterial dm on dsmr.DiplomaMaterialFK = dm.DiplomaMaterialID
where DiplomaFK = @DiplomaFK and DiplomaStudyFieldFK = @DiplomaStudyFieldFK and DiplomaCourseFK=@DiplomaCourseFK and DiplomaUnivFK=@DiplomaUnivFK and DiplomaSubjectFK=@DiplomaSubjectFK and YearFK=@YearFK and SemesterFK=@SemesterFK and MaterialTypeFK=@MaterialTypeFK
)
SELECT * FROM PagingItem WHERE RowNumber BETWEEN @PageStart AND @PageEnd;
END
else if(@TblName='CertificateRelRel')
BEGIN
WITH PagingItem AS
(
SELECT cm.CertificateMaterialID, cm.Name, cm.Description,
ROW_NUMBER() OVER (ORDER BY CertificateMaterialID) AS 'RowNumber'
FROM CertificateRel cr join CertificateMaterial cm on cr.CertificateMaterialFK = cm.CertificateMaterialID
where CertificateFK = @CertificateFK and CertificateTypeFK = @CertificateTypeFK and CertSubjectFK=@CertSubjectFK and MaterialTypeFK=@MaterialTypeFK
)
SELECT * FROM PagingItem WHERE RowNumber BETWEEN @PageStart AND @PageEnd;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF (XACT_STATE()) = -1
BEGIN
ROLLBACK TRANSACTION;
END;
IF (XACT_STATE()) = 1
BEGIN
COMMIT TRANSACTION;
END;
END CATCH
结束
我想根据条件值返回列表。但我无法弄清楚如何做到这一点。任何帮助表示赞赏。
答案 0 :(得分:1)
关于第二个想法:
而不是返回单个列表 - 为什么不返回容器:
Instead of --> return TblList.MatrlObj
Do --> return TblList;
在使用PagingList方法时 - 使用相关的内部对象:
ClassMaterialList = PagingList(dbundle).MatrlObj;
你的守则会变得凌乱而且容易出错(下面是我的初步想法)。
由于AllTblListClass和DataBundle是广泛的组合,他们尝试用DB做所有可能的事情,因此编写更清晰的代码有点困难。这是我的尝试:
private void FillClassMaterialsTable(DataBundle dataBundle, ref AllTblListClass dataStore)
{
// Ado.Net code to call Stored Proc & fetch results
dataStore.MatrlObj = #List of Data of Relevant Type#
}
private void FillCoursesTable(DataBundle dataBundle, ref AllTblListClass dataStore)
{
// Ado.Net code to call Stored Proc & fetch results
dataStore.Courses = #List of Data of Relevant Type#
}
... // So on create simpler 15/20 methods for each Table/Stored Proc call
public AllTblListClass GetDataStore(DataBundle aListBundle)
{
AllTblListClass result = new AllTblListClass();
switch(aListBundle.ListType)
{
case Constants.ClassSubMatRelation:
{
FillClassMaterialsTable(aListBundle, ref result);
}
break;
case Constants.ClassCourses:
{
FillCoursesTable(aListBundle, ref result);
}
break;
}
return result;
}
使用个别较小的方法 - 帮助本地化代码更改,不会破坏其他部分。这也有助于测试各个功能。 GetDataStore方法现在更清晰,因为任何新手开发人员都可以遵循代码(简化的switch case)。
理想情况下 - 我会建议所有类型(ClassMaterial,CourseTbl,CourseDesc等)它们都是从常见的东西继承/实现的。这样您就可以使用更简单的返回类型。此外,由于每个表在where子句中都有自己的列 - 重构代码需要花费更多的精力。但我想将ADO.NET代码概括为一个Command Wrapper类 - 您可以在其中传递Table name&amp; params并从中接收DataTable / DataSet。