I’m experimenting with the following mockable repository pattern:
public interface IEmployeeRepository
{
ITable<Employee> Employees { get; }
}
This represents a container capable of returning an ITable of Employee objects. I want to be able to create a mock object based off of the repository, as well as have my Linq-to-Sql DataContext implement the repository interface. So I thought I could just use partial class to declare my DataContext type to implement IEmployeeRepository, as it already has an auto-generated member Employees of type Table<Employee>:
public partial class MyDataContext : IEmployeeRepository { }
I get the following error message as a result:
‘MyDataContext’ does not implement interface member
‘IEmployeeRepository.Employees’. ‘MyDataContext.Employees’ cannot
implement ‘IEmployeeRepository.Employees’ because it does not have the
matching return type of ‘System.Data.Linq.ITable’.
But Table<Employee> inherits ITable<Employee>, so shouldn’t it be a suitable return type to implement the interface?
The implementing class’ type must match the interface type exactly. If you can’t change the interface, you could implement it explicitly: