Here is an example:
public class GenericRepository<TEntity> where TEntity : class
{
public virtual TEntity GetByID(object id)
{
// ...
}
public virtual void Insert(TEntity entity)
{
// ...
}
public virtual void Delete(TEntity entityToDelete)
{
// ...
}
}
These methods would apply to all your entities. However let's say you want to get a User
by an email (a property that only the user has) you can extend the GenericRepository
with your additional method:
public class UserRepository : GenericRepository<User>
{
// Now the User repository has all the methods of the Generic Repository
// with addition to something a bit more specific
public User GetByEmail(string email)
{
// ..
}
}
Edit - In the article they're using a GenericRepository in the Unit of Work, but when your repository is more specific you use it instead. For example:
public class UnitOfWork : IDisposable
{
private GenericRepository<Department> departmentRepository;
private GenericRepository<Course> courseRepository;
// here is the one we created, which is essentially a GenericRepository as well
private UserRepository userRepository;
public UserRepository UserRepository
{
get
{
if (this.userRepository== null)
{
this.userRepository= new UserRepository(context);
}
return this.userRepository;
}
}
// ...
}