Initial Upgrade to .NET Core 2.0 (#50)
* Ardalis/upgrade1 (#44) * Upgrading to netcore 2.0 Updating repository to support async options and refactoring to use it. * Starting work on tracking customer orders feature. * Cleaning up some bugs Working on basket view component implementation * Fixing up styles, especially for basket in header. * Adding Order Features (#47) * Working on order model binding from checkout page - WIP * Small layout tweaks (#43) * Updating quantities implemented. * Fixed basket widget count * Order History (#49) * working on creating and viewing orders. * Working on wiring up listing of orders * List orders page works as expected. Needed to support ThenInclude scenarios. Currently using strings.
This commit is contained in:
@@ -3,36 +3,68 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Data
|
||||
{
|
||||
public class EfRepository<T> : IRepository<T> where T : BaseEntity
|
||||
/// <summary>
|
||||
/// "There's some repetition here - couldn't we have some the sync methods call the async?"
|
||||
/// https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class EfRepository<T> : IRepository<T>, IAsyncRepository<T> where T : BaseEntity
|
||||
{
|
||||
private readonly CatalogContext _dbContext;
|
||||
protected readonly CatalogContext _dbContext;
|
||||
|
||||
public EfRepository(CatalogContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public T GetById(int id)
|
||||
public virtual T GetById(int id)
|
||||
{
|
||||
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id);
|
||||
return _dbContext.Set<T>().Find(id);
|
||||
}
|
||||
|
||||
public List<T> List()
|
||||
public virtual async Task<T> GetByIdAsync(int id)
|
||||
{
|
||||
return _dbContext.Set<T>().ToList();
|
||||
return await _dbContext.Set<T>().FindAsync(id);
|
||||
}
|
||||
|
||||
public List<T> List(ISpecification<T> spec)
|
||||
public IEnumerable<T> ListAll()
|
||||
{
|
||||
return _dbContext.Set<T>().AsEnumerable();
|
||||
}
|
||||
|
||||
public async Task<List<T>> ListAllAsync()
|
||||
{
|
||||
return await _dbContext.Set<T>().ToListAsync();
|
||||
}
|
||||
|
||||
public IEnumerable<T> List(ISpecification<T> spec)
|
||||
{
|
||||
var queryableResultWithIncludes = spec.Includes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
return queryableResultWithIncludes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
var secondaryResult = spec.IncludeStrings
|
||||
.Aggregate(queryableResultWithIncludes,
|
||||
(current, include) => current.Include(include));
|
||||
return secondaryResult
|
||||
.Where(spec.Criteria)
|
||||
.ToList();
|
||||
.AsEnumerable();
|
||||
}
|
||||
public async Task<List<T>> ListAsync(ISpecification<T> spec)
|
||||
{
|
||||
var queryableResultWithIncludes = spec.Includes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
var secondaryResult = spec.IncludeStrings
|
||||
.Aggregate(queryableResultWithIncludes,
|
||||
(current, include) => current.Include(include));
|
||||
|
||||
return await secondaryResult
|
||||
.Where(spec.Criteria)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public T Add(T entity)
|
||||
@@ -43,10 +75,12 @@ namespace Infrastructure.Data
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
public async Task<T> AddAsync(T entity)
|
||||
{
|
||||
_dbContext.Set<T>().Remove(entity);
|
||||
_dbContext.SaveChanges();
|
||||
_dbContext.Set<T>().Add(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
@@ -54,6 +88,21 @@ namespace Infrastructure.Data
|
||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
public async Task UpdateAsync(T entity)
|
||||
{
|
||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
_dbContext.Set<T>().Remove(entity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
public async Task DeleteAsync(T entity)
|
||||
{
|
||||
_dbContext.Set<T>().Remove(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user