开发者

Simple repository for .Net 2.0

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 Develop开发者_如何学运维ersRepository : 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?


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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜