Merge pull request #320 from MoienTajik/master

Made the AddAsync method fully asynchronous
This commit is contained in:
Eric Fleming
2019-10-24 13:37:24 -04:00
committed by GitHub

View File

@@ -1,6 +1,6 @@
using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -20,12 +20,12 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
{ {
_dbContext = dbContext; _dbContext = dbContext;
} }
public virtual async Task<T> GetByIdAsync(int id) public virtual async Task<T> GetByIdAsync(int id)
{ {
return await _dbContext.Set<T>().FindAsync(id); return await _dbContext.Set<T>().FindAsync(id);
} }
public async Task<IReadOnlyList<T>> ListAllAsync() public async Task<IReadOnlyList<T>> ListAllAsync()
{ {
return await _dbContext.Set<T>().ToListAsync(); return await _dbContext.Set<T>().ToListAsync();
@@ -35,7 +35,7 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
{ {
return await ApplySpecification(spec).ToListAsync(); return await ApplySpecification(spec).ToListAsync();
} }
public async Task<int> CountAsync(ISpecification<T> spec) public async Task<int> CountAsync(ISpecification<T> spec)
{ {
return await ApplySpecification(spec).CountAsync(); return await ApplySpecification(spec).CountAsync();
@@ -43,27 +43,27 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
public async Task<T> AddAsync(T entity) public async Task<T> AddAsync(T entity)
{ {
_dbContext.Set<T>().Add(entity); await _dbContext.Set<T>().AddAsync(entity);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
return entity; return entity;
} }
public async Task UpdateAsync(T entity) public async Task UpdateAsync(T entity)
{ {
_dbContext.Entry(entity).State = EntityState.Modified; _dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }
public async Task DeleteAsync(T entity) public async Task DeleteAsync(T entity)
{ {
_dbContext.Set<T>().Remove(entity); _dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }
private IQueryable<T> ApplySpecification(ISpecification<T> spec) private IQueryable<T> ApplySpecification(ISpecification<T> spec)
{ {
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec); return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
} }
} }
} }