我应该使用带有Entity Framework 5的通用存储库吗?

我目前正在使用具有通用存储库和工作单元模式的entity framework。 我的模型类似于本文中描述的模型

我过去曾使用Generic Repositories,并且非常享受它可以提供的全局function。 但是,在将它与Entity Framework一起使用时,我似乎每天都遇到更多问题。 在处理父/子/交汇关系时,这些问题似乎更多。

使用带有EF的通用存储库开始在我的口中留下不好的味道,我开始认为使用带有EF的通用存储库是错误的方法。

有人可以帮助引导我朝正确的方向发展吗?

本文的方法实际上可能会变得很痛苦,因为您已经在EF中拥有通用存储库和通用IUnitOfWork,并且为每种类型创建特定的存储库只会消除通用的好处!

我在这里发布了一个关于我如何拥有通用存储库和我的IUnitOfWork的示例,通过这个,您可以拥有一个非常好的存储库!

 public interface IUnitOfWork : IDisposable { void Save(); void Save(SaveOptions saveOptions); } public interface IRepository : IDisposable where TEntity : class { IUnitOfWork Session { get; } IList GetAll(); IList GetAll(Expression> predicate); bool Add(TEntity entity); bool Delete(TEntity entity); bool Update(TEntity entity); bool IsValid(TEntity entity); } 

而实现类似于:

 public class Repository : Component, IRepository { protected DbContext session; public virtual IUnitOfWork Session { get { if (session == null) throw new InvalidOperationException("A session IUnitOfWork do repositório não está instanciada."); return (session as IUnitOfWork); } } public virtual DbContext Context { get { return session; } } public Repository(IUnitOfWork instance) { SetSession(instance); } public IList GetAll() where TEntity : class { return session.Set().ToList(); } public IList GetAll(Expression> predicate) where TEntity : class { return session.Set().Where(predicate).ToList(); } public bool Add(TEntity entity) where TEntity : class { if (!IsValid(entity)) return false; try { session.Set(typeof(TEntity)).Add(entity); return session.Entry(entity).GetValidationResult().IsValid; } catch (Exception ex) { if (ex.InnerException != null) throw new Exception(ex.InnerException.Message, ex); throw new Exception(ex.Message, ex); } } public bool Delete(TEntity entity) where TEntity : class { if (!IsValid(entity)) return false; try { session.Set(typeof(TEntity)).Remove(entity); return session.Entry(entity).GetValidationResult().IsValid; } catch (Exception ex) { if (ex.InnerException != null) throw new Exception(ex.InnerException.Message, ex); throw new Exception(ex.Message, ex); } } public bool Update(TEntity entity) where TEntity : class { if (!IsValid(entity)) return false; try { session.Set(typeof(TEntity)).Attach(entity); session.Entry(entity).State = EntityState.Modified; return session.Entry(entity).GetValidationResult().IsValid; } catch (Exception ex) { if (ex.InnerException != null) throw new Exception(ex.InnerException.Message, ex); throw new Exception(ex.Message, ex); } } public virtual bool IsValid(TEntity value) where TEntity : class { if (value == null) throw new ArgumentNullException("A entidade não pode ser nula."); return true; } public void SetSession(IUnitOfWork session) { SetUnitOfWork(session); } protected internal void SetUnitOfWork(IUnitOfWork session) { if (!(session is DbContext)) throw new ArgumentException("A instância IUnitOfWork deve um DbContext."); SetDbContext(session as DbContext); } protected internal void SetDbContext(DbContext session) { if (session == null) throw new ArgumentNullException("DbContext: instance"); if (!(session is IUnitOfWork)) throw new ArgumentException("A instância DbContext deve implementar a interface IUnitOfWork."); this.session = session; } }