Skip to content

Instantly share code, notes, and snippets.

@onurcelikeng
Created April 21, 2017 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onurcelikeng/7bd5e99e1521e7ed545a620f32f1a38e to your computer and use it in GitHub Desktop.
Save onurcelikeng/7bd5e99e1521e7ed545a620f32f1a38e to your computer and use it in GitHub Desktop.
Repo for C#
public class Repository<T> : IDisposable, IRepository<T> where T : class
{
DataContext context;
DbSet<T> Table;
public Repository()
{
context = new DataContext();
Table = context.Set<T>();
}
~Repository()
{
Dispose();
}
public DbSet<T> Context()
{
return Table;
}
public bool Add(T obj)
{
Table.Add(obj);
return Save();
}
public bool Delete(T obj)
{
Table.Remove(obj);
return Save();
}
public T Find(object id)
{
return Table.Find(id);
}
public List<T> List()
{
return Table.ToList();
}
public bool Update(T obj)
{
return Save();
}
public List<T> Where(Func<T, bool> where)
{
return Table.Where(where).ToList();
}
public T First(Func<T, bool> where)
{
return Table.FirstOrDefault(where);
}
private bool Save()
{
try
{
context.SaveChanges();
return true;
}
catch (Exception)
{
//LogHelper.Log("Veritabanında işlem hatası: " + ex.Message, LogType.Error, ex);
return false;
}
}
public void Dispose()
{
this.context.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment