Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About GetById in a generic Repository

11 views
Skip to first unread message

csharper

unread,
Feb 25, 2011, 11:23:29 PM2/25/11
to
Hi, I am interested in implementing the Repository pattern for my data store. I searched and found the following article.

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.

Arne Vajhøj

unread,
Feb 26, 2011, 1:59:21 PM2/26/11
to

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

Big Steel

unread,
Feb 27, 2011, 3:50:31 AM2/27/11
to

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.

0 new messages