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:
Andrew Peters 2016-04-13 14:57:15 -07:00
parent 19325a6cce
commit 4cf6e463bb
6 changed files with 31 additions and 23 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -17,21 +18,14 @@ namespace MusicStore.Components
public async Task<IViewComponentResult> InvokeAsync() public async Task<IViewComponentResult> InvokeAsync()
{ {
var cartItems = await GetCartItems(); var cart = ShoppingCart.GetCart(DbContext, HttpContext);
ViewBag.CartCount = cartItems.Count(); var cartItems = await cart.GetCartAlbumTitles();
ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
ViewBag.CartCount = cartItems.Count;
ViewBag.CartSummary = string.Join("\n", cartItems);
return View(); 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);
}
} }
} }

View File

@ -24,9 +24,9 @@ namespace MusicStore.Components
return View(genres); 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) .Include(g => g.Albums).ThenInclude(a => a.OrderDetails)
// TODO use nested sum https://github.com/aspnet/EntityFramework/issues/3792 // TODO use nested sum https://github.com/aspnet/EntityFramework/issues/3792
//.OrderByDescending( //.OrderByDescending(

View File

@ -55,12 +55,12 @@ namespace MusicStore.Controllers
return View("~/Views/Shared/AccessDenied.cshtml"); 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 // Group the order details by album and return
// the albums with the highest count // the albums with the highest count
return await dbContext.Albums return dbContext.Albums
.OrderByDescending(a => a.OrderDetails.Count) .OrderByDescending(a => a.OrderDetails.Count)
.Take(count) .Take(count)
.ToListAsync(); .ToListAsync();

View File

@ -10,6 +10,7 @@ namespace MusicStore.Models
public MusicStoreContext(DbContextOptions<MusicStoreContext> options) public MusicStoreContext(DbContextOptions<MusicStoreContext> options)
: base(options) : base(options)
{ {
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
} }
public DbSet<Album> Albums { get; set; } public DbSet<Album> Albums { get; set; }

View File

@ -88,10 +88,22 @@ namespace MusicStore.Models
public Task<List<CartItem>> GetCartItems() public Task<List<CartItem>> GetCartItems()
{ {
return _dbContext.CartItems. return _dbContext
Where(cart => cart.CartId == _shoppingCartId). .CartItems
Include(c => c.Album). .Where(cart => cart.CartId == _shoppingCartId)
ToListAsync(); .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() public Task<int> GetCount()
@ -110,7 +122,8 @@ namespace MusicStore.Models
// the current price for each of those albums in the cart // the current price for each of those albums in the cart
// sum all album price totals to get the cart total // sum all album price totals to get the cart total
return _dbContext.CartItems return _dbContext
.CartItems
.Include(c => c.Album) .Include(c => c.Album)
.Where(c => c.CartId == _shoppingCartId) .Where(c => c.CartId == _shoppingCartId)
.Select(c => c.Album.Price * c.Count) .Select(c => c.Album.Price * c.Count)

View File

@ -43,12 +43,12 @@ namespace MusicStore
if (useInMemoryStore) if (useInMemoryStore)
{ {
services.AddDbContext<MusicStoreContext>(options => services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase()); options.UseInMemoryDatabase());
} }
else else
{ {
services.AddDbContext<MusicStoreContext>(options => services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")])); options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));
} }
// Add Identity services to the services container // Add Identity services to the services container