Created
April 21, 2017 18:18
-
-
Save onurcelikeng/7bd5e99e1521e7ed545a620f32f1a38e to your computer and use it in GitHub Desktop.
Repo for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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