在我的项目中,我创建了静态类DataManager
,它公开了获取数据所需的所有功能,例如
public static IList<ActionHistoryData> GetActionHistoryList(DateTime startDate, DateTime endDate, bool postprocessed)
{
return GlobalComponents.DataManagerImpl.GetActionHistoryList(null, null, null, null, null, null, null, startDate, endDate, false, postprocessed, null);
}
public static ActionHistoryData GetActionHistory(int id)
{
IList<ActionHistoryData> actionHistoryList =
GlobalComponents.DataManagerImpl.GetActionHistoryList(id, null, null, null, null, null, null, null, null, null, null, null);
CQGUtils.Verify(!CollectionsUtil.IsEmpty(actionHistoryList), "There is no action history with [ID='{0}']", id);
CQGUtils.Verify(actionHistoryList.Count == 1, "More than one action history returned.");
return actionHistoryList[0];
}
正如您在 DB 中看到的,我们只有一个带有许多不同参数的存储过程GetActionHistoryList
(用于表数据)。ActionHistory
存储过程包含动态 SQL 例如
`<select statement part>`
DECLARE @where nvarchar(4000);
SET @where = N' WHERE '
IF @ID IS NOT NULL
SET @where = @where + '(ah.ID = @ID) AND '
IF @AccountID IS NOT NULL
SET @where = @where + '(ah.AccountID = @AccountID) AND '
IF @SourceKind IS NOT NULL
SET @where = @where + '(ah.SourceKind = @SourceKind) AND '
IF @SourceIDArray IS NOT NULL
SET @where = @where + '(ah.SourceID IN ('+ @SourceIDArray +')
IF @Postprocessed IS NOT NULL
SET @where = @where + '(ah.Postprocessed = @Postprocessed) AND '
IF @StartDate IS NOT NULL
SET @where = @where + '(ah.UtcTimestamp >= @StartDate) AND '
IF @EndDate IS NOT NULL
SET @where = @where + '(ah.UtcTimestamp <= @EndDate) AND '
) AND '
SET @where = @where + ' 1=1'
SET @query = @query+@where+' order by utcTimestamp desc '
EXEC sp_executesql @query,
N'
@ID int,
@AccountID int,
@SourceKind tinyint,
@SourceIDArray nvarchar(max),
@NotificationID int,
@DataRequestID int,
@NotificationName nvarchar(250),
@StartDate datetime,
@EndDate datetime,
@MostRecent bit,
@Postprocessed bit,
@TopLimit int
',
@ID = @ID,
@AccountID = @AccountID,
@SourceKind = @SourceKind,
@SourceIDArray = @SourceIDArray,
@NotificationID = @NotificationID,
@DataRequestID = @DataRequestID,
@NotificationName = @NotificationName,
@StartDate = @StartDate,
@EndDate = @EndDate,
@MostRecent = @MostRecent,
@Postprocessed = @Postprocessed,
@TopLimit = @TopLimit
这种方法允许轻松添加新的过滤请求