通用存储库和多种PK类型

Ref*_*din 3 .net c# repository-pattern linq-to-sql

我正在尝试使用Linq To SQL实现通用存储库。我的问题在于我的某些基础表具有intID和一些GuidID。我将如何处理?

public T GetById(int id)
{
    return this._context.GetTable<T>().Single(x => x.ID.Equals(id));
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*g8r 5

向存储库接口添加另一个通用参数。

public interface IRepo<TType, PKType>
{
    TType GetById(PKType id);
}
Run Code Online (Sandbox Code Playgroud)

然后实施它。

public class CustomerRepo : IRepo<Customer, Guid>
{
    public Customer GetById(Guid id)
    {
        // code to get from repo
    }
}
Run Code Online (Sandbox Code Playgroud)