Asp.net mvc 将参数传递到存储库,同时保持关注点的分离

Asp.net mvc 将参数传递到存储库,同时保持关注点的分离,asp.net-mvc,interface,repository-pattern,separation-of-concerns,Asp.net Mvc,Interface,Repository Pattern,Separation Of Concerns,我是mvc新手,这种编程方式对我来说很陌生,所以请温柔一点 我的文章存储库中有: public IQueryable<Article> GetArticles(int? category, int? position) { return from a in dc.Articles where a.LanguageIndex == lang && a.CategoryIndex == category && a.Articl

我是mvc新手,这种编程方式对我来说很陌生,所以请温柔一点

我的文章存储库中有:

public IQueryable<Article> GetArticles(int? category, int? position)
{
    return from a in dc.Articles
           where a.LanguageIndex == lang && a.CategoryIndex == category && a.ArticlePosition == position
           select a;
}
public IQueryable GetArticles(内部类别,内部位置)
{
从华盛顿的一家报纸返回
其中a.LanguageIndex==lang&&a.CategoryIndex==category&&a.ArticlePosition==position
选择一个;
}
在保持关注点分离的同时,如何从接口传递参数类别和位置

我想:

public interface IArticleRepository
{
    IQueryable<Article> GetArticles(Article a);
}
公共接口IArticleRepository
{
IQueryable GetArticles(第a条);
}
并将参数与Article对象一起传递,但这意味着我必须传递控制器中的类别和位置。
我的方向正确吗?

不确定这与关注点分离有何关系。我能看出抽象在哪里似乎有漏洞;您是否担心用户似乎对存储库如何保存您的文章知道得太多了

除非有人提出了一种将实现与模型分离的高性能方法,否则存储抽象总是会泄漏的。你可以为此痛打自己,也可以尽力而为

你的第二种方法比第一种更糟糕。您仍然需要在文章中规定类别和位置,因此除了一个将参数与实体混淆的奇怪API之外,您仍然存在漏洞

我肯定会选择第一个版本而不是第二个版本。如果我要做任何事情,我会重构以生成CategoryIndex和ArticlePosition实体(链接到Article表的Category和Position表)。然后,您可以将API重构为更具吸引力的:

var cat = CategoryRepository.GetCategory("foo");
var pos = PositionRepository.GetPosition("bar");
var article = ArticleRepository.GetArticle(cat, pos);

这比你现有的好吗?可能不会

首先,我将分离出基本查询:

public IQueryable<Article> GetArticles()
{
    return from a in dc.Articles select a;
}

public IQueryable<Article> GetArticles(int? category, int? position)
{
    return GetArticles ().Where (a => a.LanguageIndex == category && a.CategoryIndex == position).AsQueryable ();
}
public IQueryable GetArticles()
{
从dc中的a返回。选择a;
}
公共IQueryable GetArticles(内部类别、内部位置)
{
返回GetArticles().Where(a=>a.LanguageIndex==category&&a.CategoryIndex==position).AsQueryable();
}
现在,如果要将特定查询筛选器移出存储库,可以将其移动到扩展方法:

public static IQueryable<Article> WithCategory(this IQueryable<Article> articles, int? category, int? position)
{
    return articles.Where (a => a.LanguageIndex == category && a.CategoryIndex == position).AsQueryable ();
}
public static IQueryable with category(此IQueryable文章,int?category,int?position)
{
return articles.Where(a=>a.LanguageIndex==category&&a.CategoryIndex==position).AsQueryable();
}