[英]Generic repository - IRepository<T> or IRepository
I have seen two different approaches for creating generic repositories. 我已经看到了两种不同的方法来创建通用存储库。 What are differences between those two approaches (pros and cons) ?
这两种方法之间有什么区别(利弊)? Please diregard difference in the methods because I am interested in difference between
请对方法有所区别,因为我对它们之间的区别感兴趣
public interface IRepository<T> where T : class
and 和
public interface IRepository : IDisposable
Is there any difference in functionality, flexibility, unit testing ... ? 功能,灵活性,单元测试有什么不同......? What will I get or lose ?
我会得到或失去什么?
Is there any difference how they are registered in Dependency Injection frameworks ? 它们在依赖注入框架中的注册方式有何不同?
Option 1 选项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();
}
Option 2 选项2
public interface IRepository : IDisposable
{
IQueryable<T> GetAll<T>();
void Delete<T>(T entity);
void Add<T>(T entity);
void SaveChanges();
bool IsDisposed();
}
The biggest difference is that IRepository<T>
is bound to a single type while an IRepository
is potentially bound to multiple types. 最大的区别是
IRepository<T>
绑定到单个类型,而IRepository
可能绑定到多个类型。 Which one is appropriate is highly dependent upon your particular scenario. 哪一个适合高度依赖于您的特定情况。
Generally speaking I find IRepository<T>
to be more useful. 一般来说,我发现
IRepository<T>
更有用。 At the time of use it's extremely clear what the contents of IRepository<T>
are ( T
). 在使用时,非常清楚
IRepository<T>
的内容是什么( T
)。 On the other hand it's not clear from a given IRepository
what is contained inside of it. 另一方面,从给定的
IRepository
中包含的内容并不清楚。
In cases where I have to store multiple types of objects, I usually create a map of IRepository<T>
instances. 在我必须存储多种类型的对象的情况下,我通常会创建一个
IRepository<T>
实例的映射。 For instance: Dictionary<T,IRepository<T>>
. 例如:
Dictionary<T,IRepository<T>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.