C# 具体意义上的假定<;T>;vs存储库,如果我不使用mock进行单元测试

C# 具体意义上的假定<;T>;vs存储库,如果我不使用mock进行单元测试,c#,repository,irepository,C#,Repository,Irepository,我有这个: 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(); } 我不喜欢为小型应用程序编写单元测试。存储库模式基于IoC和依赖注入模式,单元测试恰好需要IoC和依赖注入,以使测

我有这个:

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();
}

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

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

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

您可以将ViewModel
s构造函数(mvvm模式)设置为获取存储库接口IRepository的参数。现在,ViewModel的构造函数基于接口,而不是实现

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

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

你的意思是
SchoolclassRepository repo=newschoolclassrepository()?哦,是的,是一个打字错误…现在已修复你好,朗迪,我正在使用EF 4.0 RC。关于存储库模式和我的原始问题,您对此有何看法?引用:“我建议的一件事是,使用新的功能集(.NET 3.5)会带来一些新的操作方式。”来源:ok找到了我的答案:但为什么要将IRepo对象传递给VM的构造函数?为什么不这样做,请参见上文:IRepository repo=new SchoolclassRepository();您可以让viewmodel使用两个重载构造函数,一个是无参数的,默认情况下设置IRepository repo=new SchoolclassRepository(),就像您建议的那样,另一个是带有IRepository参数的。如果需要,这允许注入,但如果未通过IRepository,则具有默认值。我的ViewModel/IRepo方法是否会不如您通过构造函数参数注入IRepo的方法好?请告诉我优点/缺点。
IRepository<Schoolclass> repo = new SchoolclassRepository();
SchoolclassRepository repo = new SchoolclassRepository(); ??? 
private IRepository<Schoolclass> _repository
public ViewModel(IRepository<Schoolclass> Repository) 
{ this._repository = Repository; }
Repository repository = new ADONET.SchoolclassRepository(); 
or
Repository repository = new EntityFramework.SchoolclassRepository(); 
ViewModel viewmodel = new ViewModel(repository);