最近、リポジトリパターンをサービスに実装し始めましたが、提案が必要な小さな問題に遭遇しました。
私のサービスでは、次のコードを使用してください。
[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を呼び出すことはできません。