私のプロジェクトでは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
(テーブル データ用の) ストアド プロシージャは 1 つだけです。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
このようなアプローチにより、新しいフィルタリング要求を簡単に追加できます