http://mikehadlow.blogspot.com/2008/03/using-irepository-pattern-with-linq-to.html
In the article, the generic IRepository interface is as follows:
public interface IRepository<T> where T : class
{
T GetById(int id);
IQueryable<T> GetAll();
void InsertOnSubmit(T entity);
void DeleteOnSubmit(T entity);
[Obsolete("Units of Work should be managed externally to the Repository.")]
void SubmitChanges();
}
My question is with regard to the GetById method, which takes an int argument. Since this is a generic interface, I would assume that the GetById method should also take a generic type argument. Suppose in my business domain, Book's ID is of type varchar(13), and Author's ID is of type int, then this IRepository interface which has declared GetById(int id) won't be applicable to Book.
What's the best practice for this issue? Should I declare GetById as shown below?
T GetById(object id);
Any thoughts on this? Thanks.
Either just always use int keys.
Or create an interface as:
public interface IRepository<T,TID> where T : class
{
T GetById(TID id);
...
(possible with some restrictions on TID as well)
Arne
I have used Object to pass two different primitive types on a method's
signature and casted the Object back to its primitive type. Of course,
the code knew at which point to use the object and cast it back to its
primitive type.