Created
May 24, 2018 04:17
-
-
Save JonathanLoscalzo/aea0d503b4d94a2cf9a39d35e49a1e81 to your computer and use it in GitHub Desktop.
Repository Implementation from : https://github.com/ardalis/CleanArchitecture/blob/master/src/CleanArchitecture.Infrastructure/Data/EfRepository.cs
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 EfRepository<T> : IRepository<T> where T : BaseEntity | |
{ | |
private readonly AppDbContext _dbContext; | |
public EfRepository(AppDbContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
public T GetById(int id) | |
{ | |
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id); | |
} | |
public List<T> List() | |
{ | |
return _dbContext.Set<T>().ToList(); | |
} | |
public T Add(T entity) | |
{ | |
_dbContext.Set<T>().Add(entity); | |
_dbContext.SaveChanges(); | |
return entity; | |
} | |
public void Delete(T entity) | |
{ | |
_dbContext.Set<T>().Remove(entity); | |
_dbContext.SaveChanges(); | |
} | |
public void Update(T entity) | |
{ | |
_dbContext.Entry(entity).State = EntityState.Modified; | |
_dbContext.SaveChanges(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment