Rob*_*vić 32 c# linq generics repository
我已经看到了两种不同的方法来创建通用存储库.这两种方法之间有什么区别(利弊)?请对方法有所区别,因为我对它们之间的区别感兴趣
public interface IRepository<T> where T : class
Run Code Online (Sandbox Code Playgroud)
和
public interface IRepository : IDisposable
Run Code Online (Sandbox Code Playgroud)
功能,灵活性,单元测试有什么不同......?我会得到或失去什么?
它们在依赖注入框架中的注册方式有何不同?
选项1
public interface IRepository<T> where T : class
{
T Get(object id);
void Attach(T entity);
IQueryable<T> GetAll();
void Insert(T entity);
void Delete(T entity);
void SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)
选项2
public interface IRepository : IDisposable
{
IQueryable<T> GetAll<T>();
void Delete<T>(T entity);
void Add<T>(T entity);
void SaveChanges();
bool IsDisposed();
}
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 27
最大的区别在于IRepository<T>绑定到单个类型,而IRepository可能绑定到多个类型.哪一个适合高度依赖于您的特定情况.
一般来说,我觉得IRepository<T>更有用.在使用时,它非常清楚是什么IRepository<T>(T)的内容.另一方面,从给定IRepository内部包含的内容来看,它并不清楚.
在我必须存储多种类型的对象的情况下,我通常会创建一个IRepository<T>实例映射.例如:Dictionary<T,IRepository<T>>.
| 归档时间: |
|
| 查看次数: |
13903 次 |
| 最近记录: |