1
我最近開始在我的服務中實現Repository模式,並且遇到了一些我想要的建議。使用存儲庫模式 - 哪裏可以使用LINQ過濾?
看看下面的代碼,在我的服務:
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
public Policy GetPolicyById(int policyId, int historyId)
{
// Don't do the if not deleted check here, as we can do it on our Controller.
// This means we can use this function if we have to access the deleted records.
return _policy.GetByID(policyId, historyId);
}
這個功能顯然是直接從服務接口引用,並.GetByID方法是我的通用存儲庫的一部分。
主要問題是:如果我想對此結果集執行.Where(a => !a.Deleted)
怎麼辦。你可以通過我的代碼評論看到我的思想總體思路是什麼,但是這是做到這一點的「正確」方式,應該在服務級別,控制器(UI)級別,還是應該在內部創建一個函數我的非通用庫允許這個功能?
**更新**
這是我的代碼另一部分,在我的策略庫:
/// <summary>
///
/// </summary>
/// <param name="policyId"></param>
/// <param name="historyid"></param>
/// <returns></returns>
public virtual IEnumerable<Policy> GetPolicies(int take, int skip)
{
var policies = GetPoliciesHistoryGrouped().Take(take).Skip(skip);
return policies;
}
/// <summary>
/// Returns all non-deleted (bitDelete) policies (in an expression, not comitted via the database),
/// grouped by the latest history ID.
/// </summary>
/// <returns></returns>
private IEnumerable<Policy> GetPoliciesHistoryGrouped()
{
// Group and order by history ID.
var historyIDs = _context.Policies.Where(a => !a.bitDelete)
.GroupBy(a => a.intPolicyId).Select(group => new
{
PolicyID = group.Key,
HistoryID = group.Max(a => a.intHistoryID)
});
var query = (from h in historyIDs
join p in _context.Policies on new { h.HistoryID, h.PolicyID } equals new { HistoryID = p.intHistoryID, PolicyID = p.intPolicyId }
select p).OrderBy(a => a.intPolicyId);
return query;
}
如果這種級別的深度不應該在庫層,而不是處於服務層,其中GetPoliciesHistoryGrouped
函數仍然必須創建和共享?請記住,.Take和.Skip只有在分組完成後才能被調用。
更新了我的問題 - 我很可能將此標記爲已回答並感謝它,但您是否對問題的新部分有任何意見? –