I am attempting to follow the repository pattern described here.
Can someone please shed some light on what this really means, and what the benefits of it are? The
<TEntity> where TEntity : class, IEntity
part of the classes signature
public interface IRepository<TEntity> where TEntity : class, IEntity
{
TEntity Get(int id);
int Save(TEntity entity);
void Delete(TEntity entity);
}
I do not see any noticeable effect in my implementation of IRepository:
public class AppUserRepository : Repository, IRepository<AppUser>
{
public void Delete(AppUser entity)
{
using ( var context = new SourceEntities())
{
context.AppUsers.Attach(entity);
context.AppUsers.Remove(entity);
context.SaveChanges();
}
}
public AppUser Get(int id)
{
using (var context = new SourceEntities())
{
return context.AppUsers.Where(x => x.Id == id).SingleOrDefault();
}
}
public int Save(AppUser entity)
{
using (var context = new SourceEntities())
{
if ( entity.Id == 0 )
{
// this is a new record
context.AppUsers.Add(entity);
}
else
{
// existing record
context.AppUsers.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
// save the record
context.SaveChanges();
return entity.Id;
}
}
}