Minor improvements to EF usage.
1) Queries are untracked by default. 2) Use projection query in CartSummaryComponent. 3) Removed redundant async methods.
This commit is contained in:
parent
19325a6cce
commit
4cf6e463bb
|
|
@ -1,3 +1,4 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
|
@ -17,21 +18,14 @@ namespace MusicStore.Components
|
|||
|
||||
public async Task<IViewComponentResult> InvokeAsync()
|
||||
{
|
||||
var cartItems = await GetCartItems();
|
||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||
|
||||
var cartItems = await cart.GetCartAlbumTitles();
|
||||
|
||||
ViewBag.CartCount = cartItems.Count();
|
||||
ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
|
||||
ViewBag.CartCount = cartItems.Count;
|
||||
ViewBag.CartSummary = string.Join("\n", cartItems);
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
private async Task<IOrderedEnumerable<string>> GetCartItems()
|
||||
{
|
||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||
|
||||
return (await cart.GetCartItems())
|
||||
.Select(a => a.Album.Title)
|
||||
.OrderBy(x => x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -24,9 +24,9 @@ namespace MusicStore.Components
|
|||
return View(genres);
|
||||
}
|
||||
|
||||
private async Task<List<Genre>> GetGenres()
|
||||
private Task<List<Genre>> GetGenres()
|
||||
{
|
||||
return await DbContext.Genres
|
||||
return DbContext.Genres
|
||||
.Include(g => g.Albums).ThenInclude(a => a.OrderDetails)
|
||||
// TODO use nested sum https://github.com/aspnet/EntityFramework/issues/3792
|
||||
//.OrderByDescending(
|
||||
|
|
|
|||
|
|
@ -55,12 +55,12 @@ namespace MusicStore.Controllers
|
|||
return View("~/Views/Shared/AccessDenied.cshtml");
|
||||
}
|
||||
|
||||
private async Task<List<Album>> GetTopSellingAlbumsAsync(MusicStoreContext dbContext, int count)
|
||||
private Task<List<Album>> GetTopSellingAlbumsAsync(MusicStoreContext dbContext, int count)
|
||||
{
|
||||
// Group the order details by album and return
|
||||
// the albums with the highest count
|
||||
|
||||
return await dbContext.Albums
|
||||
return dbContext.Albums
|
||||
.OrderByDescending(a => a.OrderDetails.Count)
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace MusicStore.Models
|
|||
public MusicStoreContext(DbContextOptions<MusicStoreContext> options)
|
||||
: base(options)
|
||||
{
|
||||
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||
}
|
||||
|
||||
public DbSet<Album> Albums { get; set; }
|
||||
|
|
|
|||
|
|
@ -88,10 +88,22 @@ namespace MusicStore.Models
|
|||
|
||||
public Task<List<CartItem>> GetCartItems()
|
||||
{
|
||||
return _dbContext.CartItems.
|
||||
Where(cart => cart.CartId == _shoppingCartId).
|
||||
Include(c => c.Album).
|
||||
ToListAsync();
|
||||
return _dbContext
|
||||
.CartItems
|
||||
.Where(cart => cart.CartId == _shoppingCartId)
|
||||
.Include(c => c.Album)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public Task<List<string>> GetCartAlbumTitles()
|
||||
{
|
||||
return _dbContext
|
||||
.CartItems
|
||||
.Where(cart => cart.CartId == _shoppingCartId)
|
||||
.Select(c => c.Album.Title)
|
||||
.Distinct()
|
||||
.OrderBy(n => n)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public Task<int> GetCount()
|
||||
|
|
@ -110,7 +122,8 @@ namespace MusicStore.Models
|
|||
// the current price for each of those albums in the cart
|
||||
// sum all album price totals to get the cart total
|
||||
|
||||
return _dbContext.CartItems
|
||||
return _dbContext
|
||||
.CartItems
|
||||
.Include(c => c.Album)
|
||||
.Where(c => c.CartId == _shoppingCartId)
|
||||
.Select(c => c.Album.Price * c.Count)
|
||||
|
|
|
|||
|
|
@ -43,12 +43,12 @@ namespace MusicStore
|
|||
if (useInMemoryStore)
|
||||
{
|
||||
services.AddDbContext<MusicStoreContext>(options =>
|
||||
options.UseInMemoryDatabase());
|
||||
options.UseInMemoryDatabase());
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddDbContext<MusicStoreContext>(options =>
|
||||
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));
|
||||
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));
|
||||
}
|
||||
|
||||
// Add Identity services to the services container
|
||||
|
|
|
|||
Loading…
Reference in New Issue