.Net 2.0 的简单存储库

发布于 2024-10-26 04:44:24 字数 823 浏览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-11-02 04:44:24

我可能是错的,但听起来你想使用通用约束:

public interface IEmployeeRepository<T>
    where T : Employee
{
    DataTable Employees { get; }
    void SaveEmployee(T employee);
    void DeleteEmployee(T employee);
}

public class DevelopersRepository : IEmployeeRepository<Developer>
{             
    public void SaveEmployee(Developer employee)
    {
        Database db = new SqlDatabase(connectionString);
        DbCommand dbCommand = db.GetStoredProcCommand("Developers_Insert",   employee.ProgrammingLanguage);
        db.ExecuteNonQuery(dbCommand);
    }
}

I could be wrong but it sounds like you want to use a generic constraint:

public interface IEmployeeRepository<T>
    where T : Employee
{
    DataTable Employees { get; }
    void SaveEmployee(T employee);
    void DeleteEmployee(T employee);
}

public class DevelopersRepository : IEmployeeRepository<Developer>
{             
    public void SaveEmployee(Developer employee)
    {
        Database db = new SqlDatabase(connectionString);
        DbCommand dbCommand = db.GetStoredProcCommand("Developers_Insert",   employee.ProgrammingLanguage);
        db.ExecuteNonQuery(dbCommand);
    }
}
林空鹿饮溪 2024-11-02 04:44:24

DevelopersRepository 实现了 IEmployeesRepository,因此它必须实现接口中的所有方法。

public void SaveEmployee(Employee employee)
{
    if (!(employee is Developer)) throw new Exception("...");
    ...
}

DevelopersRepository implements IEmployeesRepository so it must implement all methods in the interface.

public void SaveEmployee(Employee employee)
{
    if (!(employee is Developer)) throw new Exception("...");
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文