This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace counter by specific query to avoid load each items in memory (#…
…611) * replace counter by specific query to avoid load each items in memory * Create IBasketQueryService
- Loading branch information
1 parent
553a101
commit 64f150d
Showing
7 changed files
with
50 additions
and
129 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces | ||
{ | ||
public interface IBasketQueryService | ||
{ | ||
Task<int> CountTotalBasketItems(string username); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopWeb.Infrastructure.Data | ||
{ | ||
public class BasketQueryService : IBasketQueryService | ||
{ | ||
private readonly CatalogContext _dbContext; | ||
|
||
public BasketQueryService(CatalogContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
public async Task<int> CountTotalBasketItems(string username) | ||
{ | ||
var totalItems = await _dbContext.Baskets | ||
.Where(basket => basket.BuyerId == username) | ||
.SelectMany(item => item.Items) | ||
.SumAsync(sum => sum.Quantity); | ||
|
||
return totalItems; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,12 @@ | ||
using Ardalis.Specification; | ||
using Ardalis.Specification.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using Ardalis.Specification.EntityFrameworkCore; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopWeb.Infrastructure.Data | ||
{ | ||
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot | ||
{ | ||
public EfRepository(CatalogContext dbContext) : base(dbContext) | ||
{ | ||
public EfRepository(CatalogContext dbContext) : base(dbContext) | ||
{ | ||
} | ||
} | ||
} | ||
/// <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> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot | ||
// { | ||
// protected readonly CatalogContext _dbContext; | ||
|
||
// public EfRepository(CatalogContext dbContext) | ||
// { | ||
// _dbContext = dbContext; | ||
// } | ||
|
||
// public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default) | ||
// { | ||
// var keyValues = new object[] { id }; | ||
// return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken); | ||
// } | ||
|
||
// public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default) | ||
// { | ||
// return await _dbContext.Set<T>().ToListAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default) | ||
// { | ||
// var specificationResult = ApplySpecification(spec); | ||
// return await specificationResult.ToListAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default) | ||
// { | ||
// var specificationResult = ApplySpecification(spec); | ||
// return await specificationResult.CountAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default) | ||
// { | ||
// await _dbContext.Set<T>().AddAsync(entity, cancellationToken); | ||
// await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
// return entity; | ||
// } | ||
|
||
// public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) | ||
// { | ||
// _dbContext.Entry(entity).State = EntityState.Modified; | ||
// await _dbContext.SaveChangesAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) | ||
// { | ||
// _dbContext.Set<T>().Remove(entity); | ||
// await _dbContext.SaveChangesAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default) | ||
// { | ||
// var specificationResult = ApplySpecification(spec); | ||
// return await specificationResult.FirstAsync(cancellationToken); | ||
// } | ||
|
||
// public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default) | ||
// { | ||
// var specificationResult = ApplySpecification(spec); | ||
// return await specificationResult.FirstOrDefaultAsync(cancellationToken); | ||
// } | ||
|
||
// private IQueryable<T> ApplySpecification(ISpecification<T> spec) | ||
// { | ||
// var evaluator = new SpecificationEvaluator<T>(); | ||
// return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec); | ||
// } | ||
// } | ||
} |
This file was deleted.
Oops, something went wrong.
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
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