【问题标题】:concrete sense of IRepository<T> vs Repository if I do not do unit tests with mocks如果我不使用模拟进行单元测试,IRepository<T> vs Repository 的具体意义
【发布时间】:2010-02-17 22:56:44
【问题描述】:

我有这个:

public interface IRepository<T> where T : class
{
    void Delete(T entity);
    void Add(T entity);
    void Attach(T entity);
    void Detach(T entity);
    void SaveChanges();
}

现在我为我的每个实体创建了实现通用 IRepository 的具体类 =>

public class SchoolclassRepository : IRepository<Schoolclass>
{
    public void Delete(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Add(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Attach(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Detach(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void SaveChanges()
    {
        throw new NotImplementedException();
    }
}

在我的 ViewModel 的构造函数(mvvm 模式)中,我这样做 =>

IRepository<Schoolclass> repo = new SchoolclassRepository();

当我必须在每个实体类中为我的 CRUD 操作编写代码时,IRepository 有什么优势?

在 Customer、Product、Pupil 中,我实现了 IRepository&lt;Product&gt;IRepository&lt;Customer&gt;IRepository&lt;Pupil&gt; 等任何类......并且我实现了接口的方法。

为什么我不能说 =>

SchoolclassRepository repo = new SchoolclassRepository(); ??? 

我不喜欢为小型应用编写单元测试。

【问题讨论】:

  • 您的意思是SchoolclassRepository repo = new SchoolclassRepository(); 吗?
  • 哦,是的,是错字...现在已修复

标签: c# repository irepository


【解决方案1】:

存储库模式基于 IoC 和依赖注入模式,单元测试恰好需要 IoC 和依赖注入以使测试更容易。它最初并不打算成为编写数据访问层的另一种方式,尽管许多人都这样发布和实现它。对于小型应用程序,这取决于您要投入多少精力。

通常,实现 SchoolclassRepository 将位于与 IRepository 接口不同的命名空间中,因此您可以支持多种实现。 (不是规则)

您可以将 ViewModels constructor (mvvm pattern) to take a parameter for the repository interface IRepository&lt;Schoolclass&gt;. Now your ViewModels 构造函数设置为基于接口而不是实现。

private IRepository<Schoolclass> _repository
public ViewModel(IRepository<Schoolclass> Repository) 
{ this._repository = Repository; }

为什么会出现上述情况......

该模式还使未来的更改更容易进行。

如果您的 SchoolclassRepository() 实现使用 ADO.NET(sqlconnections、sqlcommands...)来访问数据,您可以稍后构建另一个使用 NHibernate、Entity Framework 或其他数据访问方法的 SchoolclassRepository()。现在您所要做的就是更改您的消费者以使用目标实现并将它们注入 ViewModel。

Repository repository = new ADONET.SchoolclassRepository(); 
or
Repository repository = new EntityFramework.SchoolclassRepository(); 
ViewModel viewmodel = new ViewModel(repository);

这只是对用途的快速查看,我建议进一步研究存储库模式以及如何使其适合您。你显然对它感兴趣。

【讨论】:

  • 您好,longday,我使用的是 EF 4.0 RC。考虑到存储库模式和我的原始问题,您有何看法?
  • quote:“我要建议的一件事是,新功能集 (.NET 3.5) 带来了一些新的做事方式。”来源:weblogs.asp.net/fredriknormen/archive/2008/04/24/…
  • ok 找到了我的答案:codeproject.com/KB/database/ImplRepositoryPatternEF.aspx 但是为什么要将 IRepo 对象传递给 VM 的构造函数呢?为什么不这样做,见上文:IRepository repo = new SchoolclassRepository();
  • 您可以让视图模型使用两个重载的构造函数,一个无参数设置 IRepository repo = new SchoolclassRepository() 默认情况下,如您建议的那样,另一个带有 IRepository 参数。如果需要,这允许注入,但如果未通过 IRepository,则具有默认值。
  • 我的 ViewModel/IRepo 方法会不会像你通过构造函数参数注入 IRepo 的方法那样好用?请告诉我优点/缺点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 2017-08-16
  • 2013-03-20
  • 1970-01-01
相关资源
最近更新 更多