Created
June 25, 2021 01:38
-
-
Save MahdiKarimipour/5ed22260c5af48949bae327d52590282 to your computer and use it in GitHub Desktop.
Unit of Work Repository
This file contains hidden or 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 CustomerRepository : RepositoryBase<SampleDBContext>, IRespository<Guid, Customer> | |
{ | |
public CustomerRepository(SubscriptionDBContext _context) : base(_context) | |
{ | |
} | |
public void Create(Customer customer) | |
{ | |
Context.Customers.Add(customer); | |
} | |
public void Delete(Guid id) | |
{ | |
var customerToDelete = Context.Customers.FirstOrDefault(c => c.Id == id); | |
Context.Customers.Remove(customerToDelete); | |
} | |
public IQueryable<Customer> Get(Expression<Func<Customer, bool>> predicate, int currentPage = 0, int pageSize = 0) | |
{ | |
if (pageSize == 0) | |
{ | |
return Context.Customers | |
.Where(predicate); | |
} | |
return Context.Customers | |
.Where(predicate) | |
.Skip((currentPage - 1) * pageSize) | |
.Take(pageSize); | |
} | |
public async Task<Customer> GetAsync(Guid Id) | |
{ | |
return await Context.Customers.FirstOrDefaultAsync(c => c.Id == Id); | |
} | |
public void Update(Guid id, Customer customer) | |
{ | |
customer.Id = id; | |
Context.Entry(customer).State = EntityState.Modified; | |
} | |
public void Upsert(Guid id, Customer customer) | |
{ | |
customer.Id = id; | |
if (customer.Id != Guid.Empty) | |
{ | |
Context.Entry(customer).State = EntityState.Modified; | |
} | |
else | |
{ | |
Context.Customers.Add(customer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment