.Net 2.0 的简单存储库
我正在尝试对 .Net 2.0 中制作的网站使用存储库模式。所有客户端代码都需要数据表进行绑定,我无法更改它,所以请耐心等待。
我遇到了一个(可能是愚蠢的)问题。我无法弄清楚如何将强类型实体传递到我的具体存储库。该接口迫使我传递 Employee 类型,但我需要传递 Developer 类型(它派生自 Employee),因为它具有基类中未找到的特定属性。
public interface IEmployeesRepository
{
DataTable Employees { get; }
void SaveEmployee(Employee employee);
void DeleteEmployee(Employee employee);
}
public class DevelopersRepository : IEmployeesRepository
{
public void SaveEmployee(Developer employee)
{
Database db = new SqlDatabase(connectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Developers_Insert", employee.ProgrammingLanguage);
db.ExecuteNonQuery(dbCommand);
}
}
}
我尝试使用泛型,但我仍然没有强类型对象,对吧?
I am trying to use the repository pattern for a site made in .Net 2.0. All client code is expecting datatable for binding and I am not going to be able to change that, so please bear with me.
I am having a (probably dumb) issue. I cannot figure out how to pass a strongly typed entity to my concrete repository. The interface is forcing me to pass an Employee type but I need to pass a Developer type (it derives from Employee) because it has specific properties not found in the base class.
public interface IEmployeesRepository
{
DataTable Employees { get; }
void SaveEmployee(Employee employee);
void DeleteEmployee(Employee employee);
}
public class DevelopersRepository : IEmployeesRepository
{
public void SaveEmployee(Developer employee)
{
Database db = new SqlDatabase(connectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Developers_Insert", employee.ProgrammingLanguage);
db.ExecuteNonQuery(dbCommand);
}
}
}
I tried using generics instead but then I still will not have a strongly typed object right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能是错的,但听起来你想使用通用约束:
I could be wrong but it sounds like you want to use a generic constraint:
DevelopersRepository 实现了 IEmployeesRepository,因此它必须实现接口中的所有方法。
DevelopersRepository implements IEmployeesRepository so it must implement all methods in the interface.