Created
November 18, 2014 18:29
-
-
Save Pierry/b6ba424f3425305e4a6f to your computer and use it in GitHub Desktop.
IRepositoryBase
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 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(); | |
} |
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 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