我已经开始使用实体框架(6.x)探索存储库模式,阅读本文中我认为对该主题的最佳基本处理方式:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm
不幸的是它没有解决连接问题。使用通用存储库,如何连接到任意数量的其他实体/存储库?
截至目前,我有这个通用接口:
interface IGenericRepository<T> where T: class
{
IEnumerable<T> SelectAll();
T SelectByID(object id);
void Insert(T obj);
void Update(T obj);
void Delete(object id);
void Save();
}
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private SocialMediaEntities db = null;
private DbSet<T> table = null;
public GenericRepository()
{
this.db = new SocialMediaEntities();
table = db.Set<T>();
}
public GenericRepository(SocialMediaEntities db)
{
this.db = db;
table = db.Set<T>();
}
public IEnumerable<T> SelectAll()
{
return table.ToList();
}
public T SelectByID(object id)
{
return table.Find(id);
}
public void Insert(T obj)
{
table.Add(obj);
}
public void Update(T obj)
{
table.Attach(obj);
db.Entry(obj).State = EntityState.Modified;
}
public void Delete(object id)
{
T existing = table.Find(id);
table.Remove(existing);
}
public void Save()
{
db.SaveChanges();
}
}
}
也许客户,客户订单示例是一个很好的选择来说明。感谢。
在问我的问题后,我发现了另一个问题,答案导致了问题 以下解决方案适合我。问题是我混淆了成语吗?这是 方法保持在通用存储库模式中?
(问题是Entityframework Join using join method and lambdas)
using (CommerceEntities ce = new CommerceEntities())
{
// the repository is Company
var results = repository.SelectAll()
.Join(ce.Orders, // target
p => p.OrderID, // FK
s => s.ID, // PK
(p, s) => new { Order = s, Company = p }) // project result
.Select(x => x.Company).ToList(); // select result
}
在研究了给出的第一个答案之后,它似乎更符合通用存储库模式的精神。
答案 0 :(得分:0)
您可以为GetAll方法创建多个重载。像这样:
//get all, including orderby clause, and includes
//usage: var s = repository.GetAll(i => i.Name, false, i => i.NavigationProperty);
public IEnumerable<T> GetAll<TOrderKey>(Expression<Func<T, TOrderKey>> orderbyExp,
Boolean descending,
params Expression<Func<T, Object>>[] includeExps)
{
var query = table.AsQueryable();
query = !descending ? query.OrderBy(orderByExp) : query.OrderByDescending(orderByExp);
if (includeExps != null)
query = includeExps.Aggregate(query, (current, exp) => current.Include(exp));
return query.ToList();
}
//get all, including select clause, orderby clause, and includes
//usage: var s = repository.GetAll(i => new { Name = i.Name }, i => i.Name, false, i => i.NavigationProperty
public IEnumerable<TReturn> GetAll<TReturn, TOrderKey>(Expression<Func<T, TReturn>> selectExp,
Expression<Func<T, TOrderKey>> orderExp,
Boolean descending,
params Expression<Func<T, Object>>[] includeExps)
{
var query = table.AsQueryable();
query = !descending ? query.OrderBy(orderByExp) : query.OrderByDescending(orderByExp);
if (includeExps != null)
query = includeExps.Aggregate(query, (current, exp) => current.Include(exp));
return query.Select(selectExp).ToList();
}
//get all, including select clause, where clause, order by clause, and includes
//usage: var s = repository.GetAll(i => new { i.Name }, i => i.Name.Contains('John'), i => i.Name, false, i => i.NavigationProperty
public IEnumerable<TReturn> GetAll<TReturn, TOrderKey>(Expression<Func<T, TReturn>> selectExp,
Expression<Func<T, Boolean>> whereExp,
Expression<Func<T, TOrderKey>> orderbyExp,
Boolean descending,
params Expression<Func<T, object>>[] includeExps)
{
var query = table.Where(whereExp);
query = !descending ? query.OrderBy(orderbyExp) : query.OrderByDescending(orderbyExp);
if (includeExps != null)
query = includeExps.Aggregate(query, (current, exp) => current.Include(exp));
return query.Select(selectExp).ToList();
}
上述方法只是一个例子。您应该根据需要重新创建它们。