Skip to content

Instantly share code, notes, and snippets.

@Pierry
Created November 18, 2014 18:29
Show Gist options
  • Save Pierry/b6ba424f3425305e4a6f to your computer and use it in GitHub Desktop.
Save Pierry/b6ba424f3425305e4a6f to your computer and use it in GitHub Desktop.
IRepositoryBase
public interface IRepositoryBase<TEntity> where TEntity : class
{
TEntity Add(TEntity item);
TEntity GetById(int id);
IEnumerable<TEntity> Get();
bool Update(TEntity item);
int Count();
void Dispose();
}
public class RepositoryBase<TEntity> : IDisposable, IRepositoryBase<TEntity> where TEntity : class
{
protected MyEntities Db = new MyEntities();
public void Dispose()
{
throw new NotImplementedException();
}
public TEntity Add(TEntity item)
{
try
{
TEntity adicionado = Db.Set<TEntity>().Add(item);
Db.SaveChanges();
return adicionado;
}
catch (DbUpdateConcurrencyException ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
public TEntity GetById(int id)
{
return Db.Set<TEntity>().Find(id);
}
public IEnumerable<TEntity> Get()
{
return Db.Set<TEntity>().ToList();
}
public bool Update(TEntity item)
{
try
{
Db.Entry(item).State = EntityState.Modified;
Db.SaveChanges();
return true;
}
catch (DbUpdateConcurrencyException ex)
{
Console.WriteLine(ex.InnerException);
return false;
}
}
public int Count()
{
return Db.Set<TEntity>().Count();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment