여러 엔티티 집합을 사용하여 작업 할 때 엔티티 프레임 워크와 함께 저장소 패턴을 사용하는 올바른 방법은 무엇입니까?여러 저장소를 사용하는 올바른 방법은 무엇입니까?
모든 항목에 대한 저장소를 만들어야합니까?
예 :
다음 항목이있는 경우 : 기사, 카테고리 및 의견.
각각 하나의 저장소가 있어야합니까?
나는이 같은 저장소를 사용했다 :
public class BaseArticleRepository : BaseRepository
{
private ContentModel _contentctx;
public ContentModel Contentctx
{
get
{
if ((_contentctx == null))
{
_contentctx = new ContentModel();
}
return _contentctx;
}
set { _contentctx = value; }
}
// IDisposable Support code comes here....
}
및 샘플 저장소를 기사 :
public class ArticlesRepository : BaseArticleRepository
{
public Article GetArticleById(int id)
{
var article = Contentctx.Articles.Where(o => o.ArticleID == id).FirstOrDefault();
return article;
}
public List<Article> GetArticles()
{
var articles = Contentctx.Articles.ToList();
return articles;
}
public List<ArticleHeader> GetArticlesHeaders()
{
var articles = (from article in Contentctx.Articles
select new ArticleHeader
{
ArticleID = article.ArticleID,
Title = article.Title,
CategoryTitle = article.Articles_Categories.Title,
AddedBy = article.AddedBy,
AddedDate = article.AddedDate,
ViewCount = article.ViewCount
}).ToList();
return articles;
}
public List<ArticleHeader> GetArticlesHeaders(int PageIndex, int PageSize)
{
var articles = (from article in Contentctx.Articles
select new ArticleHeader
{
ArticleID = article.ArticleID,
Title = article.Title,
CategoryTitle = article.Articles_Categories.Title,
AddedBy = article.AddedBy,
AddedDate = article.AddedDate,
ViewCount = article.ViewCount
}).OrderBy(p => p.AddedDate).Skip(PageSize * PageIndex).Take(PageSize).ToList();
return articles;
}
public int GetArticleCount(string txtFilter)
{
int ret = Contentctx.Articles.Where(o => o.Title.Contains(txtFilter)).Count();
return ret;
}
public int AddArticle(Article article, int categoryId)
{
Contentctx.AddToArticles(article);
}
}
은 기본적으로 모든 저장소 I 불구하고, (필터 및 종류와 데이터를 가져 오는 포함) 모든 CRUD 데이터를 구현
저장소가 데이터를 검색하고 삽입 (삭제 수정)하는 기본, 일반 기능 만 구현해야하기 때문에 이것이 잘못된 저장소 패턴 구현이라고 일부 블로그에서 읽습니다.모든 정렬은 필터링을 메모리에서 로컬로 수행해야합니다.
하지만 서버 측 (sqlserver)에서 수행 할 수있는 작업을 미리 수행합니다.
제목과 초록 만 필요하면 왜 데이터베이스에서 모든 기사를로드해야합니까?
"모든 정렬은 필터링을 메모리에서 로컬로 수행해야합니다." - 어디서 읽었 니? 어떤 백엔드를 사용하고 있는지 확실하지 않지만 SQL 쿼리는 메모리에서 수행하는 것보다 훨씬 효율적으로 데이터를 정렬/필터링 할 수 있습니다 (각각 동일한 양의 프로그래밍을 프로그래밍 할 때). 수백만 행의 데이터를 필터링 할 수 있다면 어떨까요? –