通用存储库 - IRepository< T>或IRepository [英] Generic repository - IRepository<T> or IRepository

查看:880
本文介绍了通用存储库 - IRepository< T>或IRepository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了创建通用信息库两种不同的方法。那两个方法(优劣)之间的区别是什么?
请diregard差异的方法,因为我是

之间的兴趣差异

 公共接口IRepository< T>其中T:类

 公共接口IRepository:IDisposable的

是否有任何功能的差异性,灵活性,单元测试...?什么会我得不到或者失去的?结果
是有他们是如何在依赖注入框架注册有什么区别?



选项1

 公共接口IRepository< T>其中T:类
{
吨得到(对象ID);
无效连接(T实体);
&IQueryable的LT; T>得到所有();
无效插入(T实体);
无效删除(T实体);
无效的SubmitChanges();
}

选项2



 公共接口IRepository:IDisposable的
{
&IQueryable的LT; T> GETALL< T>();
无效删除< T>(T实体);
无效加入< T>(T实体);
无效的SaveChanges();
布尔IsDisposed();
}


解决方案

最大的区别在于 IRepository< T> 绑定到一个单一类型而一个 IRepository 可能被绑定到多个类型。哪一个是适当的高度取决于您的特定情况。



一般来说,我觉得 IRepository< T> 更有用。在使用的时候这是非常清楚什么 IRepository<的内容; T> 的( T )。在另一方面它不是从明确给定的 IRepository 什么是包含在它的内部。



在情况下,我有存储多个类型的对象,我通常创建地图 IRepository<的; T> 实例。例如:词典< T,IRepository< T>>


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

 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

 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. Which one is appropriate is highly dependent upon your particular scenario.

Generally speaking I find IRepository<T> to be more useful. At the time of use it's extremely clear what the contents of IRepository<T> are (T). On the other hand it's not clear from a given IRepository what is contained inside of it.

In cases where I have to store multiple types of objects, I usually create a map of IRepository<T> instances. For instance: Dictionary<T,IRepository<T>>.

这篇关于通用存储库 - IRepository&LT; T&GT;或IRepository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆