我试图创建一个基于这篇文章 的通用LINQ-TO-SQL仓库,基本上让你定义一个通用的基础仓库类,然后你可以通过派生自通用基础类来定义所有实际的仓库类。
我想要使用带有或不带有数据上下文的仓库选项,因此我决定在通用基类中创建两个构造函数:
public abstract class GenericRepository<T, C>
where T : class
where C : System.Data.Linq.DataContext, new()
{
public C _db;
public GenericRepository()
{
_db = new C();
}
public GenericRepository(C db)
{
_db = db;
}
public IQueryable<T> FindAll()
... and other repository functions
}
为了使用它,我需要派生出我的实际仓库类:
public class TeamRepository : GenericRepository<Team, AppDataContext> { }
现在,如果我尝试使用这个参数:
AppDataContext db = new AppDataContext();
TeamRepository repos=new TeamRepository(db);
我遇到了这个错误:'App.Models.TeamRepository'不包含一个带有1个参数的构造函数。
看起来在C#中你不能继承构造函数,那么你怎样编写代码才能调用:TeamRepository()或TeamRepository(db)呢?