From 5a3c4957d9707a9901891a6b909863d1a34cc37f Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 17 Nov 2015 16:18:25 -0800 Subject: [PATCH] Reacting to FromServices changes --- .../Controllers/StoreManagerController.cs | 40 ++++++++++++------- .../Controllers/AccountController.cs | 13 ++++-- .../Controllers/CheckoutController.cs | 20 +++++----- src/MusicStore/Controllers/HomeController.cs | 20 ++++------ .../Controllers/ManageController.cs | 20 +++++----- .../Controllers/ShoppingCartController.cs | 16 +++++--- src/MusicStore/Controllers/StoreController.cs | 17 ++++---- 7 files changed, 85 insertions(+), 61 deletions(-) diff --git a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs index bebab6c049..121282fbef 100644 --- a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs @@ -17,11 +17,12 @@ namespace MusicStore.Areas.Admin.Controllers [Authorize("ManageStore")] public class StoreManagerController : Controller { - [FromServices] - public MusicStoreContext DbContext { get; set; } + public StoreManagerController(MusicStoreContext dbContext) + { + DbContext = dbContext; + } - [FromServices] - public IMemoryCache Cache { get; set; } + public MusicStoreContext DbContext { get; } // // GET: /StoreManager/ @@ -37,12 +38,14 @@ namespace MusicStore.Areas.Admin.Controllers // // GET: /StoreManager/Details/5 - public async Task Details(int id) + public async Task Details( + [FromServices] IMemoryCache cache, + int id) { var cacheKey = GetCacheKey(id); Album album; - if(!Cache.TryGetValue(cacheKey, out album)) + if (!cache.TryGetValue(cacheKey, out album)) { album = await DbContext.Albums .Where(a => a.AlbumId == id) @@ -53,7 +56,7 @@ namespace MusicStore.Areas.Admin.Controllers if (album != null) { //Remove it from cache if not retrieved in last 10 minutes. - Cache.Set( + cache.Set( cacheKey, album, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10))); @@ -62,7 +65,7 @@ namespace MusicStore.Areas.Admin.Controllers if (album == null) { - Cache.Remove(cacheKey); + cache.Remove(cacheKey); return HttpNotFound(); } @@ -81,7 +84,10 @@ namespace MusicStore.Areas.Admin.Controllers // POST: /StoreManager/Create [HttpPost] [ValidateAntiForgeryToken] - public async Task Create(Album album, CancellationToken requestAborted) + public async Task Create( + Album album, + [FromServices] IMemoryCache cache, + CancellationToken requestAborted) { if (ModelState.IsValid) { @@ -94,7 +100,7 @@ namespace MusicStore.Areas.Admin.Controllers Url = Url.Action("Details", "Store", new { id = album.AlbumId }) }; - Cache.Remove("latestAlbum"); + cache.Remove("latestAlbum"); return RedirectToAction("Index"); } @@ -125,14 +131,17 @@ namespace MusicStore.Areas.Admin.Controllers // POST: /StoreManager/Edit/5 [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(Album album, CancellationToken requestAborted) + public async Task Edit( + [FromServices] IMemoryCache cache, + Album album, + CancellationToken requestAborted) { if (ModelState.IsValid) { DbContext.Update(album); await DbContext.SaveChangesAsync(requestAborted); //Invalidate the cache entry as it is modified - Cache.Remove(GetCacheKey(album.AlbumId)); + cache.Remove(GetCacheKey(album.AlbumId)); return RedirectToAction("Index"); } @@ -157,7 +166,10 @@ namespace MusicStore.Areas.Admin.Controllers // // POST: /StoreManager/RemoveAlbum/5 [HttpPost, ActionName("RemoveAlbum")] - public async Task RemoveAlbumConfirmed(int id, CancellationToken requestAborted) + public async Task RemoveAlbumConfirmed( + [FromServices] IMemoryCache cache, + int id, + CancellationToken requestAborted) { var album = await DbContext.Albums.Where(a => a.AlbumId == id).FirstOrDefaultAsync(); if (album == null) @@ -168,7 +180,7 @@ namespace MusicStore.Areas.Admin.Controllers DbContext.Albums.Remove(album); await DbContext.SaveChangesAsync(requestAborted); //Remove the cache entry as it is removed - Cache.Remove(GetCacheKey(id)); + cache.Remove(GetCacheKey(id)); return RedirectToAction("Index"); } diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index 20beac527b..b2acedc635 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -14,12 +14,17 @@ namespace MusicStore.Controllers [Authorize] public class AccountController : Controller { + public AccountController( + UserManager userManager, + SignInManager signInManager) + { + UserManager = userManager; + SignInManager = signInManager; + } - [FromServices] - public UserManager UserManager { get; set; } + public UserManager UserManager { get; } - [FromServices] - public SignInManager SignInManager { get; set; } + public SignInManager SignInManager { get; } // // GET: /Account/Login diff --git a/src/MusicStore/Controllers/CheckoutController.cs b/src/MusicStore/Controllers/CheckoutController.cs index a0eb848451..d7ae1d38ac 100644 --- a/src/MusicStore/Controllers/CheckoutController.cs +++ b/src/MusicStore/Controllers/CheckoutController.cs @@ -15,9 +15,6 @@ namespace MusicStore.Controllers { private const string PromoCode = "FREE"; - [FromServices] - public MusicStoreContext DbContext { get; set; } - // // GET: /Checkout/ public IActionResult AddressAndPayment() @@ -30,7 +27,10 @@ namespace MusicStore.Controllers [HttpPost] [ValidateAntiForgeryToken] - public async Task AddressAndPayment([FromForm] Order order, CancellationToken requestAborted) + public async Task AddressAndPayment( + [FromServices] MusicStoreContext dbContext, + [FromForm] Order order, + CancellationToken requestAborted) { if (!ModelState.IsValid) { @@ -52,14 +52,14 @@ namespace MusicStore.Controllers order.OrderDate = DateTime.Now; //Add the Order - DbContext.Orders.Add(order); + dbContext.Orders.Add(order); //Process the order - var cart = ShoppingCart.GetCart(DbContext, HttpContext); + var cart = ShoppingCart.GetCart(dbContext, HttpContext); await cart.CreateOrder(order); // Save all changes - await DbContext.SaveChangesAsync(requestAborted); + await dbContext.SaveChangesAsync(requestAborted); return RedirectToAction("Complete", new { id = order.OrderId }); } @@ -74,10 +74,12 @@ namespace MusicStore.Controllers // // GET: /Checkout/Complete - public async Task Complete(int id) + public async Task Complete( + [FromServices] MusicStoreContext dbContext, + int id) { // Validate customer owns this order - bool isValid = await DbContext.Orders.AnyAsync( + bool isValid = await dbContext.Orders.AnyAsync( o => o.OrderId == id && o.Username == HttpContext.User.GetUserName()); diff --git a/src/MusicStore/Controllers/HomeController.cs b/src/MusicStore/Controllers/HomeController.cs index 499c743f3f..50c0bbb1e3 100644 --- a/src/MusicStore/Controllers/HomeController.cs +++ b/src/MusicStore/Controllers/HomeController.cs @@ -11,28 +11,24 @@ namespace MusicStore.Controllers { public class HomeController : Controller { - [FromServices] - public MusicStoreContext DbContext { get; set; } - - [FromServices] - public IMemoryCache Cache { get; set; } - // // GET: /Home/ - public async Task Index() + public async Task Index( + [FromServices] MusicStoreContext dbContext, + [FromServices] IMemoryCache cache) { // Get most popular albums var cacheKey = "topselling"; List albums; - if(!Cache.TryGetValue(cacheKey, out albums)) + if (!cache.TryGetValue(cacheKey, out albums)) { - albums = await GetTopSellingAlbumsAsync(6); + albums = await GetTopSellingAlbumsAsync(dbContext, 6); if (albums != null && albums.Count > 0) { // Refresh it every 10 minutes. // Let this be the last item to be removed by cache if cache GC kicks in. - Cache.Set( + cache.Set( cacheKey, albums, new MemoryCacheEntryOptions() @@ -59,13 +55,13 @@ namespace MusicStore.Controllers return View("~/Views/Shared/AccessDenied.cshtml"); } - private async Task> GetTopSellingAlbumsAsync(int count) + private async Task> GetTopSellingAlbumsAsync(MusicStoreContext dbContext, int count) { // Group the order details by album and return // the albums with the highest count // TODO [EF] We don't query related data as yet, so the OrderByDescending isn't doing anything - return await DbContext.Albums + return await dbContext.Albums .OrderByDescending(a => a.OrderDetails.Count()) .Take(count) .ToListAsync(); diff --git a/src/MusicStore/Controllers/ManageController.cs b/src/MusicStore/Controllers/ManageController.cs index 90f7e563d5..b44ac59ad7 100644 --- a/src/MusicStore/Controllers/ManageController.cs +++ b/src/MusicStore/Controllers/ManageController.cs @@ -8,18 +8,20 @@ using MusicStore.Models; namespace MusicStore.Controllers { - /// - /// Summary description for ManageController - /// [Authorize] public class ManageController : Controller { - - [FromServices] - public UserManager UserManager { get; set; } + public ManageController( + UserManager userManager, + SignInManager signInManager) + { + UserManager = userManager; + SignInManager = signInManager; + } - [FromServices] - public SignInManager SignInManager { get; set; } + public UserManager UserManager { get; } + + public SignInManager SignInManager { get; } // // GET: /Manage/Index @@ -349,7 +351,7 @@ namespace MusicStore.Controllers { return await UserManager.FindByIdAsync(HttpContext.User.GetUserId()); } - + #endregion } } \ No newline at end of file diff --git a/src/MusicStore/Controllers/ShoppingCartController.cs b/src/MusicStore/Controllers/ShoppingCartController.cs index f45544ecc8..496aa11815 100644 --- a/src/MusicStore/Controllers/ShoppingCartController.cs +++ b/src/MusicStore/Controllers/ShoppingCartController.cs @@ -12,11 +12,12 @@ namespace MusicStore.Controllers { public class ShoppingCartController : Controller { - [FromServices] - public MusicStoreContext DbContext { get; set; } + public ShoppingCartController(MusicStoreContext dbContext) + { + DbContext = dbContext; + } - [FromServices] - public IAntiforgery Antiforgery { get; set; } + public MusicStoreContext DbContext { get; } // // GET: /ShoppingCart/ @@ -58,7 +59,10 @@ namespace MusicStore.Controllers // // AJAX: /ShoppingCart/RemoveFromCart/5 [HttpPost] - public async Task RemoveFromCart(int id, CancellationToken requestAborted) + public async Task RemoveFromCart( + [FromServices] IAntiforgery antiforgery, + int id, + CancellationToken requestAborted) { var cookieToken = string.Empty; var formToken = string.Empty; @@ -75,7 +79,7 @@ namespace MusicStore.Controllers } } - Antiforgery.ValidateTokens(HttpContext, new AntiforgeryTokenSet(formToken, cookieToken)); + antiforgery.ValidateTokens(HttpContext, new AntiforgeryTokenSet(formToken, cookieToken)); // Retrieve the current user's shopping cart var cart = ShoppingCart.GetCart(DbContext, HttpContext); diff --git a/src/MusicStore/Controllers/StoreController.cs b/src/MusicStore/Controllers/StoreController.cs index ea9e9e6c76..250b31c3e6 100644 --- a/src/MusicStore/Controllers/StoreController.cs +++ b/src/MusicStore/Controllers/StoreController.cs @@ -10,11 +10,12 @@ namespace MusicStore.Controllers { public class StoreController : Controller { - [FromServices] - public MusicStoreContext DbContext { get; set; } + public StoreController(MusicStoreContext dbContext) + { + DbContext = dbContext; + } - [FromServices] - public IMemoryCache Cache { get; set; } + public MusicStoreContext DbContext { get; } // // GET: /Store/ @@ -43,11 +44,13 @@ namespace MusicStore.Controllers return View(genreModel); } - public async Task Details(int id) + public async Task Details( + [FromServices] IMemoryCache cache, + int id) { var cacheKey = string.Format("album_{0}", id); Album album; - if(!Cache.TryGetValue(cacheKey, out album)) + if (!cache.TryGetValue(cacheKey, out album)) { album = await DbContext.Albums .Where(a => a.AlbumId == id) @@ -58,7 +61,7 @@ namespace MusicStore.Controllers if (album != null) { //Remove it from cache if not retrieved in last 10 minutes - Cache.Set( + cache.Set( cacheKey, album, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));