C# 我应该使用EntityFramework5的通用存储库吗?
我目前正在使用具有通用存储库和工作单元模式的实体框架。我的模型与中描述的模型类似 我过去使用过通用存储库,非常喜欢它提供的全局功能。然而,在实体框架中使用它时,我似乎每天都遇到更多的问题。在处理父/子/连接关系时,这些问题似乎出现得更多 将通用存储库与EF一起使用开始让我感到不舒服,我开始认为将通用存储库与EF一起使用是错误的方法C# 我应该使用EntityFramework5的通用存储库吗?,c#,entity-framework,generics,unit-of-work,C#,Entity Framework,Generics,Unit Of Work,我目前正在使用具有通用存储库和工作单元模式的实体框架。我的模型与中描述的模型类似 我过去使用过通用存储库,非常喜欢它提供的全局功能。然而,在实体框架中使用它时,我似乎每天都遇到更多的问题。在处理父/子/连接关系时,这些问题似乎出现得更多 将通用存储库与EF一起使用开始让我感到不舒服,我开始认为将通用存储库与EF一起使用是错误的方法 有人能帮我指引正确的方向吗?这篇文章的方法确实会让人感到痛苦,因为您已经在EF中拥有了一个通用存储库和一个通用IUnitOfWork,为每种类型创建特定的存储库只会消
有人能帮我指引正确的方向吗?这篇文章的方法确实会让人感到痛苦,因为您已经在EF中拥有了一个通用存储库和一个通用IUnitOfWork,为每种类型创建特定的存储库只会消除通用存储库的好处 我在这里发布了一个我如何拥有一个通用存储库和我的IUnitOfWork的示例,有了它,你可以拥有一个非常好的存储库
public interface IUnitOfWork : IDisposable
{
void Save();
void Save(SaveOptions saveOptions);
}
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
IUnitOfWork Session { get; }
IList<TEntity> GetAll();
IList<TEntity> GetAll(Expression<Func<TEntity, bool>> predicate);
bool Add(TEntity entity);
bool Delete(TEntity entity);
bool Update(TEntity entity);
bool IsValid(TEntity entity);
}
公共接口IUnitOfWork:IDisposable
{
作废保存();
作废保存(保存选项保存选项);
}
公共接口Irespository:IDisposable where tenty:class
{
IUnitOfWork会话{get;}
IList GetAll();
IList GetAll(表达式谓词);
bool-Add(TEntity实体);
bool-Delete(TEntity实体);
bool更新(TEntity实体);
bool是有效的(潜在实体);
}
而实施过程类似于:
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<TEntity> GetAll<TEntity>() where TEntity : class
{
return session.Set<TEntity>().ToList();
}
public IList<TEntity> GetAll<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return session.Set<TEntity>().Where(predicate).ToList();
}
public bool Add<TEntity>(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>(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>(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>(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;
}
}
公共类存储库:组件,IRepository
{
受保护的DbContext会话;
公共虚拟IUnitOfWork会话
{
得到
{
if(会话==null)
抛出新的无效操作异常(“一个会话IUNITOFWORKS do repositório não estáinstanciada.”);
返回(会话作为工作单元);
}
}
公共虚拟数据库上下文
{
得到
{
返回会议;
}
}
公共存储库(IUnitOfWork实例)
{
设置会话(实例);
}
public IList GetAll(),其中tenty:class
{
返回session.Set().ToList();
}
公共IList GetAll(表达式谓词),其中tenty:class
{
return session.Set().Where(谓词).ToList();
}
公共bool Add(tenty实体),其中tenty:class
{
如果(!IsValid(实体))
返回false;
尝试
{
session.Set(typeof(tenty)).Add(entity);
返回session.Entry(entity).GetValidationResult().IsValid;
}
捕获(例外情况除外)
{
if(例如InnerException!=null)
抛出新异常(ex.InnerException.Message,ex);
抛出新异常(例如Message、ex);
}
}
公共bool Delete(tenty实体),其中tenty:class
{
如果(!IsValid(实体))
返回false;
尝试
{
session.Set(typeof(tenty)).Remove(entity);
返回session.Entry(entity).GetValidationResult().IsValid;
}
捕获(例外情况除外)
{
if(例如InnerException!=null)
抛出新异常(ex.InnerException.Message,ex);
抛出新异常(例如Message、ex);
}
}
公共布尔更新(TEntity实体),其中TEntity:类
{
如果(!IsValid(实体))
返回false;
尝试
{
session.Set(字体(tenty)).Attach(实体);
session.Entry(entity.State=EntityState.Modified;
返回session.Entry(entity).GetValidationResult().IsValid;
}
捕获(例外情况除外)
{
if(例如InnerException!=null)
抛出新异常(ex.InnerException.Message,ex);
抛出新异常(例如Message、ex);
}
}
公共虚拟bool有效(tenty值),其中tenty:class
{
如果(值==null)
抛出新的异常(“entidade não pode ser nula.”);
返回true;
}
公共作废设置会话(IUnitOfWork会话)
{
工作组(会议);
}
受保护的内部void SetUnitOfWork(IUnitOfWork会话)
{
if(!(会话是DbContext))
抛出新的ArgumentException(“inst–ncia IUnitOfWork deve um DbContext”);
SetDbContext(会话作为DbContext);
}
受保护的内部void SetDbContext(DbContext会话)
{
if(会话==null)
抛出新ArgumentNullException(“DbContext:instance”);
如果(!(会话是i工作单元))
抛出新的ArgumentException(“inst–ncia DbContext开发实现接口IUnitOfWork”);
this.session=会话;
}
}
这取决于您拥有通用存储库/UoW的目标。如果你认为<代码> dSETP>代码>已经是一个通用的知识库,而DbSet
已经实现了这些。在通用存储库上进行测试更容易,并且如果实体重要的话,将依赖于实体框架。性能和复杂性也是需要考虑的。通过EF访问数据的速度明显慢于ADO.net,EF给应用程序增加了一层额外的复杂性,最终减少了对DAL的控制。@kmdsax我认为问题在于是否将存储库与EF一起使用,而不是是否使用EF。在add中,try catch的作用是什么?还有,为什么不使用session.Set().Add(entity)?问题是关于通用性的,所以请记住