Skip to content

Commit

Permalink
Merge pull request dotnet-architecture#320 from MoienTajik/master
Browse files Browse the repository at this point in the history
Made the AddAsync method fully asynchronous
  • Loading branch information
efleming18 authored Oct 24, 2019
2 parents 15fd172 + 96e16ca commit 8ccc87d
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Infrastructure/Data/EfRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -20,12 +20,12 @@ public EfRepository(CatalogContext dbContext)
{
_dbContext = dbContext;
}

public virtual async Task<T> GetByIdAsync(int id)
{
return await _dbContext.Set<T>().FindAsync(id);
}

public async Task<IReadOnlyList<T>> ListAllAsync()
{
return await _dbContext.Set<T>().ToListAsync();
Expand All @@ -35,35 +35,35 @@ public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
{
return await ApplySpecification(spec).ToListAsync();
}

public async Task<int> CountAsync(ISpecification<T> spec)
{
return await ApplySpecification(spec).CountAsync();
}

public async Task<T> AddAsync(T entity)
{
_dbContext.Set<T>().Add(entity);
await _dbContext.Set<T>().AddAsync(entity);
await _dbContext.SaveChangesAsync();

return entity;
}

public async Task UpdateAsync(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}

public async Task DeleteAsync(T entity)
{
_dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync();
}

private IQueryable<T> ApplySpecification(ISpecification<T> spec)
{
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
}
}
}
}
}

0 comments on commit 8ccc87d

Please sign in to comment.